Rrule: Documentation for i18n of toText

Created on 17 Apr 2013  ·  15Comments  ·  Source: jakubroztocil/rrule

Please add better documentation about i18n of the toText() method.

  1. What's the meaning of the today parameter?
  2. Which values are passed to gettext?
  3. How do I actually translate stuff? I'm able to change dayNames and monthNames, but that's only part of it.

Thank you for creating this lib!

enhancement

Most helpful comment

Any updates? How to translate text to other language?

All 15 comments

What's the meaning of the today parameter?

It's not actually used as of now, you can just pass null.

Which values are passed to gettext?
How do I actually translate stuff? I'm able to change dayNames and monthNames, but that's only part of it.

The API isn't very nice.

You need to create a language definition with date related stuff. The default English one is here. tokens are only needed for RRule.fromText.

gettext gets the English string (you can see what strings are used here).

A crude example (you need the current master, older versions cached the output of toText):

// Date-related translations
var portuguese = {
    dayNames: [
        "Domingo",
        "Segunda-Feira",
        "Terça-feira",
        "Quarta-feira",
        "Quinta-feira",
        "Sexta-feira",
        "Sábado"
    ],
    monthNames: [
        "Janeiro",
        "Fevereiro",
        // …
    ]
    // `tokens` are only needed for `RRule.fromText`

};


// Strings
var portugueseStrings = {
    'every': 'cada',
    'until': 'até',
    'day': 'dia',
    'days': 'dias',
    'week': 'semana',
    'weeks': 'semanas',
    'on': 'em'
    // …
};

var gettext =  function(id) {
    // Return pt. string, default to english.
    return portugueseStrings[id] || id;
};


var rule = new RRule(RRule.WEEKLY, {
    interval: 5,
    byweekday: [RRule.MO, RRule.FR],
    dtstart: new Date(2012, 1, 1, 10, 30),
    until: new Date(2012, 12, 31)
});

console.log(rule.toText());
// => every 5 weeks on Monday, Friday until January 31, 2013

console.log(rule.toText(null, gettext, portuguese));
// => cada 5 semanas em Segunda-Feira, Sexta-feira até Janeiro 31, 2013

TODO: improve and document the API (perhaps a Language class would be nice).

The API isn't very nice.

Hm, yeah. There's something missing. Because you can't just translate the words. Depending on the language the order needs to be changed as well, because they have a different grammar. Instead there should be format strings like

singular: 'every week on {{dayNames}}',
plural: 'every {{numWeeks}} weeks on {{dayNames}}'

That's just a rough example. I know that there are a lot edge cases etc. and that it's probably hard to do right.

Just came across https://github.com/airbnb/polyglot.js , but there are many other solutions for i18n

Some thing that just bit me:

In English it's "every week" and "every month". In German it's "jede Woche" and "jeden Monat". Notice the "n". I can't just translate "every", it won't work.

Similar thing with passing "st" or "rd" to gettext. It doesn't make any sense.

Any news or plans? Now that I started using DTSTART and UNTIL I can't use the toText method anymore. Before that I was able to hack around the current implementation.

One more thing for the list: Date formats. I want DD.MM.YYYY and not MMMM DD, YYYY.

I don't understand. In one html file I have this code whith the latests release (2.0).

[code]

trad tests


[/code]
It's doesn't work with the error :

TypeError: _ is undefined
var defaults = _.clone(RRule.DEFAULT_OPTIONS);
in rrule at line 463.

Where I am in wrong ?

You need another library called underscorejs. I'm on my phone otherwise I would have given you a link.

On Oct 30, 2013, at 11:54 PM, elecoest [email protected] wrote:

I don't understand. In one html file I have this code whith the latests release (2.0).

trad tests
It's doesn't work woth the error :

TypeError: _ is undefined

var defaults = .clone(RRule.DEFAULTOPTIONS);
in rrule at line 463.

Where I am in wrong ?


Reply to this email directly or view it on GitHub.

ok, i'd placed undesrcore.js in third position...
I've place underscore.js in first position.
Now i'vva error
TypeError: 2 is not an object
return result.call(this, func.apply(_, args));
in underscore.js

Sorry but this way is ak for every body ?

Nobody has this error before ?

Thanks a lot for your help.

What's the meaning of the today parameter?

It's not actually used as of now, you can just pass null.

Which values are passed to gettext?
How do I actually translate stuff? I'm able to change dayNames and monthNames, but that's only part of it.

The API isn't very nice.

You need to create a language definition with date related stuff. The default English one is here. tokens are only needed for RRule.fromText.

gettext gets the English string (you can see what strings are used here).

A crude example (you need the current master, older versions cached the output of toText):

// Date-related translations
var portuguese = {
    dayNames: [
        "Domingo",
        "Segunda-Feira",
        "Terça-feira",
        "Quarta-feira",
        "Quinta-feira",
        "Sexta-feira",
        "Sábado"
    ],
    monthNames: [
        "Janeiro",
        "Fevereiro",
        // …
    ]
    // `tokens` are only needed for `RRule.fromText`

};


// Strings
var portugueseStrings = {
    'every': 'cada',
    'until': 'até',
    'day': 'dia',
    'days': 'dias',
    'week': 'semana',
    'weeks': 'semanas',
    'on': 'em'
    // …
};

var gettext =  function(id) {
    // Return pt. string, default to english.
    return portugueseStrings[id] || id;
};


var rule = new RRule(RRule.WEEKLY, {
    interval: 5,
    byweekday: [RRule.MO, RRule.FR],
    dtstart: new Date(2012, 1, 1, 10, 30),
    until: new Date(2012, 12, 31)
});

console.log(rule.toText());
// => every 5 weeks on Monday, Friday until January 31, 2013

console.log(rule.toText(null, gettext, portuguese));
// => cada 5 semanas em Segunda-Feira, Sexta-feira até Janeiro 31, 2013

TODO: improve and document the API (perhaps a Language class would be nice).

Hi, I'm trying to get i18n for german, but I can not find what strings are used in "gettext" since the link is broken. I tried to look on the sources but I could find it anything.

Thanks!

What's the meaning of the today parameter?

It's not actually used as of now, you can just pass null.

Which values are passed to gettext?
How do I actually translate stuff? I'm able to change dayNames and monthNames, but that's only part of it.

The API isn't very nice.
You need to create a language definition with date related stuff. The default English one is here. tokens are only needed for RRule.fromText.
gettext gets the English string (you can see what strings are used here).
A crude example (you need the current master, older versions cached the output of toText):

// Date-related translations
var portuguese = {
    dayNames: [
        "Domingo",
        "Segunda-Feira",
        "Terça-feira",
        "Quarta-feira",
        "Quinta-feira",
        "Sexta-feira",
        "Sábado"
    ],
    monthNames: [
        "Janeiro",
        "Fevereiro",
        // …
    ]
    // `tokens` are only needed for `RRule.fromText`

};


// Strings
var portugueseStrings = {
    'every': 'cada',
    'until': 'até',
    'day': 'dia',
    'days': 'dias',
    'week': 'semana',
    'weeks': 'semanas',
    'on': 'em'
    // …
};

var gettext =  function(id) {
    // Return pt. string, default to english.
    return portugueseStrings[id] || id;
};


var rule = new RRule(RRule.WEEKLY, {
    interval: 5,
    byweekday: [RRule.MO, RRule.FR],
    dtstart: new Date(2012, 1, 1, 10, 30),
    until: new Date(2012, 12, 31)
});

console.log(rule.toText());
// => every 5 weeks on Monday, Friday until January 31, 2013

console.log(rule.toText(null, gettext, portuguese));
// => cada 5 semanas em Segunda-Feira, Sexta-feira até Janeiro 31, 2013

TODO: improve and document the API (perhaps a Language class would be nice).

Hi, I'm trying to get i18n for german, but I can not find what strings are used in "gettext" since the link is broken. I tried to look on the sources but I could find it anything.

Thanks!

https://github.com/jakubroztocil/rrule/tree/master/src/nlp

Would be nice to have some documentation how to use it. I've translated to german, but it's looks wrong:

const germanStrings = {
    every: 'jedes',
    until: 'bis',
    day: 'Tag',
    days: 'Tage',
    week: 'Woche',
    weeks: 'Wochen',
    on: 'ein',
    in: 'in',
    'on the': 'auf dem',
    for: 'für',
    and: 'und',
    or: 'oder',
    at: 'bei',
    last: 'zuletzt',
    '(~ approximate)': '(~ approximativ)',
    times: 'Zeiten',
    time: 'Zeit',
    minutes: 'Minuten',
    hours: 'Stunden',
    weekdays: 'Wochentage',
    weekday: 'Wochentag',
    months: 'Monate',
    month: 'Monat',
    years: 'Jahre',
    year: 'Jahr'
  };
const RRULE_GERMAN = {
  dayNames: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
  monthNames: [
    'Januar',
    'Februar',
    'März',
    'April',
    'Mai',
    'Juni',
    'Juli',
    'August',
    'September',
    'Oktober',
    'November',
    'Dezember'
  ]
};
const gettext = id => {
    return germanStrings[id] || id;
};

const rruleText = rule.toText(id => {
    gettext(id);
}, RRULE_GERMAN);

Any updates? How to translate text to other language?

I think the i18n of this library, as of now, is fundamentally flawed. Things like

nth_monthday' => array(
        '1' => 'the 1st',
        '2' => 'the 2nd',
        '3' => 'the 3rd',
        '21' => 'the 21st',
        '22' => 'the 22nd',
        '23' => 'the 23rd',
        '31' => 'the 31st',
        'else' => 'the %{n}th'
    ),

are absolutely necessary for some final user experience usage, in different languages. I'd recommend implementing the logic in this alternative (as mentioned by @wakirin in this post). It would really bring the usage of this library up, since, at this moment, this logic must be done manually, in order to create some usable i18n logic.

UPDATE: after crawling the code, I found a way to do what I wanted, which, for my language, "solved" it. Just for future reference, I'll leave here a list of all the keys you must translate on the getText method you pass on.

 '(~ approximate)'
 'and'
 'at'
 'day'
 'days'
 'every'
 'for'
 'hour'
 'hours'
 'in'
 'last'
 'minutes'
 'month'
 'months'
 'nd'
 'on the'
 'on'
 'or'
 'rd'
 'st'
 'th'
 'the'
 'time'
 'times'
 'until'
 'week'
 'weekday'
 'weekdays'
 'weeks'
 'weeks'
 'year'
 'years'
 'RRule error: Unable to fully convert this rrule to text'

@gongAll Thanks,
With your example, I was able to use moment.js and i18n, as below:

export const RRuleToText = ({ strRRule }) => {
  const language = {
    dayNames: moment.weekdays(),
    monthNames: moment.months()
  };

  const getText = id => {
    return i18n.t(`vendor.rrule.${id}`, id);
  };

  const dateFormat = (year, month, day) =>
    moment()
      .date(day)
      .year(year)
      .month(month)
      .format("LL");

  return strRRule
    ? capitalize(
        RRule.fromString(strRRule).toText(getText, language, dateFormat)
      )
    : "";
};

Will there be an update on this matter ? I have to translate into Japanese; the words order is significantly different, so I have no idea how to proceed...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shorlbeck picture shorlbeck  ·  21Comments

michaelkrog picture michaelkrog  ·  9Comments

kirrg001 picture kirrg001  ·  5Comments

zeluspudding picture zeluspudding  ·  11Comments

shavenwalrus picture shavenwalrus  ·  7Comments