模块:I18n

来自萌娘共享
跳转至: 导航搜索

此模块的文档可以在模块:I18n/doc创建

  1. local p = {}
  2. -- Credit to http://stackoverflow.com/a/1283608/2644759
  3. -- cc-by-sa 3.0
  4. local function tableMerge(t1, t2, overwrite)
  5. for k,v in pairs(t2) do
  6. if type(v) == "table" and type(t1[k]) == "table" then
  7. -- since type(t1[k]) == type(v) == "table", so t1[k] and v is true
  8. tableMerge(t1[k], v, overwrite) -- t2[k] == v
  9. else
  10. if overwrite or t1[k] == nil then t1[k] = v end
  11. end
  12. end
  13. return t1
  14. end
  15. function p.loadI18n(name, i18n_arg)
  16. local exist, res = pcall(require, name)
  17. if exist and next(res) ~= nil then
  18. if i18n_arg then
  19. tableMerge(i18n_arg, res.i18n, true)
  20. elseif type(i18n) == "table" then
  21. -- merge to global i18n
  22. tableMerge(i18n, res.i18n, true)
  23. end
  24. end
  25. end
  26. function p.loadI18nFrame(frame, i18n_arg)
  27. p.loadI18n(frame:getTitle().."/i18n", i18n_arg)
  28. end
  29. return p