Languages Library
Internationalization (i18n) and localization system for the Lilia framework.
Overview
The languages library provides comprehensive internationalization (i18n) functionality for the Lilia framework. It handles loading, storing, and retrieving localized strings from language files, supporting multiple languages with fallback mechanisms. The library automatically loads language files from directories, processes them into a unified storage system, and provides string formatting with parameter substitution. It includes functions for adding custom language tables, retrieving available languages, and getting localized strings with proper error handling. The library operates on both server and client sides, ensuring consistent localization across the entire gamemode. It supports dynamic language switching and provides the global L() function for easy access to localized strings throughout the codebase.
lia.lang.loadFromDir
π Purpose
Loads language files from a specified directory and processes them into the language storage system
β° When Called
During gamemode initialization or when manually loading language files
βοΈ Parameters
| Parameter | Type | Description |
|---|---|---|
directory |
string | The directory path containing language files |
β©οΈ Returns
- None
π Realm
Shared
π‘ Example Usage
π° Low Complexity
π Medium Complexity
-- Medium: Load languages from custom module directory
local moduleDir = "lilia/gamemode/modules/mymodule/languages"
if file.Exists(moduleDir, "LUA") then
lia.lang.loadFromDir(moduleDir)
end
βοΈ High Complexity
-- High: Load languages from multiple directories with validation
local languageDirs = {
"lilia/gamemode/languages",
"lilia/gamemode/modules/custom/languages",
"addons/mycustomaddon/languages"
}
for _, dir in ipairs(languageDirs) do
if file.Exists(dir, "LUA") then
lia.lang.loadFromDir(dir)
end
end
lia.lang.addTable
π Purpose
Adds a custom language table to the language storage system
β° When Called
When manually adding language strings or when modules need to register their own translations
βοΈ Parameters
| Parameter | Type | Description |
|---|---|---|
name |
string | The language name/key |
tbl |
table | Table containing key-value pairs of translations |
β©οΈ Returns
- None
π Realm
Shared
π‘ Example Usage
π° Low Complexity
-- Simple: Add basic language strings
lia.lang.addTable("english", {
hello = "Hello",
goodbye = "Goodbye"
})
π Medium Complexity
-- Medium: Add module-specific language strings
local moduleLang = {
moduleTitle = "My Module",
moduleDescription = "This is a custom module",
moduleError = "An error occurred: %s"
}
lia.lang.addTable("english", moduleLang)
βοΈ High Complexity
-- High: Add multiple language tables with validation
local languages = {
english = { title = "Title", desc = "Description" },
spanish = { title = "TΓtulo", desc = "DescripciΓ³n" },
french = { title = "Titre", desc = "Description" }
}
for lang, strings in pairs(languages) do
if istable(strings) then
lia.lang.addTable(lang, strings)
end
end
lia.lang.getLanguages
π Purpose
Retrieves a sorted list of all available language names
β° When Called
When building language selection menus or when checking available languages
β©οΈ Returns
- table - Sorted array of language names with proper capitalization
π Realm
Shared
π‘ Example Usage
π° Low Complexity
-- Simple: Get list of available languages
local languages = lia.lang.getLanguages()
print("Available languages:", table.concat(languages, ", "))
π Medium Complexity
-- Medium: Create language selection menu
local languages = lia.lang.getLanguages()
local menu = vgui.Create("DFrame")
local combo = vgui.Create("DComboBox", menu)
for _, lang in ipairs(languages) do
combo:AddChoice(lang)
end
βοΈ High Complexity
-- High: Validate language selection with fallback
local function setLanguage(langName)
local languages = lia.lang.getLanguages()
local found = false
for _, lang in ipairs(languages) do
if lang:lower() == langName:lower() then
found = true
break
end
end
if found then
lia.config.set("Language", langName:lower())
else
lia.notice.add("Invalid language selected, using English", NOTIFY_ERROR)
lia.config.set("Language", "english")
end
end
lia.lang.getLocalizedString
π Purpose
Retrieves a localized string with parameter substitution and formatting
β° When Called
When displaying text to users or when any localized string is needed
βοΈ Parameters
| Parameter | Type | Description |
|---|---|---|
key |
string | The language key to look up |
β©οΈ Returns
- string - The localized and formatted string, or the key if not found
π Realm
Shared
π‘ Example Usage
π° Low Complexity
-- Simple: Get basic localized string
local message = lia.lang.getLocalizedString("hello")
print(message) -- Outputs: "Hello" (in current language)
π Medium Complexity
-- Medium: Get localized string with parameters
local playerName = "John"
local welcomeMsg = lia.lang.getLocalizedString("welcomePlayer", playerName)
print(welcomeMsg) -- Outputs: "Welcome, John!" (if template is "Welcome, %s!")
βοΈ High Complexity
-- High: Complex localized string with multiple parameters and error handling
local function displayItemInfo(itemName, quantity, price)
local lang = lia.config and lia.config.get("Language", "english") or "english"
local langTable = lia.lang.stored and lia.lang.stored[lang:lower()]
local template = langTable and langTable["itemInfo"] or "itemInfo"
if template then
local message = lia.lang.getLocalizedString("itemInfo", itemName, "No description available")
lia.notice.add(message, NOTIFY_GENERIC)
else
lia.notice.add("Item: " .. itemName .. " x" .. quantity .. " - $" .. price, NOTIFY_GENERIC)
end
end