Ticket #4559 (new enhancement)

Opened 1 year ago

Last modified 6 months ago

Slightly different approach to javascript localization

Reported by: nbachiyski Assigned to: anonymous
Priority: normal Milestone: 2.9
Component: i18n Version:
Severity: normal Keywords: js i18n has-patch early
Cc:

Description

Although we now have very nice js localization from Michael Adams in [4968], but there are some things that can be improved, more specifically:

  • All the strings for translations have to be extracted from the js source, they have to be give names and have to stay in en external php file
  • There is not support for plural forms
  • The template strings are processed manually (message = obj.delText.replace(/%thing%/g, what);) (this one is not very big problem).

So, I thought, isn't there a way to incorporate gettext into javascript. And after that, I wrote some stupid js gettext implementation. Here is what it looks like:

  • A js file can be xgettext-ed using the perl parser, they look almost the same, regarding the strings: xgettext -L perl <js files>
  • The translations are taken from a jsvascript dictionary, which must be given on object initialization. I wrote a simple function to generate this from a mo file. So we can take extract the strings from js files into a separate mo and give them to the jsgettext on load.
  • There is a simple format function, which can make things like: gettext.format("Here are {count} of {type} dragons!", {count: 8, type: 'mighty'}); or gettext.format("Gimme a {0}, a {1} and a {2}", ['ball', 'gun', 'kite']);
  • There is the usual gettext plural support. The different plural forms are given as a javascript function (we practically have this, because we generate a php function, which does the same thing)

No more words, here is some code:

// the key for plural forms is singular\0plural (like in the mo file)
var trans = {
    baba: 'dyado',
    '{0} baba\0{0} babas': ['{0} dyado', '{0} dyados']
};
var l10n = new gettext(trans);
alert(l10n.gettext("baba"));
alert(gettext.format(l10n.ngettext("{0} baba", "{0} babas", 1), [1]);
alert(gettext.format(l10n.ngettext("{0} baba", "{0} babas", 5), [5]);

Eventually, if you have reached this point and you are not totally bored, you can see the code:

  • The attached archive is a current (rev23) snapshot of the repository at: http://ncb.googlecode.com/svn/js/gettext/
  • In the app sub-folder there is a proof-of-concept simple i18n-ed application, which is ready for execution, just open app.php in your browser
  • You can find some junit unit tests in test.html

What do you think?

Attachments

jsgettext.tar.gz (11.3 kB) - added by nbachiyski on 06/28/07 22:25:25.
js-i18n.diff (34.2 kB) - added by nbachiyski on 08/08/07 18:06:03.
gettext.js (2.3 kB) - added by nbachiyski on 08/08/07 18:50:45.
jsgettext.php (0.7 kB) - added by nbachiyski on 08/09/07 07:58:42.

Change History

06/28/07 22:25:25 changed by nbachiyski

  • attachment jsgettext.tar.gz added.

08/08/07 18:06:03 changed by nbachiyski

  • attachment js-i18n.diff added.

08/08/07 18:50:45 changed by nbachiyski

  • attachment gettext.js added.

08/09/07 07:58:42 changed by nbachiyski

  • attachment jsgettext.php added.

08/13/07 15:52:33 changed by nbachiyski

In order to use it:

  1. Apply js-i18n.diff.
  2. Place gettext.js in wp-includes/js/.
  3. Place jsgettext.php in wp-includes/.

08/13/07 16:59:44 changed by Nazgul

  • keywords changed from js i18n to js i18n has-patch.

09/25/07 00:31:57 changed by mdawaffe

  • keywords changed from js i18n has-patch to js i18n has-patch early.

03/15/08 23:19:46 changed by westi

  • milestone changed from 2.5 to 2.6.

I suspect this doesn't apply to trunk at all anymore.

Pushing to 2.6 for now.