4

I have a project that I want to be localizable. While most strings are in the source code, where xgettext/Poedit can easily find them when wrapped with the localization function call, some are in pure JSON files, that I'm using for data storage. Since it's just JSON, and not actually JS, I can't use function calls. For example, a little database:

somedb.txt

[
{ "id": 1, "name": "Xyz", "local": "AxWhyZzz", /*...*/ },
/*...*/
]

Is there a way to extract the "local" values from the JSON files with xgettext? And if there isn't, what are my options? Creating a source file that has all local values, wrapped with calls to _?

Alternatively I could write my own parser of course, or modify gettext, but I'd much rather use existing solutions if available.

Mars
  • 912
  • 1
  • 10
  • 21

2 Answers2

2

No, there isn't a way. JSON is just a generic container format, the actual meaning of the values is domain/application specific — and xgettext must understand the meaning to know what to extract. How could it understand your homegrown format?

For XML files, this is solved by ITS (v2), which gettext (and thus Poedit) supports since 0.19.7. But for JSON, nothing exists… yet. There's some work being done (see here and here and here), though.

Václav Slavík
  • 6,445
  • 2
  • 28
  • 25
  • I think calling it a homegrown format is a stretch, JSON is a clearly specified standard, isn't it? xgettext should technically be able to parse specific values based on their keys, it can manage XML as well after all, as you've mentioned. This still answers my question though, too bad there isn't much yet =/ Thanks. – Mars Feb 13 '16 at 12:39
  • JSON *syntax* is a standard. It's not a file format in any meaningful way, because the *structure* and *semantics* of your file are not defined by it. JSON spec doesn't say there should be fields named "id", "name" and "local", you say that in the definition of the your custom file format built atop the JSON syntax. You need to understand the semantics of XML as well. ITS is just that: a formal way to define a subset of semantics relevant to translation. XML is not a format, e.g. AppData files are, and ITS definition for .appdata.xml files enables xgettext to understand them. – Václav Slavík Feb 13 '16 at 13:52
  • I don't know, I expected there to be something. Just like "id" and "name" are "custom", a function name like "getmethelocalization" is custom as well, but xgettext can still easily look for it, because it knows the *syntax* of a function call. Similarly it would be fairly easy to teach it to look for values in a JSON file, for example by looking for a "local" key. There's no big mystery there, the syntax being standardized is enough. – Mars Feb 13 '16 at 21:28
  • You should [have a look](http://git.savannah.gnu.org/cgit/gettext.git/tree/gettext-tools/its) at how relatively complicated specifying extraction rules in ITS (which is, as far as complexity goes, a problem identical to extracting from JOSN) is, even for simple files, before making categorical, but sadly uninformed, pronouncements about how "fairy easy" it is. It isn't comparable to `-kgetmethelocalization` at all, neither implementation-wise nor from user's POV. But if you still think it's so easy, we lesser programmers would sure appreciate your contribution to GNU gettext to handle JSON ;-) – Václav Slavík Feb 14 '16 at 09:21
  • I'm looking at this from a simple coding standpoint, finding values for keys is simply not complicated. I didn't look at the gettext src yet, it might not be as easy without extending the parser, since it probably wasn't made with JSON in mind, but you'll excuse me for naively thinking you lesser programmers may have done that already, some time in the past 21 years ;-) – Mars Feb 14 '16 at 14:03
  • There's no magical super-parser that you need to only "extend"; there's a new (generic, not specific to *your* files) parser to be written. I'll also remind you that JSON isn't 21 years old, but was standardized in 2013 after ~10 years of some use. And anecdotally, people only started complaining about lack of support for their custom JSON files in Poedit/xgettext in early 2015 or so, so its wider use for *translation* purposes is somewhat recent. GNU gettext devs aren't clairvoyant, sorry. And they aren't as incompetent as you seem to believe either. – Václav Slavík Feb 14 '16 at 19:02
  • I'm not sure what makes you think that I believe them to be incompetent, I don't think I've stated anything like that, nor am I complaining. Actually it's quite the contrary, gettext is marvelous, and I'd have a hard time thinking bad about its developers. I simply said JSON == simple, which it is, and that I'm surprised nothing has been done in regards to parsing it. I will admit that I didn't know JSON had only been standardized this recently, that does explain a few things. – Mars Feb 15 '16 at 16:22
-1

Here is the way you will get them as JS arrays through XMLHttpRequest: http://codepen.io/KryptoniteDove/post/load-json-file-locally-using-pure-javascript

Also there is a way to include somedb.txt as a valid js if you modify it by adding variable id somevar to provide further access:

somevar = [
{ "id": 1, "name": "Xyz", "local": "AxWhyZzz", /*...*/ },
/*...*/
]
kay27
  • 897
  • 7
  • 16