2013-01-18 23:39:11 +01:00
|
|
|
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
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function sendLocale(localeName)
|
2012-05-16 22:09:37 +02:00
|
|
|
local protocolGame = g_game.getProtocolGame()
|
|
|
|
if protocolGame then
|
2013-01-21 22:36:53 +01:00
|
|
|
protocolGame:sendExtendedOpcode(ExtendedIds.Locale, localeName)
|
2012-05-16 22:09:37 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function createWindow()
|
|
|
|
localesWindow = g_ui.displayUI('locales')
|
|
|
|
local localesPanel = localesWindow:getChildById('localesPanel')
|
2013-02-10 06:24:51 +01:00
|
|
|
local layout = localesPanel:getLayout()
|
|
|
|
local spacing = layout:getCellSpacing()
|
|
|
|
local size = layout:getCellSize()
|
2013-01-18 23:39:11 +01:00
|
|
|
|
2013-02-10 06:24:51 +01:00
|
|
|
local count = 0
|
2013-01-18 23:39:11 +01:00
|
|
|
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
|
2013-02-10 06:24:51 +01:00
|
|
|
count = count + 1
|
2013-01-18 23:39:11 +01:00
|
|
|
end
|
|
|
|
|
2013-02-10 06:24:51 +01:00
|
|
|
count = math.max(1, math.min(count, 3))
|
|
|
|
localesPanel:setWidth(size.width*count + spacing*(count-1))
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
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()
|
2012-05-16 22:09:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hooked functions
|
2013-01-18 23:39:11 +01:00
|
|
|
function onGameStart()
|
2012-05-16 22:09:37 +02:00
|
|
|
sendLocale(currentLocale.name)
|
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function onExtendedLocales(protocol, opcode, buffer)
|
2012-05-16 22:09:37 +02:00
|
|
|
local locale = installedLocales[buffer]
|
2013-01-18 23:39:11 +01:00
|
|
|
if locale and setLocale(locale.name) then
|
|
|
|
g_modules.reloadModules()
|
2012-05-16 22:09:37 +02:00
|
|
|
end
|
2012-04-27 00:28:31 +02:00
|
|
|
end
|
2012-04-26 04:57:56 +02:00
|
|
|
|
|
|
|
-- public functions
|
2013-01-18 23:39:11 +01:00
|
|
|
function init()
|
2012-04-26 04:57:56 +02:00
|
|
|
installedLocales = {}
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
installLocales('/locales')
|
2012-04-26 04:57:56 +02:00
|
|
|
|
2014-04-01 05:27:33 +02:00
|
|
|
local userLocaleName = g_settings:get('locale', 'false')
|
2013-01-18 23:39:11 +01:00
|
|
|
if userLocaleName ~= 'false' and 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
|
2013-01-18 23:39:11 +01:00
|
|
|
setLocale(defaultLocaleName)
|
2013-02-10 06:24:51 +01:00
|
|
|
connect(g_app, { onRun = createWindow })
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
2012-04-27 08:52:49 +02:00
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
ProtocolGame.registerExtendedOpcode(ExtendedIds.Locale, onExtendedLocales)
|
2012-05-16 22:09:37 +02:00
|
|
|
connect(g_game, { onGameStart = onGameStart })
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function terminate()
|
2012-04-26 04:57:56 +02:00
|
|
|
installedLocales = nil
|
|
|
|
currentLocale = nil
|
2012-08-23 19:45:39 +02:00
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
ProtocolGame.unregisterExtendedOpcode(ExtendedIds.Locale)
|
2013-02-10 06:24:51 +01:00
|
|
|
disconnect(g_app, { onRun = createWindow })
|
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]
|
2013-01-18 23:39:11 +01:00
|
|
|
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
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function 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
|
|
|
|
|
2013-02-10 06:24:51 +01:00
|
|
|
if _G.allowedLocales and not _G.allowedLocales[locale.name] then return end
|
|
|
|
|
2012-07-31 01:52:06 +02:00
|
|
|
if locale.name ~= defaultLocaleName then
|
2013-01-23 18:35:43 +01:00
|
|
|
local updatesNeeded = 0
|
2013-01-18 23:39:11 +01:00
|
|
|
for _i,k in pairs(neededTranslations) do
|
2012-07-31 01:52:06 +02:00
|
|
|
if locale.translation[k] == nil then
|
2013-01-23 18:35:43 +01:00
|
|
|
updatesNeeded = updatesNeeded + 1
|
2012-07-31 01:52:06 +02:00
|
|
|
end
|
|
|
|
end
|
2013-01-23 18:35:43 +01:00
|
|
|
|
|
|
|
if updatesNeeded > 0 then
|
2013-02-28 22:39:27 +01:00
|
|
|
pdebug('Locale \'' .. locale.name .. '\' is missing ' .. updatesNeeded .. ' translations.')
|
2013-01-23 18:35:43 +01:00
|
|
|
end
|
2012-07-31 01:52:06 +02:00
|
|
|
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
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function installLocales(directory)
|
2012-04-26 18:45:25 +02:00
|
|
|
dofiles(directory)
|
2012-04-26 04:57:56 +02:00
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function setLocale(name)
|
2012-04-26 04:57:56 +02:00
|
|
|
local locale = installedLocales[name]
|
2013-01-24 17:01:28 +01:00
|
|
|
if locale == currentLocale then return end
|
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
|
2013-02-19 02:18:10 +01:00
|
|
|
if currentLocale then
|
|
|
|
sendLocale(locale.name)
|
|
|
|
end
|
2012-04-26 21:54:16 +02:00
|
|
|
currentLocale = locale
|
2014-04-01 05:27:33 +02:00
|
|
|
g_settings:set('locale', name)
|
2013-01-18 23:39:11 +01:00
|
|
|
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
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function getInstalledLocales()
|
|
|
|
return installedLocales
|
|
|
|
end
|
|
|
|
|
|
|
|
function getCurrentLocale()
|
|
|
|
return currentLocale
|
2012-08-27 09:47:08 +02:00
|
|
|
end
|
|
|
|
|
2012-05-28 15:06:26 +02:00
|
|
|
-- global function used to translate texts
|
2013-01-18 23:39:11 +01:00
|
|
|
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]
|
2012-04-26 21:54:16 +02:00
|
|
|
if not translation then
|
2013-01-18 23:39:11 +01:00
|
|
|
if translation == nil then
|
|
|
|
if currentLocale.name ~= defaultLocaleName then
|
2013-02-28 22:39:27 +01:00
|
|
|
pdebug('Unable to translate: \"' .. text .. '\"')
|
2013-01-18 23:39:11 +01:00
|
|
|
end
|
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
|