question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to localize text unknown at compile time?

See original GitHub issue

Problem: A lot of our application text (or keys, that’s irrelevant in this case) comes from a backend. For example, enum types. We’d like to localize those texts too, and we can’t use i18nMark because, as discussed at #177, this content is unknown at compile time.

Question: Is there a way to solve this problem using linguijs? Or plans to support this in the future?

Feature request: Given a fully automated solution seems unfeasible (beyond what was commented at https://github.com/lingui/js-lingui/issues/177#issuecomment-373749758), our current approach is to manually add entries (without an origin) to the catalogs. However, in this case lingui extract marks these entries as obsolete, so we risk deleting them whenever anyone runs lingui extract --clean. Is it even acceptable to manually modify the catalog? Would it be possible to support adding manual entries to the catalog?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
tricoder42commented, Aug 27, 2018

I’m trying to say that my original proposal isn’t about loading locales, but only message definitions. No matter if messages are extracted from source files or remote API, first you always need just message definitions and then you create locale files by merging with existing catalogs. Yes, we could also add loading translations from API, but that’s another step.

In your case you would load message IDs and default values:

export default async function loadEnumsFromGraphQL() {
    await response
    return [
        {
            id: 'Episode.NEWHOPE',
            defaults: 'New Hope'
        }, {
            id: 'Episode.EMPIRE',,
            defaults: 'Empire Strikes Back'
        }, {
            id: 'Episode.JEDI',,
            defaults: 'Return of the Jedi'
        }, 
    ]
}

In gettext, the workflow was usually extract file with message definitions messages.pot (mind the pot extension) from source files and then generate locale files en.po, cs.po, etc.

My idea here is similar: query API for message definitions only, because then we can guarantee that each locale files have the same messages (and warn user about missing translations, etc).

Also if export of that file is supposed to be a catalog, how would you tackle asynchronous nature of it? It has to be able to return Promise and wait for it.

Reading from FS is synchronous at the moment, but we could change it to be async and then the messages from all sources would be loaded in parallel.

2reactions
tricoder42commented, Aug 27, 2018

At the moment I’m implementing this RFC which improves localeDir config. I guess we could add support for a) static catalogs and b) catalogs loaded from scripts (e.g. query graphql backend)

Something like:

{
   "lingui": {
      "localeDir": {
         // messages.json extracted from source code
         "./locale/{locale}/messages": "./src/",

         // graphql.json loaded from script
         "./locale/{locale}/graphql": "!./scripts/loadGraphQLEnums.js",

         // static.json edited manually
         "./locale/{locale}/static": true,
      }
   }
}

Loading from script

Load message descriptors from local FS or remote API and export them. lingui extract will collect them and create a catalog for them, something like this:


const catalog = loadEnumsFromGraphQL()
// catalog = [
  {
    id: 'Episode.NEWHOPE',
  }, {
    id: 'Episode.EMPIRE',
  }, {
    id: 'Episode.JEDI',
  }, 
]

export default catalog

Generated graphql.json will look like this (depending on format, this is minimal json, where values are translations):

{
    "Episode.NEWHOPE": "",
    "Episode.EMPIRE": "",
    "Episode.JEDI": "",
}

Static files

These files are basically ignored in lingui extract, but messages from them are used in lingui compile.


What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Translate string that are unknown at compile time
I am writing an application which has content that can be added from external sources (through a set of JSON files).
Read more >
How to localize unknown text at runtime? - Inter-illusion
To localize the text in this gameObject at runtime, you use the callback, "CallbackNotification.cs" to specify the player color:.
Read more >
How to localize your iOS app - Hacking with Swift
Localizing text you create in code. Go to the File menu and choose New > File, then select Strings File from the list...
Read more >
Isolate localizable resources - Globalization | Microsoft Learn
Figure 1: The string "Unknown Modem" was hard-coded and thus went untranslated. Hard-coded period. Figure 2: The string is a text resource ...
Read more >
Do I have to re-assign localized text every time the game loads?
Localization systems I've seen generally work in a "pull" approach, rather than a "push". ie. instead of pushing out a change to all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found