jquery-timeago icon indicating copy to clipboard operation
jquery-timeago copied to clipboard

I18N made easier for third party applications.

Open qn7o opened this issue 13 years ago • 6 comments
trafficstars

if you include timeago.js inside a large application that already handles i18n, it's now made easy to give timeago.js your own translator() function. and if you don't, timeago will continue to behave the same way than before.

qn7o avatar Jun 25 '12 15:06 qn7o

Yep; at first I renamed it (into "convert()" or something like that), but a colleague suggested me to minify the diff and leave you the discretion to give it the best name ever possible.

qn7o avatar Jun 25 '12 16:06 qn7o

Oh ok, I see what you mean. This is an error; I'll fix that asap, my bad.

qn7o avatar Jun 25 '12 16:06 qn7o

Sorry, I've been meaning to look into some more standard locale/i18n/translation methods for jQuery as I prepare for a Timeago rewrite. Could you fill me in a little more on the significance of the translator function? Is this a convention that plugins adhere to or a convention that another plugin expects?

rmm5t avatar Jun 25 '12 19:06 rmm5t

I'm not sure if this is a "convention", but in our case (and I hope several others) it's very convenient: our application (OpenERP) uses a rich javascript web client which implements its own i18n mechanism. The translator() function will work like a gettext() function.

This mechanism is also the reason why the resulting string has to be processed in this order: 1/ deduce the strings to use and the number which will go in it 2/ put the strings together 3/ translate the complete string way more comprehensive for translator contributors as it gives more context than little bits of sentence like "ago" or "from now" -- dont forget than translators are not always tech people [1]) 4/ substitute %d with the proper number

This way to proceed seems more powerful than translating each bit of the string separately, as can't be sure that every language will use the form "[prefix] words [suffix]", or handle the plural with more fineness than "one or more" [2].

This is made really easy thanks to the translator() function which will act as a placeholder: when initializing timeago, we just give it our own translating function and we're good to go ! And if no function is defined, timeago will continue to behave as expected.

Hope that helps !

[1] especially when using systems like transifex ou launchpad rosetta [2] see http://www.codeproject.com/Articles/222389/Delivering-Software-to-a-Global-Audience, probably a good read if you are about to refactor the i18n part, especially the "Pluralization" and the "Formatting of Plurals" sections.

qn7o avatar Jun 26 '12 09:06 qn7o

@abourguignon Did you know that Timeago's built-in string settings can either take a string or a function? Does that help you in some way here. You could always assign the same function to different settings if that helps.

The Russian translation is a good example of this.

More Locale Examples

rmm5t avatar Jun 29 '12 14:06 rmm5t

Yep, I know, but it's too early in the code to produce a proper translation.

Once again, the translation function has to be applied on the final string, just before number's substitution. See previous explanations and examples in the linked article.

qn7o avatar Jul 03 '12 16:07 qn7o