Bug 21156 - Internationalization: plural forms, context, and more for JS files
Summary: Internationalization: plural forms, context, and more for JS files
Status: Signed Off
Alias: None
Product: Koha
Classification: Unclassified
Component: I18N/L10N (show other bugs)
Version: master
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Julian Maurice
QA Contact: Testopia
URL:
Whiteboard:
Keywords:
Depends on: 15395
Blocks:
  Show dependency treegraph
 
Reported: 2018-08-03 15:32 UTC by Julian Maurice
Modified: 2018-10-01 07:31 UTC (History)
4 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Bot Control: ---
When did the bot last check this:
Who signed the patch off:
Text to go in the release notes:


Attachments
Bug 21156: Add plural translation capabilities to JS files (109.52 KB, patch)
2018-08-03 15:32 UTC, Julian Maurice
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Julian Maurice 2018-08-03 15:32:01 UTC

    
Comment 1 Julian Maurice 2018-08-03 15:32:52 UTC
Created attachment 77477 [details] [review]
Bug 21156: Add plural translation capabilities to JS files

It adds Javascript equivalent of Koha::I18N's exported subroutines, and
they are used the same way.

String extraction is done only on *.js files and require gettext 0.19
(available in Debian jessie, and also in wheezy-backports)

It adds Javascript library Gettext.js for handling translation and a
Perl script po2json to transform PO file into JSON.

Gettext.js and po2json both come from Locale::Simple.
There are several tools named po2json. It's simpler to integrate this
one into Koha than to check if the good one is installed on the system.
Locale::Simple is not needed.

To avoid polluting the global namespace too much, this patch also
introduce a global JS object named Koha and add some stuff in Koha.i18n

Test plan:
1. Add a translatable string in a JS file. For example, add this:
     alert(__nx("There is one item", "There are {count} items", 3,
     {count: 3}));
   to staff-global.js
2. cd misc/translator && ./translate update fr-FR
3. Open misc/translator/po/fr-FR-messages-js.po, verify that your
   string is present, and translate it
4. cd misc/translator && ./translate install fr-FR
5. (Optional) Verify that
   koha-tmpl/intranet-tmpl/prog/fr-FR/js/locale_data.js exists and
   contains your translation
6. Open your browser on the staff main page, change language and verify
   that the message is translated
7. Repeat 1-6 on OPAC side

Signed-off-by: Marc Véron <veron@veron.ch>
Comment 2 Katrin Fischer 2018-10-01 06:05:11 UTC
1. Add a translatable string in a JS file. For example, add this:
     alert(__nx("There is one item", "There are {count} items", 3,
     {count: 3}));

It looks like we already have a way of doing this with the format() syntax.

Example:
http://git.koha-community.org/gitweb/?p=koha.git;a=blob;f=koha-tmpl/intranet-tmpl/prog/en/includes/select2.inc;hb=705571a2b870fbb652ca210e32ee3e7ca050e75f#l45

How would plural translation work? Can you give examples for context and plurals?
Comment 3 Julian Maurice 2018-10-01 07:31:04 UTC
(In reply to Katrin Fischer from comment #2)
> 1. Add a translatable string in a JS file. For example, add this:
>      alert(__nx("There is one item", "There are {count} items", 3,
>      {count: 3}));
> 
> It looks like we already have a way of doing this with the format() syntax.
Variable substitution works the same way as format() but allows to give names to substitutions, to ease the work of translators (ex: "I like {food} and {drink}" is easier to read and translate than "I like %s and %s")

> How would plural translation work? Can you give examples for context and
> plurals?
For plural translation, gettext will select the correct string based on the integer parameter (the 3rd for __n and __nx, the 4th for __np and __npx) and the Plural-Forms header in the PO file
For instance,
  var count = <a positive integer>
  __n('item', 'items', count)

In English, __n will return 'item' if count == 1, 'items' otherwise
In French, __n will return 'exemplaire' if count == 0 or count == 1, 'exemplaires' otherwise, because Plural-Forms is "nplurals=2; plural=(n > 1);
In Slovenian, __n can return up to 4 different strings, because Slovenian has 4 different "plural forms"

The context is just another string to help disambiguation. Ex:
  __p('Bibliographic record', 'item')

This will appear in the po file as
  msgctxt "Bibliographic record"
  msgid "item"

And you can combine context and plural forms
  __np('Bibliographic record', 'item', 'items', count)