Commit 75fc94e1f978b155c63a24b95d37d7be6332bc05

Authored by DarkGod
1 parent e6b4ce6f

support loading locales from engine, modules and addons

... ... @@ -103,12 +103,24 @@ function _M:loadLocale(file)
103 103 end,
104 104 }, {__index=getfenv(2)})
105 105 local f, err = util.loadfilemods(file, env)
106   - if not f and err then error(err) end
  106 + if not f and err then
  107 + if config.settings.cheat then
  108 + error(err)
  109 + else
  110 + print("[I18N] ERROR, localization file ", file, "not loaded due to error:", err)
  111 + return
  112 + end
  113 + end
107 114 f()
  115 + print("[I18N] Loaded locale file:", file)
108 116 end
109 117
110 118 function _M:setLocale(lc)
111 119 cur_locale_name = lc
  120 + if not locales[lc] then locales[lc] = {} end
  121 + if not locales_args[lc] then locales_args[lc] = {} end
  122 + if not locales_special[lc] then locales_special[lc] = {} end
  123 + if not flags[lc] then flags[lc] = {} end
112 124 cur_locale = locales[lc] or {}
113 125 cur_locale_args = locales_args[lc] or {}
114 126 cur_locale_special = locales_special[lc] or {}
... ... @@ -155,6 +167,10 @@ function _M.setFlag(lc, flag, data)
155 167 end
156 168 end
157 169
  170 +function _M:getLocalesData()
  171 + return cur_locale_name, cur_locale, cur_locale_args, cur_locale_special, cur_flags, "::", locales, locales_args, locales_special, flags
  172 +end
  173 +
158 174 function _M:test()
159 175 self:loadLocale("/data/locales/fr_FR.lua")
160 176 self:setLocale("fr_FR")
... ...
... ... @@ -501,6 +501,11 @@ function _M:loadAddon(mod, add, hashlist, hooks_list)
501 501 elseif add.teaa then fs.mount("subdir:/data/|"..fs.getRealPath(add.teaa), "/data-"..add.short_name, true)
502 502 else fs.mount(base.."/data", "/data-"..add.short_name, true)
503 503 end
  504 +
  505 + -- Load localizations, addons just need to provide the file and it's autoloaded
  506 + if mod.i18n_support and config.settings.locale then
  507 + I18N:loadLocale("/data-"..add.short_name.."/locales/"..config.settings.locale..".lua")
  508 + end
504 509 end
505 510 if add.superload then
506 511 print(" * with superload")
... ... @@ -939,14 +944,9 @@ function _M:instanciate(mod, name, new_game, no_reboot, extra_module_info)
939 944 mod.load("setup")
940 945
941 946 -- Load localizations
942   - if mod.i18n_support then
943   - local locale = config.settings.locale or "en_US"
944   - I18N:setLocale(locale)
945   - I18N:loadLocale("/data/i18n/"..locale..".lua")
  947 + if mod.i18n_support and config.settings.locale then
  948 + I18N:loadLocale("/data/locales/"..config.settings.locale..".lua")
946 949 end
947   -
948   - -- I18N:loadLocale("/data/locales/zh_hans.lua")
949   - -- I18N:setLocale("zh_hans")
950 950
951 951 -- Load font packages
952 952 FontPackage:loadDefinition("/data/font/packages/default.lua")
... ...
... ... @@ -125,9 +125,10 @@ if core.display.safeMode() then
125 125 config.settings.window = {size='800x600 Windowed'}
126 126 end
127 127
128   --- I18N:loadLocale("/data/locales/zh_hans.lua")
129   --- I18N:setLocale("zh_hans")
130   -
  128 +if config.settings.locale then
  129 + I18N:loadLocale("/data/locales/engine/"..config.settings.locale..".lua")
  130 + I18N:setLocale(config.settings.locale)
  131 +end
131 132
132 133 -- Default resolution as big as possible
133 134 if not config.settings.window or not config.settings.window.size then
... ...
... ... @@ -61,7 +61,7 @@ teams = {
61 61
62 62 i18n_support = true
63 63 show_funfacts = true
64   -loading_wait_ticks = 260
  64 +loading_wait_ticks = 280
65 65 profile_stats_fields = {"artifacts", "characters", "deaths", "uniques", "scores", "lore", "escorts"}
66 66 allow_userchat = true -- We can talk to the online community
67 67 no_get_name = true -- Name setting for new characters is done by the module itself
... ...