Skip to main content

Web apps with JSON catalogs

easyi18n translates a JavaScript or TypeScript app that keeps one JSON file per locale: Angular with Transloco or ngx-translate, and React, Vue or Node apps that use i18next. The base-language file is the source of truth. You add strings to it, and easyi18n writes the file of every other language.

The web adapter needs CLI 1.0.0 or later.

What init sets up​

init with stack: auto selects the web stack when package.json depends on one of these libraries. easyi18n init --stack web names it explicitly.

LibraryICU pluginWiring
@jsverse/transloco@jsverse/transloco-messageformatprovideTranslocoMessageformat() beside provideTransloco()
@ngx-translate/corengx-translate-messageformat-compiler and @messageformat/coreprovideTranslateCompiler(TranslateMessageFormatCompiler)
i18nexti18next-icu and intl-messageformati18next.use(ICU) on every instance, or ICU in its plugins: [...] or use: [...] list

easyi18n writes placeholders as {name} and plurals as ICU, for example {count, plural, one {# item} other {# items}}. Each library reads that syntax through its plugin. init names the plugin and prints the wiring. It does not edit package.json: install the plugin with your package manager. doctor fails until the plugin is in package.json and wired in the app.

An i18next app with server rendering has more than one instance, and each one formats only with the plugins it registered. In Remix with remix-i18next, the client, the instance entry.server.tsx creates with createInstance() and the plugins: [...] list of RemixI18Next each need ICU. doctor reads every file that chains .use() into .init() on i18next or passes i18next plugins in a plugins: or use: list, and warns for each one without ICU, naming the file. It fails only when no file registers ICU.

init writes an easyi18n.yaml like this one:

workspace: acme
project: web-app
mode: a
format: json
output: public/i18n
source: public/i18n/en.json
sourceRoot: public/i18n
sourceLayout: locale-files

source is the base-language file that sync uploads. output is the directory where the files of the other languages are written. format is json for a flat file and nested_json when the base file nests objects.

sourceRoot and sourceLayout are required for source: sync. Paths are relative to easyi18n.yaml; the root must contain the physical source file. The project base language is read from /meta before any catalog upload, including a preview. Choose the layout that describes your input catalog:

sourceLayoutFiles that belong to the base-language catalog
locale-filesFiles named for the base locale, such as en.json and admin/en.json. Locale names ignore case and treat _ and - alike. es.json is a different locale even under a directory named en.
base-treeEvery JSON under sourceRoot, including en.json, es.json, app.json and nested files. Keep target-language directories outside this root.
namespacesi18next namespaces: every JSON directly in sourceRoot, the base-language directory such as locales/en. Each file is one namespace.
scopesTransloco scopes: source:, the root file directly in sourceRoot, and each file named for the base locale one directory below it, such as admin/en.json.

locale-files and base-tree sync one file. namespaces and scopes sync every file of the layout as one catalog.

For i18next namespaces, init declares the base-language directory:

format: json
output: locales
source: locales/en/translation.json
sourceRoot: locales/en
sourceLayout: namespaces

source: names any one of the namespace files; init picks translation.json, the i18next default namespace, when it is there. The project stores each key under its namespace, such as common.save and translation.save, so two namespaces can hold the same key. pull writes locales/<lang>/<namespace>.json for each target language. Each file follows the key order and shape of its base file: a flat file with "home.title" stays flat, and a nested file stays nested. A JSON file in a subdirectory of sourceRoot is refused with several_files_per_locale.

For Transloco scopes, init declares the root file:

format: json
output: public/i18n
source: public/i18n/en.json
sourceRoot: public/i18n
sourceLayout: scopes

The root keys keep their names. The keys of public/i18n/admin/en.json are stored under admin, the name of the directory, and pull writes them back to public/i18n/admin/<lang>.json. A root key named like a scope, such as admin or admin.title next to the scope admin, is refused with catalog_key_collision before anything is uploaded: the server would keep one of the two. A scope two directories deep is refused with several_files_per_locale.

Under locale-files and base-tree, a second base-language file is refused with several_files_per_locale, naming both files. Changing the layout of a project that already synced renames its keys: the next sync creates the new ones and disables the old ones. In locale-files, the source must have a base-language filename in the input inventory, either directly or through a file alias. Aliases and hardlinks of the same file do not count as additional files. Directory names outside the root, YAML location and the working directory do not change the declared layout.

If generated files live inside the input tree, declare their directories separately, for example sourceExclude: [public/i18n/dist]. These paths are also relative to the YAML and must name existing directories. Changing output does not exclude inputs. Directory aliases are resolved before exclusions; an exclusion cannot contain the source file. Hidden directories are skipped unless they contain the configured source. Unreadable directories, unresolved links and invalid input boundaries stop sync before upload.

doctor makes the same selection. With a login or a key with the read scope, it takes the project's base language from the server, as sync does. Without one, or when it cannot read the base language from the server, it takes the filename of source: as the base language. When sync would refuse the selection, doctor fails source with the refusal sync prints and exits with 1. When the refusal comes from a source: named for another language code, such as en-US.json in a project whose base language is en, a detail line names the base language and the filename sync expects.

Migrate an existing configuration​

Add both settings to every configuration that uses source:. For one file per locale, use the locale directory and locale-files; for i18next namespaces, the base-language directory and namespaces; for Transloco scopes, the directory of the root file and scopes. An absent or invalid declaration produces catalog_scope_unknown; sync does not guess a layout or edit the YAML. init writes locale-files, namespaces or scopes for the layout it finds and keeps an existing configuration unchanged.

Run CLI 1.0.0 or later both in the shell and for the MCP server your client starts. An earlier prerelease may ignore sourceLayout and use the previous selector.

init takes the directory from rootTranslationsPath when transloco.config.ts declares it. Otherwise it looks in public/i18n, src/assets/i18n and locales, and creates an empty base file when it finds none. For i18next it also looks for language directories in locales and public/locales and writes namespaces, with en as the base language when that directory exists. For Transloco it writes scopes when a directory beside the root file holds a file of the same name, such as admin/en.json. Its steps name the namespace files or the scopes. A split that is not its library's default, such as an admin/en.json beside an ngx-translate base file, stays locale-files, and sync refuses it with the files named.

Translate and publish​

The agent uses the file-first tools, the same ones an Xcode project uses:

  1. sync_preview uploads the base file as a dry run. It reports the keys the sync would create and the estimated credits.
  2. You approve the spend. sync uploads the base file and queues the translation.
  3. fill_status polls until the translation is complete.
  4. You approve the publish.
  5. pull writes one file per target language into output, for example public/i18n/es.json, or with namespaces and scopes, one per namespace or scope and language.

From a terminal or CI, easyi18n sync runs those steps in one command, and easyi18n pull downloads the published files.

Neither command writes a base-language file. Edit the base files yourself and review the other files in the git diff. Do not edit a target file: the next pull or sync stops on it with edited_targets until the change is in the dashboard or discarded. Reload the app to load the new files.

Limits​

  • Several files per locale only as i18next namespaces or Transloco scopes one directory deep, declared with sourceLayout. Any other split, such as a custom loadPath, is refused with several_files_per_locale before anything is uploaded: a sync is the whole catalog of the project, so uploading one file would disable the keys of the others.
  • The agent moves your literals into the base file by hand, following the setup prompt. Extracting t('...') calls from the source is on the roadmap.
  • Angular's built-in i18n (i18n attributes and $localize) has its own adapter: see Angular apps.