2012-04-26 04:57:56 +02:00
|
|
|
Locales = { }
|
|
|
|
|
2012-07-31 01:52:06 +02:00
|
|
|
dofile 'neededtranslations.lua'
|
|
|
|
|
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
|
|
|
local localeComboBox
|
|
|
|
|
|
|
|
-- private functions
|
2012-05-16 22:09:37 +02:00
|
|
|
local function sendLocale(localeName)
|
|
|
|
local protocolGame = g_game.getProtocolGame()
|
|
|
|
if protocolGame then
|
2012-07-06 07:00:49 +02:00
|
|
|
protocolGame:sendExtendedOpcode(ExtendedLocales, localeName)
|
2012-05-16 22:09:37 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-04-27 00:28:31 +02:00
|
|
|
local function onLocaleComboBoxOptionChange(self, optionText, optionData)
|
2012-05-16 22:09:37 +02:00
|
|
|
if Locales.setLocale(optionData) then
|
2012-06-26 00:13:30 +02:00
|
|
|
g_settings.set('locale', optionData)
|
2012-05-16 22:09:37 +02:00
|
|
|
sendLocale(currentLocale.name)
|
2012-06-26 00:13:30 +02:00
|
|
|
g_modules.reloadModules()
|
2012-05-16 22:09:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hooked functions
|
|
|
|
local function onGameStart()
|
|
|
|
sendLocale(currentLocale.name)
|
|
|
|
end
|
|
|
|
|
2012-07-06 07:00:49 +02:00
|
|
|
local function onExtendedLocales(protocol, opcode, buffer)
|
2012-05-16 22:09:37 +02:00
|
|
|
local locale = installedLocales[buffer]
|
|
|
|
if locale then
|
|
|
|
localeComboBox:setCurrentOption(locale.languageName)
|
|
|
|
end
|
2012-04-27 00:28:31 +02:00
|
|
|
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
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
local userLocaleName = g_settings.get('locale', 'false')
|
2012-06-20 08:31:52 +02:00
|
|
|
if userLocaleName ~= 'false' and Locales.setLocale(userLocaleName) then
|
2012-06-20 02:15:56 +02:00
|
|
|
pdebug('Using configured locale: ' .. userLocaleName)
|
2012-04-26 21:54:16 +02:00
|
|
|
else
|
2012-06-20 02:15:56 +02:00
|
|
|
pdebug('Using default locale: ' .. defaultLocaleName)
|
2012-04-26 21:54:16 +02:00
|
|
|
Locales.setLocale(defaultLocaleName)
|
2012-06-26 00:13:30 +02:00
|
|
|
g_settings.set('locale', defaultLocaleName)
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
2012-04-27 08:52:49 +02:00
|
|
|
|
2012-04-27 00:28:31 +02:00
|
|
|
addEvent( function()
|
2012-08-06 16:39:27 +02:00
|
|
|
localeComboBox = g_ui.createWidget('ComboBoxRounded', rootWidget:recursiveGetChildById('rightButtonsPanel'))
|
2012-08-05 17:54:23 +02:00
|
|
|
localeComboBox:setFixedSize(true)
|
2012-04-27 00:28:31 +02:00
|
|
|
for key,value in pairs(installedLocales) do
|
|
|
|
localeComboBox:addOption(value.languageName, value.name)
|
|
|
|
end
|
|
|
|
localeComboBox:setCurrentOption(currentLocale.languageName)
|
|
|
|
localeComboBox.onOptionChange = onLocaleComboBoxOptionChange
|
|
|
|
end, false)
|
2012-05-16 22:09:37 +02:00
|
|
|
|
2012-07-06 07:00:49 +02:00
|
|
|
Extended.register(ExtendedLocales, onExtendedLocales)
|
2012-05-16 22:09:37 +02:00
|
|
|
connect(g_game, { onGameStart = onGameStart })
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Locales.terminate()
|
|
|
|
installedLocales = nil
|
|
|
|
currentLocale = nil
|
2012-08-23 19:45:39 +02:00
|
|
|
|
|
|
|
if localeComboBox then
|
|
|
|
localeComboBox:destroy()
|
|
|
|
localeComboBox = nil
|
|
|
|
end
|
|
|
|
|
2012-07-06 07:00:49 +02:00
|
|
|
Extended.unregister(ExtendedLocales)
|
2012-05-16 22:09:37 +02:00
|
|
|
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(Locales.neededTranslations) do
|
|
|
|
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
|
|
|
|
|
2012-04-26 04:57:56 +02:00
|
|
|
function Locales.installLocale(locale)
|
2012-04-26 21:54:16 +02:00
|
|
|
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(Locales.neededTranslations) do
|
|
|
|
if locale.translation[k] == nil then
|
|
|
|
pwarning('Translation for locale \'' .. locale.name .. '\' not found: \"' .. k.. '\"')
|
|
|
|
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
|
2012-06-20 08:31:52 +02:00
|
|
|
if localeComboBox then
|
|
|
|
localeComboBox.onOptionChange = nil
|
|
|
|
localeComboBox:addOption(locale.languageName, locale.name)
|
|
|
|
localeComboBox.onOptionChange = onLocaleComboBoxOptionChange
|
|
|
|
end
|
2012-04-26 18:45:25 +02:00
|
|
|
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]
|
2012-04-26 21:54:16 +02:00
|
|
|
if not locale then
|
2012-06-20 02:15:56 +02:00
|
|
|
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
|
2012-04-26 21:54:16 +02:00
|
|
|
currentLocale = locale
|
2012-04-27 08:52:49 +02:00
|
|
|
return true
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
|
|
|
|
2012-05-28 15:06:26 +02:00
|
|
|
-- global function used to translate texts
|
2012-04-26 04:57:56 +02:00
|
|
|
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]
|
2012-04-26 21:54:16 +02:00
|
|
|
if not translation then
|
|
|
|
if currentLocale.name ~= defaultLocaleName then
|
2012-06-20 02:15:56 +02:00
|
|
|
pwarning('Unable to translate: \"' .. text .. '\"')
|
2012-04-26 21:54:16 +02:00
|
|
|
end
|
|
|
|
translation = text
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
2012-04-26 21:54:16 +02:00
|
|
|
return string.format(translation, ...)
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return text
|
2012-04-26 21:54:16 +02:00
|
|
|
end
|