Sitelet https://github.com/codingcommons/typesafe-i18n/tree/main/packages/importer
Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

typesafe-i18n Importer

In order to import language files that come from an API, spreadsheet or JSON-files, typesafe-i18n provides an importer functionality.

services

There exists an official plugin for Inlang. It allows you to use typesafe-i18n together with the tooling Inlang provides. You can find it here.

custom implementation

You have to write your own logic to get the data, then map it to a dictionary-representation and then call the storeTranslationToDisk function. Here is an example how this could look like:

Setup

See here on more information how to set up typesafe-i18n.

manual installation

npm install typesafe-i18n

Example

An example implementation can be seen in the import.ts-file.

you need to run this script during development or as a CI-process and not at runtime. Create an own file with the implementation and run it with tsx or something similar.

To run this example execute following command in your terminal:

npm run i18n:import

You should see a successful log message show up in your terminal.

In a real world implementation you would have to write your own logic to fetch the data from a service.

Please share your implementation in a PR. typesafe-i18n wants to provide built-in importer-packages in the future.

typesafe-i18n/importer

The typesafe-i18n/importer package provides following:

ImportLocaleMapping interface

interface ImportLocaleMapping {
   locale: string
   translations: BaseTranslation | BaseTranslation[]
   namespaces?: string[]
}

storeTranslationToDisk function

Can be used to write translations for a single locale to disk. If locale matches the baseTranslation the generator will automatically run after the import. If you don't want to run the generator after the translations are being imported, set generateTypes to false.

(mapping: ImportLocaleMapping, generateTypes?: boolean) => Promise<Locale>

storeTranslationsToDisk function

Can be used to write translations for multiple locales to disk. If one of entry's locale matches the baseTranslation the generator will automatically run after the import. If you don't want to run the generator after the translations are being imported, set generateTypes to false.

(mappings: ImportLocaleMapping[], generateTypes?: boolean) => Promise<Locales[]>