tibia-client/modules/client_locales/locales.lua

178 lines
4.5 KiB
Lua
Raw Normal View History

dofile 'neededtranslations'
2012-07-31 01:52:06 +02:00
2012-04-26 04:57:56 +02:00
-- private variables
2012-04-27 08:52:49 +02:00
local defaultLocaleName = 'en'
2012-04-26 04:57:56 +02:00
local installedLocales
local currentLocale
2012-04-27 00:28:31 +02:00
LocaleExtendedId = 1
function sendLocale(localeName)
local protocolGame = g_game.getProtocolGame()
if protocolGame then
protocolGame:sendExtendedOpcode(LocaleExtendedId, localeName)
return true
end
return false
end
function createWindow()
localesWindow = g_ui.displayUI('locales')
local localesPanel = localesWindow:getChildById('localesPanel')
for name,locale in pairs(installedLocales) do
local widget = g_ui.createWidget('LocalesButton', localesPanel)
widget:setImageSource('/images/flags/' .. name .. '')
widget:setText(locale.languageName)
widget.onClick = function() selectFirstLocale(name) end
end
addEvent(function() addEvent(function() localesWindow:raise() localesWindow:focus() end) end)
end
function selectFirstLocale(name)
if localesWindow then
localesWindow:destroy()
localesWindow = nil
end
if setLocale(name) then
2012-06-26 00:13:30 +02:00
g_modules.reloadModules()
end
end
-- hooked functions
function onGameStart()
sendLocale(currentLocale.name)
end
function onExtendedLocales(protocol, opcode, buffer)
local locale = installedLocales[buffer]
if locale and setLocale(locale.name) then
g_modules.reloadModules()
end
2012-04-27 00:28:31 +02:00
end
2012-04-26 04:57:56 +02:00
-- public functions
function init()
2012-04-26 04:57:56 +02:00
installedLocales = {}
installLocales('/locales')
2012-04-26 04:57:56 +02:00
2012-06-26 00:13:30 +02:00
local userLocaleName = g_settings.get('locale', 'false')
if userLocaleName ~= 'false' and setLocale(userLocaleName) then
pdebug('Using configured locale: ' .. userLocaleName)
else
setLocale(defaultLocaleName)
connect(g_app, {onRun = createWindow})
2012-04-26 04:57:56 +02:00
end
2012-04-27 08:52:49 +02:00
ProtocolGame.registerExtendedOpcode(LocaleExtendedId, onExtendedLocales)
connect(g_game, { onGameStart = onGameStart })
2012-04-26 04:57:56 +02:00
end
function terminate()
2012-04-26 04:57:56 +02:00
installedLocales = nil
currentLocale = nil
ProtocolGame.unregisterExtendedOpcode(LocaleExtendedId)
disconnect(g_game, { onGameStart = onGameStart })
2012-04-26 04:57:56 +02:00
end
2012-07-31 01:52:06 +02:00
function generateNewTranslationTable(localename)
local locale = installedLocales[localename]
for _i,k in pairs(neededTranslations) do
2012-07-31 01:52:06 +02:00
local trans = locale.translation[k]
k = k:gsub('\n','\\n')
k = k:gsub('\t','\\t')
k = k:gsub('\"','\\\"')
if trans then
trans = trans:gsub('\n','\\n')
trans = trans:gsub('\t','\\t')
trans = trans:gsub('\"','\\\"')
end
if not trans then
print(' ["' .. k .. '"]' .. ' = false,')
else
print(' ["' .. k .. '"]' .. ' = "' .. trans .. '",')
end
end
end
function 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-07-31 01:52:06 +02:00
if locale.name ~= defaultLocaleName then
for _i,k in pairs(neededTranslations) do
2012-07-31 01:52:06 +02:00
if locale.translation[k] == nil then
local ktext = string.gsub(k, "\n", "\\n")
pwarning('Translation for locale \'' .. locale.name .. '\' not found: \"' .. ktext .. '\"')
2012-07-31 01:52:06 +02:00
end
end
end
2012-04-26 18:45:25 +02:00
local installedLocale = installedLocales[locale.name]
if installedLocale then
for word,translation in pairs(locale.translation) do
installedLocale.translation[word] = translation
end
else
installedLocales[locale.name] = locale
end
end
function installLocales(directory)
2012-04-26 18:45:25 +02:00
dofiles(directory)
2012-04-26 04:57:56 +02:00
end
function setLocale(name)
2012-04-26 04:57:56 +02:00
local locale = installedLocales[name]
if not locale then
pwarning("Locale " .. name .. ' does not exist.')
2012-04-27 08:52:49 +02:00
return false
2012-04-26 04:57:56 +02:00
end
currentLocale = locale
g_settings.set('locale', name)
if onLocaleChanged then onLocaleChanged(name) end
2012-04-27 08:52:49 +02:00
return true
2012-04-26 04:57:56 +02:00
end
function getInstalledLocales()
return installedLocales
end
function getCurrentLocale()
return currentLocale
2012-08-27 09:47:08 +02:00
end
-- global function used to translate texts
function _G.tr(text, ...)
2012-04-26 04:57:56 +02:00
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 translation == nil then
if currentLocale.name ~= defaultLocaleName then
pwarning('Unable to translate: \"' .. text .. '\"')
end
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