2
0

i18n.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local tparser = require "luci.template.parser"
  4. local util = require "luci.util"
  5. local tostring = tostring
  6. module "luci.i18n"
  7. i18ndir = util.libpath() .. "/i18n/"
  8. context = util.threadlocal()
  9. default = "en"
  10. function setlanguage(lang)
  11. local code, subcode = lang:match("^([A-Za-z][A-Za-z])[%-_]([A-Za-z][A-Za-z])$")
  12. if not (code and subcode) then
  13. subcode = lang:match("^([A-Za-z][A-Za-z])$")
  14. if not subcode then
  15. return nil
  16. end
  17. end
  18. context.parent = code and code:lower()
  19. context.lang = context.parent and context.parent.."-"..subcode:lower() or subcode:lower()
  20. if tparser.load_catalog(context.lang, i18ndir) and
  21. tparser.change_catalog(context.lang)
  22. then
  23. return context.lang
  24. elseif context.parent then
  25. if tparser.load_catalog(context.parent, i18ndir) and
  26. tparser.change_catalog(context.parent)
  27. then
  28. return context.parent
  29. end
  30. end
  31. return nil
  32. end
  33. function translate(key)
  34. return tparser.translate(key) or key
  35. end
  36. function translatef(key, ...)
  37. return tostring(translate(key)):format(...)
  38. end
  39. function dump()
  40. local rv = {}
  41. tparser.get_translations(function(k, v) rv[k] = v end)
  42. return rv
  43. end