1dofile 'neededtranslations'
2
3-- private variables
4local defaultLocaleName = 'pt'
5local installedLocales
6local currentLocale
7local missingTranslations = {}
8
9function createWindow()
10 localesWindow = g_ui.displayUI('locales')
11 local localesPanel = localesWindow:getChildById('localesPanel')
12 local layout = localesPanel:getLayout()
13 local spacing = layout:getCellSpacing()
14 local size = layout:getCellSize()
15
16 local count = 0
17 for name,locale in pairs(installedLocales) do
18 local widget = g_ui.createWidget('LocalesButton', localesPanel)
19 widget:setImageSource('/images/flags/' .. name .. '')
20 widget:setText(locale.languageName)
21 widget.onClick = function() selectFirstLocale(name) end
22 count = count + 1
23 end
24
25 count = math.max(1, math.min(count, 3))
26 localesPanel:setWidth(size.width*count + spacing*(count-1))
27
28 addEvent(function() addEvent(function() localesWindow:raise() localesWindow:focus() end) end)
29end
30
31function selectFirstLocale(name)
32 if localesWindow then
33 localesWindow:destroy()
34 localesWindow = nil
35 end
36 if setLocale(name) then
37 g_modules.reloadModules()
38 end
39 g_settings.save()
40end
41
42-- public functions
43function init()
44 installedLocales = {}
45
46 installLocales('/locales')
47
48 local userLocaleName = g_settings.get('locale', 'false')
49 if userLocaleName ~= 'false' and setLocale(userLocaleName) then
50 pdebug('Using configured locale: ' .. userLocaleName)
51 else
52 setLocale(defaultLocaleName)
53 --connect(g_app, { onRun = createWindow })
54 end
55end
56
57function terminate()
58 installedLocales = nil
59 currentLocale = nil
60
61 --disconnect(g_app, { onRun = createWindow })
62end
63
64function generateNewTranslationTable(localename)
65 local locale = installedLocales[localename]
66 for _i,k in pairs(neededTranslations) do
67 local trans = locale.translation[k]
68 k = k:gsub('\n','\\n')
69 k = k:gsub('\t','\\t')
70 k = k:gsub('\"','\\\"')
71 if trans then
72 trans = trans:gsub('\n','\\n')
73 trans = trans:gsub('\t','\\t')
74 trans = trans:gsub('\"','\\\"')
75 end
76 if not trans then
77 print(' ["' .. k .. '"]' .. ' = false,')
78 else
79 print(' ["' .. k .. '"]' .. ' = "' .. trans .. '",')
80 end
81 end
82end
83
84function installLocale(locale)
85 if not locale or not locale.name then
86 error('Unable to install locale.')
87 end
88
89 if _G.allowedLocales and not _G.allowedLocales[locale.name] then return end
90
91 if locale.name ~= defaultLocaleName then
92 local updatesNamesMissing = {}
93 for _,k in pairs(neededTranslations) do
94 if locale.translation[k] == nil then
95 updatesNamesMissing[#updatesNamesMissing + 1] = k
96 end
97 end
98
99 if #updatesNamesMissing > 0 then
100 pdebug('Locale \'' .. locale.name .. '\' is missing ' .. #updatesNamesMissing .. ' translations.')
101 for _,name in pairs(updatesNamesMissing) do
102 pdebug('["' .. name ..'"] = \"\",')
103 end
104 end
105 end
106
107 local installedLocale = installedLocales[locale.name]
108 if installedLocale then
109 for word,translation in pairs(locale.translation) do
110 installedLocale.translation[word] = translation
111 end
112 else
113 installedLocales[locale.name] = locale
114 end
115end
116
117function installLocales(directory)
118 dofiles(directory)
119end
120
121function setLocale(name)
122 local locale = installedLocales[name]
123 if locale == currentLocale then return end
124 if not locale then
125 pwarning("Locale " .. name .. ' does not exist.')
126 return false
127 end
128 currentLocale = locale
129 g_settings.set('locale', name)
130 if onLocaleChanged then onLocaleChanged(name) end
131 return true
132end
133
134function getInstalledLocales()
135 return installedLocales
136end
137
138function getCurrentLocale()
139 return currentLocale
140end
141
142-- global function used to translate texts
143function _G.tr(text, ...)
144 if currentLocale then
145 if tonumber(text) and currentLocale.formatNumbers then
146 local number = tostring(text):split('.')
147 local out = ''
148 local reverseNumber = number[1]:reverse()
149 for i=1,#reverseNumber do
150 out = out .. reverseNumber:sub(i, i)
151 if i % 3 == 0 and i ~= #number then
152 out = out .. currentLocale.thousandsSeperator
153 end
154 end
155
156 if number[2] then
157 out = number[2] .. currentLocale.decimalSeperator .. out
158 end
159 return out:reverse()
160 elseif tostring(text) then
161 local translation = currentLocale.translation[text]
162 if not translation then
163 if translation == nil then
164 if currentLocale.name ~= defaultLocaleName then
165 if not missingTranslations[text] then
166 pdebug('Unable to translate: \"' .. text .. '\"')
167 missingTranslations[text] = true
168 end
169 end
170 end
171 translation = text
172 end
173 return string.format(translation, ...)
174 end
175 end
176 return text
177end
178