Features
- Simple and Familiar API
- Unobstrusive and Unopinionated
- Less than 300 bytes – including dependencies!
Install
$ npm install --save rosetta
Usage
; const i18n = ; // set default languagei18n; // add new languagei18n; // append extra key(s) to existing languagei18n; const data = feminine: false username: 'lukeed' date: ; // Retrieve translations// NOTE: Relies on "en" defaulti18n; //=> 'Welcome, lukeed!'i18n; //=> 'I hope you find this useful.'i18n; //=> 'For questions, I'm available on 4/8/2020, any time after 11:00.' // Retrieve translations w/ lang overridei18n; //=> 'Benvindo, lukeed!' // Change default language keyi18n; // Retrieve translations w/ new defaultsi18n; //=> 'Espero que você ache isso útil.'i18n; //=> 'I hope you find this useful.'
API
rosetta(dict?)
Returns: Rosetta
Initializes a new Rosetta
instance.
You may optionally provide an initial translation object.
rosetta.locale(lang?)
Returns: String
Sets the language code for the Rosetta
instance.
This will cause all rossetta.t()
lookups to assume this lang
code.
The function will return the currently active lang
code. This means that a setting a new value will reply with the same value. Additionally, calling locale()
without any argument will return the lang
code that the Rosetta
instance was last given.
lang
Type: String
Required: false
The language code to choose.
If locale()
is called without an argument (or with a falsey value), then the current lang
code is returned.
rosetta.set(lang, table)
Merge (or override) translation keys into the lang
collection.
lang
Type: String
The language code to target.
table
Type: Object
A new record of key-values to merge into the lang
's dictionary.
Each key within the table
can correspond to a function or a string template.
When using a function, it will receive the entire data input (see params
).
You are required to ensure the function returns a (string) value of your liking.
When using a string template, anything within double curly brackets ({{ example }}
) will be interpreted as a key path and interpolated via templite
. The key path can use dot-notation to access nested values from the data input (see params
). Additionally, if a key path did not resolve to a value, an empty string is injected.
const ctx = ; ctx;//=> 'function sees "~DEFAULT~"ctx;//=> 'function sees "123" ctx;//=> 'template sees ""ctx;//=> 'template sees "123"
rosetta.table(lang)
Returns: Object
or undefined
Retrieve the the lang
's full dictionary/table of translation keys.
If the language does not exist (aka, no translations have been provided for it), you'll receive undefined
.
Otherwise, you'll receive the full object as it exists within the Rosetta
instance. See table
.
Important: Manipulating this object is any way will mutate and affect your
Rosetta
instance. Be careful!
lang
Type: String
The language code's table to retrieve.
rosetta.t(key, params?, lang?)
Returns: String
Retrieve the value for a given key
.
Important: In the normal/default mode, an empty string will be returned for unknown keys.
Conversely, in "debug" mode, an error message will be printed andundefined
will be returned for unknown keys.
key
Type: String
or Array<String|Number>
The identifier to retrieve.
A key
can access nested properties via:
- a string that with dot notation —
'foo.bar[1].baz'
- an array of individual key segments —
['foo', 'bar', 1, 'baz']
Important: You are expected to know & traverse your own dictionary structure correctly.
const ctx = ; ctx; ctx; //=> 'apple'ctx; //=> 'apple'
params
Type: any
Optional: true
The data object argument to pass your dictionary keys' string templates and/or functions.
Note: If your string template tries to access a key that doesn't exist, an empty string is injected.
const ctx = ; const user1 = name: 'lukeed' ;const user2 = name: 'Billy' ; ctx; //=> '¡Hola lukeed!' ctx; //=> 'wazzzuppp'ctx; //=> 'Hello, Billy!' ctx; //=> 'Oi , tudo bem?'
lang
Type: String
Optional: true
A language code override without changing the entire Rosetta
instance's default language.
const ctx = ; ctx; //=> set default ctx;//=> (en) 'Hello lukeed!'ctx;//=> (es) '¡Hola lukeed!'ctx;//=> (en) 'Cya'
Debugging
There is a "debug" mode included for development environments.
The only difference with "debug" mode is that rossetta.t()
will log an error to the console when attempting to access a key
that does not exist. Conversely, the main/default runtime will quietly return an an empty string for consistent output.
Otherwise, the API is exactly the same as the main/default export!
This makes it easy to alias or swap the versions for development vs production bundles. Checkout the Configuration section below for recipes.
// debug mode; const i18n = ; i18n; i18n;//=> 'hello' i18n;// [rosetta] Missing the "foobar" key within the "en" dictionary//=> undefined
Note: With the non-"debug" runtime, an empty string would be returned for the
foobar
key.
Configuration
Here are quick configuration recipes for Rollup and webpack that allow you to choose the right version of rosetta
for your current environment without changing you application code.
With both recipes, you will import rosetta
like this:
;
It is up to the bundler to change what 'rosetta'
resolves to...
Rollup
You will need to install @rollup/plugin-alias
before continuing.
const isDev = /*custom logic*/ || !!processenvROLLUP_WATCH; // ..., plugins: // ... entries: rosetta: isDev ? 'rosetta/debug' : 'rosetta'
webpack
The ability to add aliases within webpack comes by default.
One simply needs to add a resolve.alias
value depending on the environment:
const isDev = /*specific to your config*/; moduleexports = //..., resolve: alias: // ..., rosetta: isDev ? 'rosetta/debug' : 'rosetta'
Runtime Support
The library makes use of Object shorthand methods and Object.assign
.
This yields the following support matrix:
Chrome | Safari | Firefox | Edge | IE | Node.js |
---|---|---|---|---|---|
45+ | 9+ | 34+ | 12+ | ❌ | 4.0+ |
If you need to support older platforms, simply attach rosetta
to your project's Babel (or similar) configuration.
Examples
- Using Next.js — Thank you @SharpTech
Official Next.js example using React Hooks and Context to provide SSR, SSG, CSR compatible i18n solutions.
Credits
Thank you @7sempra for gifting the rosetta
name on npm.
License
MIT © Luke Edwards