.po files and gettext VS JSON and custom i18n library?

Problem

I need to develop or find some sort of lightweight gettext-like library in JavaScript for browser-side translations. Preferably the same library could be used by node.js if I'd like to translate server-side instead.

1) But, what are really the pros and cons of using .po files and a JavaScript gettext library?
(As opposed to JSON and jQuery Globalize or similar?)

2) And is there any workflow/service that'll allow crowdsourcing of translations that can be exported to a format (.po/json) that can be used by a JavaScript i18n library (gettext implementation, Globalize.js etc.)?

Problem courtesy of: dani

Solution

1) But, what are really the pros and cons of using .po files and a JavaScript gettext library? gettext format is rather common: - so if your serverside is using gettext format already you might share it with client - translators might feel more comfortable with gettext format (or yaml) than with json format - there is a lot of tooling around for it

I personally don't like the translation functions build around gettext - feels clumpsy to me, where lightweight javascript libraries (like http://i18next.com) feel better.

Libs you could use:

  • jed comes with pure gettext implementation
  • i18next supports gettext files but uses a json approach for translation functions

2) And is there any workflow/service that'll allow crowdsourcing of translations that can be exported to a format (.po/json) that can be used by a JavaScript i18n library (gettext implementation, Globalize.js etc.)?

Solution courtesy of: jamuhl

Discussion

I would recommend using l10ns. For any i18n related project you need the following:

  1. A good storage system that stores localization string.
  2. A good localization format that can handle complex formatting and not just simple strings. And by complex formatting I mean a format that can handle plural formatting, genus/context based formatting, number formatting, date formatting etc.

There are very few tools out there that handles both of these points The most common solution is to use gettext together with xgettext. Xgettext is a tool that traverses your source code to sync localization keys between your source code and localization storage. Though gettext is not so good at handling point 2. For instance you can't format a string with two plural words. So strings such as I like 2 cats and 1 dog is very hard to format. Plural formatting is a very complex problem to solve and have a lot of edge cases. Let say that instead of liking just two cats we like 2000. The correct formatted string would be I like 2,000 cats and 1 dog. Did you notice the , in 2,000?. So in order to use gettext's plural solution correctly we also need to use an external library for handling number formatting.

So for point 2, having a good localization format. I find ICU's MessageFormat handle this the best. It's markup language that deals with l10n formatting and it's used by many big name companies such as Google, Apple and Yahoo. It handles the edge cases with plural formatting mentioned above. It also handles a lot of other types of complex formatting. Such as genus context, ordinal formatting, number formatting and date formatting etc.

One tool that support ICU's MessageFormat and have a storage system built in is L10ns. It also supports Xgettext's workflow. You write your source code and then you sync your localization keys.

Discussion courtesy of: einstein

Pros of using gettext is that there are lots of tools available for it, translators are used to using it and you can easily share strings between your javascript and the rest of the front-end.

You can generate .po files from javascript using xgettext, though javascript is not supported by default. If you use the same function names and tell it you're using perl (I think) it works more reliably. There are a few tools to convert .po to json, or just use one of the gettext js implementations.

Discussion courtesy of: dsas

This recipe can be found in it's original form on Stack Over Flow.