A very simple TypeScript translation utility.
ts-mini-translate is a lightweight translation function set for TypeScript projects. It allows you to easily add multi-language support by managing translation strings in JSON files.
-
Copy the
translatefolder
Simply copy the entiretranslatefolder into your project. -
Enable JSON importing
Ensure your TypeScript configuration allows importing JSON files.
In yourtsconfig.json, add:{ "compilerOptions": { "resolveJsonModule": true } }Alternatively, you can modify the code to fetch JSON files from your webroot or a server endpoint if you prefer not to import them directly.
- Place your translation files (e.g.,
en.json,es.json) inside thetranslate/localesdirectory. - Import and use the functions from
translate/translations.tsin your application.
Loads the translation file for the specified language and sets it as the current language.
Also saves the language preference in localStorage.
- lang: Language code (e.g.,
"en","es"). - Throws: Error if the language file is not found.
Translates a given key using the currently loaded translations.
- key: The translation key.
- Returns: The translated string, or the key itself if not found.
Returns the currently active language code.
Retrieves the language preference previously saved in localStorage, or null if none is set.