WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 4 years ago

Last modified 4 years ago

#4559 closed enhancement (wontfix)

Slightly different approach to javascript localization

Reported by: nbachiyski Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: I18N Keywords: js i18n needs-patch
Focuses: 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 (4)

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

Download all attachments as: .zip

Change History (11)

nbachiyski7 years ago

nbachiyski7 years ago

nbachiyski7 years ago

nbachiyski7 years ago

comment:1 nbachiyski7 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/.

comment:2 Nazgul7 years ago

  • Keywords has-patch added

comment:3 mdawaffe7 years ago

  • Keywords early added

comment:4 westi6 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.

comment:5 Denis-de-Bernardy5 years ago

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

comment:6 nbachiyski4 years ago

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

This was a long time ago.

comment:7 nacin4 years ago

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