Flux.Lang class
Part of Flow
Localization: translates phrases into the language of the player. The phrases come from the
YAML files in the languages folders of Flux, the schema and the plugins, where they are
nested under the language code; the server reads the files and sends the phrases to the
clients. A phrase is referred to by its path with dots, such as 'ui.hud.bar_text.respawn',
and translated with the global t function, which also fills in the {placeholders} of the
phrase. Text that is not a known phrase is returned as it is, so t can be given text that
may or may not be a phrase. A phrase that the language has no translation for is taken
from English.
A phrase that depends on a number can have plural forms: instead of a text it is a table
of texts under the keys '1', '2' and '5', and t picks the one that fits the number among
its arguments. English only uses '1' (one thing) and '2' (several); Russian uses all three:
ru:
time:
hours:
"1": час
"2": часа
"5": часов
Which form a number takes is decided by the plural rule of the language. Flux has the rules
of English (also used for languages without a rule of their own), French and Russian;
Flux.Lang:set_plural_rule adds others.
On the client the current language follows the gmod_language setting of the game, unless
the player has picked another one: Flux.Lang:set_language overrides it and remembers the
choice in the fl_language console variable. Flux.Lang:get_languages and
Flux.Lang:get_language_name list the languages to pick from. Either way the language is
reported to the server, where Flux.Lang:get_player_lang returns it for a player. On the
server t translates to English unless it is given a language, so text that depends on the
language of the reader should be built on the client: Flux.Lang:duration does that for the
durations in notifications. Phrases can also be added from code with Flux.Lang:add, and a
language can define a get_case function for its grammar, which Flux.Lang:get_case uses.
Summary
Methods
t translates to when it is not given one.Flux.Lang:set_language or else the language of their game.gmod_language setting of the game otherwise.gmod_language setting of the game.Methods
Adds a phrase, or a table of phrases that is merged into the existing ones recursively.
-- Makes t'my_plugin.greeting' available in English.
Flux.Lang:add('en', {
my_plugin = {
greeting = 'Hello, {name}!'
}
})
Parameters
- index String language code, or the key of the phrase inside the reference table
- value String/Map phrase or a nested table of phrases
- reference Map optional, defaults to
niltable to add to, all stored phrases by default
Returns all of the stored phrases.
Returns
- Map nested tables of phrases by language code
Translates a phrase and puts it in a grammatical case using the rules of the current language of the client. Clientside only.
Parameters
- phrase String phrase ID
- case String grammatical case
Returns
- String
See also
Makes a phrase argument that stands for a duration. t replaces it with the same text
that Flux.Lang:nice_time returns, in the language the phrase is translated to. Use it
for notifications, which are translated on the client of each recipient, so that everyone
reads the duration in their own language.
-- 'You have been muted for about 2 hours.' or 'Вам был отключен ООС чат на 2 часа.'
target:notify('notification.muted', { time = Flux.Lang:duration(7200) })
Parameters
- seconds Number amount of seconds
Returns
- Map value to pass as a phrase argument
See also
Translates a phrase and puts it in a grammatical case using the rules of the specified language. Languages define the rules with a get_case function in their language table.
Parameters
- language String language code
- phrase String phrase ID
- case String grammatical case
Returns
- String
Returns the language that t translates to when it is not given one.
Returns
- String language code; always 'en' on the server
Returns the name of a language in that language, such as 'English' or 'Русский'. It is
the language.name phrase of the language.
Parameters
- lang String language code
Returns
- String the name, or the language code if the language does not have one
Returns the language the local player has picked instead of the language of the game. Clientside only.
Returns
- String language code, nil if the player follows the language of the game
Returns the languages that have phrases, for a language picker.
Returns
- List<String> language codes in alphabetical order
Returns the language of a player: the one their client has reported, which is the
language they picked with Flux.Lang:set_language or else the language of their game.
Parameters
- target Player
Returns
- String language code, 'en' if the player is not valid or has not sent it yet
Translates a phrase to the specified language, in the plural form that fits the amount. A phrase without plural forms is translated as it is.
Parameters
- language String language code
- phrase String phrase ID
- count Number amount of things, which also replaces {count} in the phrase
Returns
- String
Returns the key of the plural form that an amount takes in a language: '1', '2' or '5', named after the smallest amount that takes the form in Russian. Languages without a plural rule use the rule of English, which only knows '1' and '2'.
Parameters
- language String language code
- count Number amount of things
Returns
- String key of the plural form
See also
Returns the language the client should use: the one the player has picked, if it has
phrases, and the gmod_language setting of the game otherwise. Clientside only.
Returns
- String language code
Converts an amount of seconds into a human readable duration, such as 'about 2 hours'. A duration of less than 15 seconds is given in seconds too, and never as less than '1 second', so that the result can always be put after a word like 'for' or 'in'.
Parameters
- time Number/String amount of seconds
- lang String optional, defaults to
nillanguage code, the current language by default
Returns
- String
Translates a phrase to the current language of the client, in the plural form that fits the amount. Clientside only.
Parameters
- phrase String phrase ID
- count Number amount of things
Returns
- String
See also
Sets the language of the local player, instead of following the gmod_language
setting of the game. The choice is kept in the fl_language console variable, which
the game saves, takes effect right away and is reported to the server, so that
Flux.Lang:get_player_lang returns it there. Clientside only.
Flux.Lang:set_language('ru')
-- Back to the language of the game.
Flux.Lang:set_language(nil)
Parameters
- lang String optional, defaults to
nillanguage code, one of Flux.Lang#get_languages; nil or an empty string to follow the language of the game again
Returns
- Boolean false if there are no phrases for the language, in which case nothing changes
Sets the plural rule of a language: the function that decides which plural form of a phrase an amount takes.
-- Polish has the same three forms as Russian, with a different rule for the first one.
Flux.Lang:set_plural_rule('pl', function(count)
if count == 1 then return '1' end
local last_digit, last_two = count % 10, count % 100
if last_digit >= 2 and last_digit <= 4 and (last_two < 12 or last_two > 14) then
return '2'
end
return '5'
end)
Parameters
- language String language code
- rule Function called with the amount; returns the key of the form to use: '1', '2' or '5'