3
votes

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.

2

2 Answers

1
votes

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.

-1
votes

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", /*...*/ },
/*...*/
]