Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Give your JavaScript the ability to speak many languages known on Earth.
A tiny i18n helper library
This is a modern fork of airbnb/polyglot.js – Polyglot.js is a tiny I18n helper library written in JavaScript, made to work both in the browser and in CommonJS environments (Node). It provides a simple solution for interpolation and pluralization, based off of Airbnb’s experience adding I18n functionality to its Backbone.js and Node apps.
I18n is incredibly important for us at Airbnb, as we have listings in 192 countries, and we translate our site into 30-odd different languages. We’re also hiring talented engineers to help us scale up to meet the challenges of building a global marketplace.
TerraGlot is agnostic to your translation backend. It doesn’t perform any translation; it simply gives you a way to manage translated phrases from your client- or server-side JavaScript application.
install with npm:
$ npm install terraglot
Clone the repo, run npm install
, and npm test
.
First, create an instance of the Polyglot
class, which you will use for translation.
var terraglot = new TerraGlot();
TerraGlot is class-based so you can maintain different sets of phrases at the same time, possibly in different locales. This is very useful for example when serving requests with Express, because each request may have a different locale, and you don’t want concurrent requests to clobber each other’s phrases.
See Options Overview for information about the options object you can choose to pass to new TerraGlot
.
Tell TerraGlot what to say by simply giving it a phrases object, where the key is the canonical name of the phrase and the value is the already-translated string.
terraglot.extend({
"hello": "Hello"
});
terraglot.t("hello");
=> "Hello"
You can also pass a mapping at instantiation, using the key phrases
:
var terraglot = new TerraGlot({ phrases: { hello: 'Hello' } });
TerraGlot doesn’t do the translation for you. It’s up to you to give it the proper phrases for the user’s locale.
A common pattern is to gather a hash of phrases in your backend, and output
them in a <script>
tag at the bottom of the document. For example, in Rails:
app/controllers/home_controller.rb
def index
@phrases = {
"home.login" => I18n.t("home.login"),
"home.signup" => I18n.t("home.signup"),
...
}
end
app/views/home/index.html.erb
<script>
var terraglot = new TerraGlot({phrases: <%= raw @phrases.to_json %>});
</script>
And now you can utilize i.e. terraglot.t("home.login")
in your JavaScript application
or Handlebars templates.
terraglot.t()
also provides interpolation. Pass an object with key-value pairs of
interpolation arguments as the second parameter.
terraglot.extend({
"hello_name": "Hola, %{name}."
});
terraglot.t("hello_name", {name: "DeNiro"});
=> "Hola, DeNiro."
TerraGlot also supports nested phrase objects.
terraglot.extend({
"nav": {
"hello": "Hello",
"hello_name": "Hello, %{name}",
"sidebar": {
"welcome": "Welcome"
}
}
});
terraglot.t("nav.sidebar.welcome");
=> "Welcome"
The substitution variable syntax is customizable.
var terraglot = new TerraGlot({
phrases: {
"hello_name": "Hola {{name}}"
},
interpolation: {prefix: '{{', suffix: '}}'}
});
terraglot.t("hello_name", {name: "DeNiro"});
=> "Hola, DeNiro."
For pluralization to work properly, you need to tell TerraGlot what the current locale is. You can use terraglot.locale("fr")
to set the locale to, for example, French. This method is also a getter:
terraglot.locale()
=> "fr"
You can also pass this in during instantiation.
var terraglot = new TerraGlot({ locale: 'fr' });
Currently, the only thing that TerraGlot uses this locale setting for is pluralization.
TerraGlot provides a very basic pattern for providing pluralization based on a single string that contains all plural forms for a given phrase. Because various languages have different nominal forms for zero, one, and multiple, and because the noun can be before or after the count, we have to be overly explicit about the possible phrases.
To get a pluralized phrase, still use terraglot.t()
but use a specially-formatted phrase string that separates the plural forms by the delimiter ||||
, or four vertical pipe characters.
For pluralizing "car" in English, TerraGlot assumes you have a phrase of the form:
terraglot.extend({
num_cars: '%{smart_count} car |||| %{smart_count} cars'
});
Please keep in mind that smart_count
is required. No other option name is taken into account to transform pluralization strings.
In English (and German, Spanish, Italian, and a few others) there are only two plural forms: singular and not-singular.
Some languages get a bit more complicated. In Czech, there are three separate forms: 1, 2 through 4, and 5 and up. Russian is even more involved.
var terraglot = new TerraGlot({ locale: 'cs' }); // Czech
terraglot.extend({
num_foxes: 'Mám %{smart_count} lišku |||| Mám %{smart_count} lišky |||| Mám %{smart_count} lišek'
});
terraglot.t()
will choose the appropriate phrase based on the provided smart_count
option, whose value is a number.
terraglot.t("num_cars", {smart_count: 0});
=> "0 cars"
terraglot.t("num_cars", {smart_count: 1});
=> "1 car"
terraglot.t("num_cars", {smart_count: 2});
=> "2 cars"
As a shortcut, you can also pass a number to the second parameter:
terraglot.t("num_cars", 2);
=> "2 cars"
TerraGlot provides some default pluralization rules for some locales. You can specify a different set of rules through the pluralRules
constructor param.
var terraglot = new TerraGlot({
pluralRules: {
pluralTypes: {
germanLike: function (n) {
// is 1
if (n === 1) {
return 0;
}
// everything else
return 1;
},
frenchLike: function (n) {
// is 0 or 1
if (n <= 1) {
return 0;
}
// everything else
return 1;
}
},
pluralTypeToLanguages: {
germanLike: ['de', 'en', 'xh', 'zu'],
frenchLike: ['fr', 'hy']
}
}
});
This can be useful to support locales that TerraGlot does not support by default or to change the rule definitions.
The most-used method. Provide a key, and t()
will return the phrase.
terraglot.t("hello");
=> "Hello"
The phrase value is provided first by a call to terraglot.extend()
or terraglot.replace()
.
Pass in an object as the second argument to perform interpolation.
terraglot.t("hello_name", {name: "Spike"});
=> "Hello, Spike"
Pass a number as the second argument as a shortcut to smart_count
:
// same as: terraglot.t("car", {smart_count: 2});
terraglot.t("car", 2);
=> "2 cars"
If you like, you can provide a default value in case the phrase is missing. Use the special option key "_" to specify a default.
terraglot.t("i_like_to_write_in_language", {
_: "I like to write in %{language}.",
language: "JavaScript"
});
=> "I like to write in JavaScript."
Use extend
to tell TerraGlot how to translate a given key.
terraglot.extend({
hello: 'Hello',
hello_name: 'Hello, %{name}'
});
The key can be any string. Feel free to call extend
multiple times; it will override any phrases with the same key, but leave existing phrases untouched.
Use unset
to selectively remove keys from a TerraPlot instance.
unset
accepts one argument: either a single string key, or an object whose keys are string keys, and whose values are ignored unless they are nested objects (in the same format).
Example:
terraglot.unset('some_key');
terraglot.unset({
hello: 'Hello',
hello_name: 'Hello, %{name}',
foo: {
bar: 'This phrase’s key is "foo.bar"'
}
});
Get or set the locale (also can be set using the constructor option, which is used only for pluralization. If a truthy value is provided, it will set the locale. Afterwards, it will return it.
Clears all phrases. Useful for special cases, such as freeing up memory if you have lots of phrases but no longer need to perform any translation. Also used internally by replace
.
Completely replace the existing phrases with a new set of phrases.
Normally, just use extend
to add more phrases, but under certain circumstances, you may want to make sure no old phrases are lying around.
Returns true
if the key does exist in the provided phrases, otherwise it will return false
.
Takes a phrase string and transforms it by choosing the correct plural form and interpolating it. This method is used internally by t.
The correct plural form is selected if substitutions.smart_count is set.
You can pass in a number instead of an Object as substitutions
as a shortcut for smart_count
.
You should pass in a third argument, the locale, to specify the correct plural type. It defaults to 'en'
which has 2 plural forms.
new TerraGlot
accepts a number of options:
phrases
: a key/value map of translated phrases. See Translation.locale
: a string describing the locale (language and region) of the translation, to apply pluralization rules. see PluralizationallowMissing
: a boolean to control whether missing keys in a t
call are allowed. If false
, by default, a missing key is returned and a warning is issued.onMissingKey
: if allowMissing
is true
, and this option is a function, then it will be called instead of the default functionality. Arguments passed to it are key
, options
, and locale
. The return of this function will be used as a translation fallback when terraglot.t('missing.key')
is called (hint: return the key).interpolation
: an object to change the substitution syntax for interpolation by setting the prefix
and suffix
fields.pluralRules
: an object of pluralTypes
and pluralTypeToLanguages
to control pluralization logic.v3.2.0: September 1, 2022
types
field in package.json
for TypeScriptFAQs
Give your JavaScript the ability to speak many languages known on Earth.
The npm package terraglot receives a total of 1 weekly downloads. As such, terraglot popularity was classified as not popular.
We found that terraglot demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.