Make WordPress Core

Opened 17 years ago

Closed 14 years ago

Last modified 14 years ago

#4559 closed enhancement (wontfix)

Slightly different approach to javascript localization

Reported by: nbachiyski's profile nbachiyski Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: I18N Keywords: js i18n needs-patch
Focuses: Cc:


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(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:
  • 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 (4)

jsgettext.tar.gz (11.3 KB) - added by nbachiyski 17 years ago.
js-i18n.diff (34.2 KB) - added by nbachiyski 17 years ago.
gettext.js (2.3 KB) - added by nbachiyski 17 years ago.
jsgettext.php (683 bytes) - added by nbachiyski 17 years ago.

Download all attachments as: .zip

Change History (11)

17 years ago

17 years ago

17 years ago

#1 @nbachiyski
17 years ago

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/.

#2 @Nazgul
17 years ago

  • Keywords has-patch added

#3 @mdawaffe
17 years ago

  • Keywords early added

#4 @westi
16 years ago

  • 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.

#5 @Denis-de-Bernardy
15 years ago

  • Keywords needs-patch added; has-patch early removed
  • Milestone changed from 2.9 to Future Release

#6 @nbachiyski
14 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

This was a long time ago.

#7 @nacin
14 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.