A pure lua library for management of strings geared towards localization purposes.
- Hot swap in real time of strings
- Simple API
- Lightweight
- Protected mode
- Configurable
- 📡 Get a copy of language.lua from the Official Repository or From Luarocks
- 💾 Copy
language.lua
where you like to use it, or just on the root directory of the project - ⚙ Add it to your project like this:
local language = require("path/to/language")
- 📃 Pass a path or table containing the data
- Method 1: The strings are defined internally
local english = { welcome = "Welcome back!", menu = { title = "Main menu:", option1 = { name = "Start", description = "Description of start" } }, debugmenu = { title = "Hello world" } } local spanish = { welcome = "Bienvenido de regreso!", menu = { title = "Menú principal:", option1 = { name = "Iniciar", description = "Descripcion de iniciar" } } } language.loadLanguage("english", english) language.loadLanguage("spanish", spanish) language.setLanguage("english") language.setBackupLanguage("english")
- Method 2: The string are on an external file
--This will look for english.lua and spanish.lua inside the folder l10n --Those files must return a table of strings to work with language.loadLanguage("english", "l10n.english") language.loadLanguage("spanish", "l10n.spanish")
- 🖨️ Every time you want tell something to the user, do it by the
getString
function.print(language.getString("welcome")) print(language.getString("menu.title")) print(language.getString("menu.option1.name")) print(language.getString("menu.option1.description")) language.setLanguage("spanish") print(language.getString("welcome")) print(language.getString("menu.title")) print(language.getString("menu.option1.name")) print(language.getString("menu.option1.description")) --English: -->Welcome back! -->Main menu: -->Start -->Description of start --Spanish: -->Bienvenido de regreso! -->Menú principal -->Iniciar -->Descripcion de iniciar
- 💎 Profit. For more in depth documentation take a look down below.
-
language.setProtectedMode(bool)
Enabled by default, Make protected calls for catching runtime errors.- Parameters:
- *Boolean
bool
Enable true, Disable false.
- *Boolean
- Returns:
- nothing
- Parameters:
-
language.setIsoleatedMode(bool)
Enabled by default, when loading a string from an external lua file isolate said lua file for accessing the environment, can be disabled to do wacky stuff for dynamic and or procedural string generation.- Parameters:
- *Boolean
bool
Enable true, Disable false.
- *Boolean
- Returns:
- nothing
- Parameters:
-
language.setSanitizeOutput(bool)
Enabled by default, when calling getString filter out any type that is not in thesanitizeFilter
whitelist.- Parameters:
- *Boolean
bool
Enable true, Disable false.
- *Boolean
- Returns:
- nothing
- Parameters:
-
language.setSanitizeOutputFilters(filterList)
Set what typesgetString
is allowed to output whensetSanitizeOutput
is enabled, by default they arestring
,number
andtable
- Parameters:
- Table
filterList
A table containing strings that represent a type likestring
,function
,number
,nil
,table
oruserdata
when given the literallnil
it will assume default config wich is{"string", "table", "number"}
.
- Table
- Returns:
- nothing
- Parameters:
-
language.setLanguage(name)
Sets the current language to use- Parameters:
- *String
name
The current language chosen.
- *String
- Returns:
- nothing
- Parameters:
-
language.setPrintErrors(bool)
Enabled by default, When in protected mode, print error that are being caught.- Parameters:
- *Boolean
bool
Enable true, Disable false.
- *Boolean
- Returns:
- nothing
- Parameters:
-
language.loadLanguage(name, data)
Load a table containing the strings into memory.- Parameters:
- *String
name
What name will have the key where the data will be saved. - *String, Table
data
- *String
data
Path to the file to look for loading the table. - *Table
data
Table containing the strings already loaded.
- *String
- *String
- Returns:
- nothing
- Parameters:
-
language.getString(stringKey, languageKey)
- Parameters:
- *String
stringKey
String identification key, the key can be nested in other keys using the.
separator eg:menu.main.title
- String
languageKey
when set this will override the current language only for this call to the specified one, if not found it will just ignore it.
- *String
- Returns:
- *String
string
when used correctly this will always return a String, it may return a table when thekey
points to said table, but also may point to a function or undefined types ifsetSanitizeOutput
is set tofalse
.
- *String
- Parameters:
-
lang.getLocalizationData()
- Parameters:
- nothing
- Returns:
- *Table
localization
This contains the strings loaded, although not encouraged this can be manipulated directly.
- *Table
- Parameters:
-
lang.getActualLanguageData()
- Parameters:
- nothing
- Returns:
- *Table
actualLang
Returns what is effectivelylocalization[systemLanguage]
.
- *Table
- Parameters:
-
lang.getBackupLanguageData()
- Parameters:
- nothing
- Returns:
- *Table
backupLang
This is a fallback table where akey
will be searched whenactualLang
has no defined suchkey
. return backupLang end
- *Table
- Parameters:
-
lang.getLanguage()
Get the current language that this library tries to look for when asked of a key ongetString
.- Parameters:
- nothing
- Returns:
- *String
systemLanguage
What is the language that is set.
- *String
- Parameters: