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

Adds a phrase, or a table of phrases that is merged into the existing ones recursively.
all()shared
Returns all of the stored phrases.
Translates a phrase and puts it in a grammatical case using the rules of the current language of the client.
Makes a phrase argument that stands for a duration.
Translates a phrase and puts it in a grammatical case using the rules of the specified language.
Returns the language that t translates to when it is not given one.
Returns the name of a language in that language, such as 'English' or 'Русский'.
Returns the language the local player has picked instead of the language of the game.
Returns the languages that have phrases, for a language picker.
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.
Translates a phrase to the specified language, in the plural form that fits the amount.
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.
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.
Converts an amount of seconds into a human readable duration, such as 'about 2 hours'.
Translates a phrase to the current language of the client, in the plural form that fits the amount.
Sets the language of the local player, instead of following the gmod_language setting of the game.
Sets the plural rule of a language: the function that decides which plural form of a phrase an amount takes.

Methods

Flux.Lang:add(index, value, reference)

shared packages/flow/lib/sh_lang.lua:247

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 nil table to add to, all stored phrases by default

Flux.Lang:all()

shared packages/flow/lib/sh_lang.lua:231

Returns all of the stored phrases.

Returns

  • Map nested tables of phrases by language code

Flux.Lang:case(phrase, case)

client packages/flow/lib/sh_lang.lua:496

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

Flux.Lang:duration(seconds)

shared packages/flow/lib/sh_lang.lua:316

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

Flux.Lang:get_case(language, phrase, case)

shared packages/flow/lib/sh_lang.lua:348

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

Flux.Lang:get_language()

shared packages/flow/lib/sh_lang.lua:371

Returns the language that t translates to when it is not given one.

Returns

  • String language code; always 'en' on the server

Flux.Lang:get_language_name(lang)

shared packages/flow/lib/sh_lang.lua:395

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

Flux.Lang:get_language_override()

client packages/flow/lib/sh_lang.lua:411

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

Flux.Lang:get_languages()

shared packages/flow/lib/sh_lang.lua:377

Returns the languages that have phrases, for a language picker.

Returns

  • List<String> language codes in alphabetical order

Flux.Lang:get_player_lang(target)

shared packages/flow/lib/sh_lang.lua:363

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

Flux.Lang:get_plural(language, phrase, count)

shared packages/flow/lib/sh_lang.lua:301

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

Flux.Lang:get_plural_form(language, count)

shared packages/flow/lib/sh_lang.lua:291

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

Flux.Lang:get_preferred_language()

client packages/flow/lib/sh_lang.lua:422

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

Flux.Lang:nice_time(time, lang)

shared packages/flow/lib/sh_lang.lua:326

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 nil language code, the current language by default

Returns

  • String

Flux.Lang:pluralize(phrase, count)

client packages/flow/lib/sh_lang.lua:486

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

Flux.Lang:set_language(lang)

client packages/flow/lib/sh_lang.lua:468

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 nil language 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

Flux.Lang:set_plural_rule(language, rule)

shared packages/flow/lib/sh_lang.lua:280

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'

Defined in