Forums

[resolved] Regional support for Relative Time (3 posts)

  1. Workshopshed
    Member
    Posted 12 months ago #

    Many of the twitter widgets (including my own) use a relative time jscript function called "relative_time".

    e.g. http://twitter.pbworks.com/w/page/1779897/RelativeTimeScripts

    This returns things like "1 minute ago", "4 hours ago", "yesterday" etc.

    Has anyone managed to produce a version of this that can be regionalised for different countries?

    e.g. vor 1 Tag
    or
    il y a 1 jour

    Would be interested to hear of example of this?

  2. Workshopshed
    Member
    Posted 12 months ago #

    I've found some useful information here which should allow a regionalised version to be created.

    http://codex.wordpress.org/I18n_for_WordPress_Developers

    "Use wp_localize_script() to add server-side data to a previously enqueued script."

    wp_enqueue_script( 'script-handle', … );
    wp_localize_script( 'script-handle', 'objectL10n', array(
    	'speed' => $distance / $time,
    	'submit' => __( 'Submit' ),
    ) );

    "Then in the JavaScript file, corresponding to script-handle you can use objectL10n.variable:"

    $('#submit').val(objectL10n.submit);
    $('#speed').val('{speed} km/h'.replace('{speed}', objectL10n.speed));
  3. Workshopshed
    Member
    Posted 12 months ago #

    Here's my solution to a regionalised version of the relative time javascript function.

    In your plugin first queue then localise your script.

    wp_enqueue_script("LocRelativeTime",plugins_url('/Loc_Relative_Time.js', __FILE__));
    wp_localize_script("LocRelativeTime", 'RelTimeL10n', array(
    	                       'RelTSeconds' => __('less than a minute ago'),
    	                       'RelTMinute' => __( 'about a minute ago' ),
                               'RelTMinutes' => __('%s minutes ago'),
                               'RelTHour' => __('about an hour ago'),
                               'RelTHours' => __('%s hours ago'),
                               'RelTDay' =>  __('1 day ago')
                                ) );

    What you will see in your page source will be a little extra section of javascript that's been created by the wp_localize_script call.

    <script type='text/javascript'>
    /* <![CDATA[ */
    var RelTimeL10n = {
    	RelTSeconds: "less than a minute ago",
    	RelTMinute: "about a minute ago",
    	RelTMinutes: "%s minutes ago",
    	RelTHour: "about an hour ago",
    	RelTHours: "%s hours ago",
    	RelTDay: "1 day ago"
    };
    /* ]]> */

    Then you can use these strings in your function with some simple replaces where necessary.

    /* A localised version of the Relative Time Function */
    
    function Loc_relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      delta = delta + (relative_to.getTimezoneOffset() * 60);
    
      if (delta < 60) {
        return RelTimeL10n.RelTSeconds;
      } else if(delta < 120) {
        return RelTimeL10n.RelTMinute;
      } else if(delta < (60*60)) {
        return RelTimeL10n.RelTMinutes.replace('%s',parseInt(delta / 60).toString());
      } else if(delta < (120*60)) {
        return val(RelTimeL10n.RelTHour);
      } else if(delta < (24*60*60)) {
        return RelTimeL10n.RelTHours.replace('%s',parseInt(delta/ 3600).toString());
      } else if(delta < (48*60*60)) {
        return RelTimeL10n.RelTDay;
      } else {
        return RelTimeL10n.RelTDays.replace('%s',parseInt(delta / 86400).toString());
      }
    }

Reply

You must log in to post.

About this Topic