Translation Functions
cLib.L / cLib.Lang.Get
Get a translated phrase.
local text = cLib.L(key, ...)
local text = cLib.Lang.Get(key, ...)Simple Translation
local welcome = cLib.L("welcome")
local goodbye = cLib.L("goodbye")With Named Parameters
Use {name} placeholders:
cLib.Lang.AddPhrases("en", {
["items_count"] = "You have {count} items",
["player_money"] = "{player} has ${amount}"
})
local text = cLib.L("items_count", {count = 5})
-- "You have 5 items"
local text = cLib.L("player_money", {player = "John", amount = 1000})
-- "John has $1000"With Positional Parameters
Use %s, %d, etc.:
cLib.Lang.AddPhrases("en", {
["player_joined"] = "%s joined the game",
["purchase_made"] = "%s bought %d items for $%d"
})
local text = cLib.L("player_joined", playerName)
-- "John joined the game"
local text = cLib.L("purchase_made", playerName, 3, 500)
-- "John bought 3 items for $500"Plural Forms
-- Simple plural
local text = cLib.Lang.Plural(count, "item", "items")
-- count=1: "item", count=5: "items"
-- Plural with number
local text = cLib.Lang.PluralN(count, "item", "items")
-- count=1: "1 item", count=5: "5 items"
-- Complex plural (for Slavic languages, etc.)
local text = cLib.Lang.PluralForm(count, {
"предмет", -- 1
"предмета", -- 2-4
"предметов" -- 5+
})Register Multiple Languages
-- Register phrase for multiple languages at once
cLib.Lang.RegisterPhrases("shop.title", {
en = "Item Shop",
es = "Tienda de Artículos",
fr = "Boutique d'Articles",
de = "Artikelshop"
})Check Phrase Exists
if cLib.Lang.HasPhrase("custom.phrase") then
local text = cLib.L("custom.phrase")
else
local text = "Default text"
endComplete Addon Example
-- sh_lang.lua
hook.Add("cLib.OnLoaded", "MyAddon.Lang", function()
-- English
cLib.Lang.AddPhrases("en", {
["myaddon.title"] = "My Addon",
["myaddon.welcome"] = "Welcome, {player}!",
["myaddon.balance"] = "Your balance: ${amount}",
["myaddon.buy"] = "Buy",
["myaddon.sell"] = "Sell",
["myaddon.confirm_purchase"] = "Buy {item} for ${price}?"
})
-- Spanish
cLib.Lang.AddPhrases("es", {
["myaddon.title"] = "Mi Addon",
["myaddon.welcome"] = "¡Bienvenido, {player}!",
["myaddon.balance"] = "Tu saldo: ${amount}",
["myaddon.buy"] = "Comprar",
["myaddon.sell"] = "Vender",
["myaddon.confirm_purchase"] = "¿Comprar {item} por ${price}?"
})
end)
-- cl_menu.lua
function MyAddon.OpenShop()
local frame = vgui.Create("cLib.DFrame")
frame:SetTitle(cLib.L("myaddon.title"))
local welcome = vgui.Create("DLabel", frame)
welcome:SetText(cLib.L("myaddon.welcome", {player = LocalPlayer():Nick()}))
local buyBtn = vgui.Create("cLib.DButton", frame)
buyBtn:SetText(cLib.L("myaddon.buy"))
end