Internationalisation
Bundled Plugins API
i18n

i18n.lua

A very complete i18n lib for Lua

Description

1
i18n = require 'i18n'
2
3
-- loading stuff
4
i18n.set('en.welcome', 'welcome to this program')
5
i18n.load({
6
en = {
7
good_bye = "good-bye!",
8
age_msg = "your age is %{age}.",
9
phone_msg = {
10
one = "you have one new message.",
11
other = "you have %{count} new messages."
12
}
13
}
14
})
15
i18n.loadFile('path/to/your/project/i18n/de.lua') -- load German language file
16
i18n.loadFile('path/to/your/project/i18n/fr.lua') -- load French language file
17
-- section 'using language files' below describes structure of files
18
19
-- setting the translation context
20
i18n.setLocale('en') -- English is the default locale anyway
21
22
-- getting translations
23
i18n.translate('welcome') -- Welcome to this program
24
i18n('welcome') -- Welcome to this program
25
i18n('age_msg', {age = 18}) -- Your age is 18.
26
i18n('phone_msg', {count = 1}) -- You have one new message.
27
i18n('phone_msg', {count = 2}) -- You have 2 new messages.
28
i18n('good_bye') -- Good-bye!
Copied!

Interpolation

You can interpolate variables in 3 different ways:
1
-- the most usual one
2
i18n.set('en.variables', 'Interpolating variables: %{name} %{age}')
3
i18n('variables', {name='john', 'age'=10}) -- Interpolating variables: john 10
4
5
i18n.set('en.lua', 'Traditional Lua way: %d %s')
6
i18n('lua', {1, 'message'}) -- Traditional Lua way: 1 message
7
8
i18n.set('en.combined', 'Combined: %<name>.q %<age>.d %<age>.o')
9
i18n('combined', {name='john', 'age'=10}) -- Combined: john 10 12k
Copied!

Pluralization

This lib implements the unicode.org plural rules. Just set the locale you want to use and it will deduce the appropiate pluralization rules:
1
i18n = require 'i18n'
2
3
i18n.load({
4
en = {
5
msg = {
6
one = "one message",
7
other = "%{count} messages"
8
}
9
},
10
ru = {
11
msg = {
12
one = "1 сообщение",
13
few = "%{count} сообщения",
14
many = "%{count} сообщений",
15
other = "%{count} сообщения"
16
}
17
}
18
})
19
20
i18n('msg', {count = 1}) -- one message
21
i18n.setLocale('ru')
22
i18n('msg', {count = 5}) -- 5 сообщений
Copied!
The appropiate rule is chosen by finding the 'root' of the locale used: for example if the current locale is 'fr-CA', the 'fr' rules will be applied.
If the provided functions are not enough (i.e. invented languages) it's possible to specify a custom pluralization function in the second parameter of setLocale. This function must return 'one', 'few', 'other', etc given a number.

Fallbacks

When a value is not found, the lib has several fallback mechanisms:
  • First, it will look in the current locale's parents. For example, if the locale was set to 'en-US' and the key 'msg' was not found there, it will be looked over in 'en'.
  • Second, if the value is not found in the locale ancestry, a 'fallback locale' (by default: 'en') can be used. If the fallback locale has any parents, they will be looked over too.
  • Third, if all the locales have failed, but there is a param called 'default' on the provided data, it will be used.
  • Otherwise the translation will return nil.
The parents of a locale are found by splitting the locale by its hyphens. Other separation characters (spaces, underscores, etc) are not supported.

Using language files

It might be a good idea to store each translation in a different file. This is supported via the 'i18n.loadFile' directive:
1
2
i18n.loadFile('path/to/your/project/i18n/de.lua') -- German translation
3
i18n.loadFile('path/to/your/project/i18n/en.lua') -- English translation
4
i18n.loadFile('path/to/your/project/i18n/fr.lua') -- French translation
5
Copied!
The German language file 'de.lua' should read:
1
return {
2
de = {
3
good_bye = "Auf Wiedersehen!",
4
age_msg = "Ihr Alter beträgt %{age}.",
5
phone_msg = {
6
one = "Sie haben eine neue Nachricht.",
7
other = "Sie haben %{count} neue Nachrichten."
8
}
9
}
10
}
Copied!
If desired, you can also store all translations in one single file (eg. 'translations.lua'):
1
return {
2
de = {
3
good_bye = "Auf Wiedersehen!",
4
age_msg = "Ihr Alter beträgt %{age}.",
5
phone_msg = {
6
one = "Sie haben eine neue Nachricht.",
7
other = "Sie haben %{count} neue Nachrichten."
8
}
9
},
10
fr = {
11
good_bye = "Au revoir !",
12
age_msg = "Vous avez %{age} ans.",
13
phone_msg = {
14
one = "Vous avez une noveau message.",
15
other = "Vous avez %{count} noveaux messages."
16
}
17
},
18
19
}
Copied!

Specs

This project uses busted for its specs. If you want to run the specs, you will have to install it first. Then just execute the following from the root inspect folder:
1
busted
Copied!
Last modified 1mo ago