@yuo-app/lin v0.1.6
get started
install
npm i -D @yuo-app/linor use -g to install globally for non-npm projects.
setup
You will need:
- a project with i18n set up
- a default locale JSON file (e.g.
en-US.json) OPENAI_API_KEYin your .env file
lin needs to know 3 things to work:
- locales: an array of locales to translate
- defaultLocale: the default from the locales array
- directory: the folder with the locale JSON files
Create an i18n.config.ts (or js, json, etc, see Config) file in the root of your project:
import { defineConfig } from '@yuo-app/lin'
export default defineConfig({
locales: ['en-US', 'es-ES'],
defaultLocale: 'en-US',
directory: 'locales',
})!IMPORTANT
linwill be able to infer all these from your existing i18n setup.
usage
!TIP Run
lin -handlin <command> -hto see all the options.
translate
The translate command syncs all locale JSON files with the default locale JSON file. It finds the missing keys in locales, and translates them with one GPT request.
lin translateTo translate only specific locales, list them like this:
lin translate es frYou can also use the translate command to add a new language.
- First add the locale code to
localesin the i18n config - Then run
lin translateand it will create the new locale JSON file
!NOTE There is some syntax around locale codes:
- locale JSONs should be named with the full locale code (e.g.
en-US.json): lanugage code 2 characters, country code 2 characters- in commands - just like above - you can use the first 2 letters as a shorthand (e.g.
ento match en-**)allis a special keyword that matches all localesdefmeans the default locale from the config
add
add can be useful when writing a new part of the UI. You can use it to quickly add a new key to the default locale and translate it to all the locales.
lin add ui.button.save Text of the save buttonui.button.save will be the key inserted to the JSONs, and the rest of the arguments will be the value ie. the translated text.
!NOTE if the key is nested, it should be in dot notation like
ui.button.save
To add a key to a specific locale, use the -l flag:
lin add -l ko ui.button.save Text of the save buttonListing multiple locales is done by using the -l flag multiple times:
lin add ui.button.save -l jp Text of the save button -l zh(flags can be all over the place, but their values stop at the first space)
For adding more keys, it's usually best to just directly edit the default JSON and then use lin translate to translate them. But translate does not remove keys, so del is needed for that.
del
del just removes keys from the locale JSON files.
lin del nav.title footer.descriptiontidy
tidy provides a quick way to check everything is set up correctly; it prints info about the locales.
lin tidyYou can also use it to sort the locale JSONs which can happen as translate and add don't keep the order.
To sort alphabetically:
lin tidy abcTo sort with respect to the default locale:
lin tidy defconfig
!TIP All properties in the config can be used as CLI flags too.
config file
Use only one config file.
lin config
lin.config.ts
lin.config.{ts, mts, cts, js, mjs, cjs, json, ∅}.linrc.{ts, mts, cts, js, mjs, cjs, json, ∅}lininpackage.jsonlinin your vite or nuxt config
i18n config
i18n.config.tsori18ninlin.config.ts
i18nin lin configi18n.config.{ts, mts, cts, js, mjs, cjs, json, ∅}.i18nrc.{ts, mts, cts, js, mjs, cjs, json, ∅}lin.i18ninpackage.jsonlin.i18nin your vite or nuxt config
LLM config
for the add and translate commands
The OpenAI options (ex. temperature) are exposed directly in options in the lin config.
context in config
This simple string is directly added to the system prompt. Use it to provide extra information to the LLM about your project.
context: 'hello gpt friend, how do you do'with arg
You can use this flag with locales to add them to the context window. This will add the entire ja-JP.json file to the LLM.
lin translate zh -w jp