tibia-client/modules/client_locales/locales.lua

104 lines
2.8 KiB
Lua
Raw Normal View History

2012-04-26 04:57:56 +02:00
Locales = { }
-- private variables
local defaultLocaleName = 'en-us'
local installedLocales
local currentLocale
2012-04-27 00:28:31 +02:00
local localeComboBox
-- private functions
local function onLocaleComboBoxOptionChange(self, optionText, optionData)
Locales.setLocale(optionData)
Settings.set('locale', optionData)
reloadModules()
end
2012-04-26 04:57:56 +02:00
-- public functions
function Locales.init()
installedLocales = {}
2012-04-26 18:45:25 +02:00
Locales.installLocales('locales')
2012-04-26 04:57:56 +02:00
local userLocaleName = Settings.get('locale')
if userLocaleName then
print('Using configurated locale: ' .. userLocaleName)
Locales.setLocale(userLocaleName)
else
print('Using default locale: ' .. defaultLocaleName)
Locales.setLocale(defaultLocaleName)
2012-04-26 04:57:56 +02:00
Settings.set('locale', defaultLocaleName)
end
2012-04-27 00:28:31 +02:00
addEvent( function()
localeComboBox = createWidget('ComboBox', rootWidget:recursiveGetChildById('rightButtonsPanel'))
for key,value in pairs(installedLocales) do
localeComboBox:addOption(value.languageName, value.name)
end
localeComboBox:setCurrentOption(currentLocale.languageName)
localeComboBox.onOptionChange = onLocaleComboBoxOptionChange
end, false)
2012-04-26 04:57:56 +02:00
end
function Locales.terminate()
installedLocales = nil
currentLocale = nil
2012-04-27 00:28:31 +02:00
localeComboBox = nil
2012-04-26 04:57:56 +02:00
end
function Locales.installLocale(locale)
if not locale or not locale.name then
error('Unable to install locale.')
2012-04-26 04:57:56 +02:00
end
2012-04-26 18:45:25 +02:00
local installedLocale = installedLocales[locale.name]
if installedLocale then
-- combine translations replacing with new if already exists
for word,translation in pairs(locale.translation) do
installedLocale.translation[word] = translation
end
else
installedLocales[locale.name] = locale
-- update combobox
end
end
function Locales.installLocales(directory)
dofiles(directory)
2012-04-26 04:57:56 +02:00
end
function Locales.setLocale(name)
local locale = installedLocales[name]
if not locale then
error("Locale " .. name .. ' does not exist.')
2012-04-26 04:57:56 +02:00
end
currentLocale = locale
2012-04-26 04:57:56 +02:00
end
function tr(text, ...)
if currentLocale then
if tonumber(text) then
2012-04-26 18:45:25 +02:00
-- todo: use locale information to calculate this. also detect floating numbers
local out = ''
local number = tostring(text):reverse()
for i=1,#number do
out = out .. number:sub(i, i)
if i % 3 == 0 and i ~= #number then
out = out .. ','
end
end
return out:reverse()
2012-04-26 04:57:56 +02:00
elseif tostring(text) then
local translation = currentLocale.translation[text]
if not translation then
if currentLocale.name ~= defaultLocaleName then
warning('Unable to translate: \"' .. text .. '\"')
end
translation = text
2012-04-26 04:57:56 +02:00
end
return string.format(translation, ...)
2012-04-26 04:57:56 +02:00
end
end
return text
end