This guide will teach you how to build a clean, scalable localization system using a centralized string table and a dynamic helper function.
Step 1: Define Your Language Data Table
Instead of hardcoding text everywhere in your script, create a central configuration table. TheoTown uses a specific inline format for translations: English Text[language_code]Translated Text.
Here is how you set up your multi-language string database:
Code: Select all Reset
local strings = { title = "Radar Settings[vi]Cài đặt Radar[zh]雷达设置[in]Pengaturan Radar", -- Use {1} and {2} as placeholders for dynamic values desc = "Current scan range is {1} tiles.\nDo you want to change it to {2} tiles?[vi]Khoảng cách quét hiện tại là {1} ô.\nBạn có muốn đổi thành {2} ô không?[zh]当前扫描范围为 {1} 格。\n你想把它改成 {2} 格吗?[in]Jangkauan pindai saat ini adalah {1} ubin.\nApakah Anda ingin mengubahnya menjadi {2} ubin?", btn_cancel = "Cancel[vi]Hủy[zh]取消[in]Batal", btn_change = "Change[vi]Thay đổi[zh]更改[in]Ubah", toast_success = "Scan range changed to: {1} tiles[vi]Đã chuyển khoảng cách quét: {1} ô[zh]扫描范围已更改为:{1} 格[in]Jangkauan pindai diubah menjadi: {1} ubin", toast_error = "Unknown error: Cannot load station![vi]Lỗi không xác định: Không thể nạp trạm![zh]未知错误:无法加载雷达站![in]Kesalahan tidak diketahui: Tidak dapat memuat stasiun!" }
Lua editor
Default Language: The text before any bracket (e.g., Cancel) acts as the default English text.
Language Tags: [vi] is Vietnamese, [zh] is Chinese, [in] is Indonesian, etc.
Placeholders: {1} and {2} are temporary markers that we will replace later with actual dynamic numbers or strings (like the number of tiles).
Step 2: Create the Translation Engine Function
To fetch these strings and automatically inject dynamic values, we use a custom utility function. This function performs two vital tasks: replacing placeholders and resolving the game language.
Code: Select all Reset
local function yinx_getText(key, val1, val2) -- Fallback to the key itself if the key is missing from the table local text = strings[key] or key -- Automatically replace {1} with val1 and {2} with val2 if provided if val1 then text = string.gsub(text, "{1}", tostring(val1)) end if val2 then text = string.gsub(text, "{2}", tostring(val2)) end -- Let TheoTown pick the correct language based on player settings return TheoTown.translateInline(text) end
Lua editor
1 string.gsub(text, "{1}", tostring(val1)): This searches your text for the {1} tag and replaces it with your variable (val1).
2 TheoTown.translateInline(text): This is the native TheoTown API. It detects the player's active game language and strips out all other translations, leaving only the correct one.
Step 3: Implement it in Your Code
Now that your system is ready, displaying localized text anywhere in your plugin becomes incredibly simple and clean.
Example A: Displaying a Simple Button
For basic text without placeholders, just pass the key name:
local buttonText = yinx_getText("btn_cancel")
-- Output (for a Vietnamese player): "Hủy"
Example B: Displaying Dynamic Toasts or Dialogs
For text requiring live information (like ranges or coordinates), pass your values as additional arguments:
local currentRange = 10
local newRange = 25
-- Displaying a confirmation dialog description
local dialogMessage = yinx_getText("desc", currentRange, newRange)
-- Output (for an English player): "Current scan range is 10 tiles. Do you want to change it to 25 tiles?"
-- Displaying a success toast notice
TheoTown.toast(yinx_getText("toast_success", newRange))
-- Output (for a Chinese player): "扫描范围已更改为:25 格"
Provide a fallback by making sure the first part of your string is always in English, ensuring that if a language isn't supported, the game still displays a readable default.