language.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. shell=util.shell
  2. function default_locales()
  3. local cmd="locale -a"
  4. local _locales={}
  5. for line in shell(cmd):gmatch("[^\r\n]+") do
  6. table.insert(_locales,line)
  7. end
  8. return {locales=_locales}
  9. end
  10. function sys_lang(lang)
  11. local cmd="initc locale.language"
  12. if lang ~= nil then
  13. local valid=false
  14. for _,l in ipairs(default_locales().locales) do
  15. if l == lang then
  16. valid=true
  17. break
  18. end
  19. end
  20. if valid then shell(cmd..lang)
  21. else
  22. return {error="language is not valid"}
  23. end
  24. end
  25. return {language={system=shell(cmd)}}
  26. end
  27. function keyb_lang(lang)
  28. local cmd="initc keyboard"
  29. if lang ~= nil then
  30. -- check if it is valid
  31. shell(cmd..lang)
  32. end
  33. return {language={keyboard=shell(cmd)}}
  34. end
  35. function run()
  36. ret.data={}
  37. if request and request.data then
  38. req=json.decode(request.data)
  39. if req.method == "get" then
  40. if req.query == nil then req.query = "system" end
  41. if req.query == "keyboard" then
  42. ret.data=keyb_lang()
  43. end
  44. if req.query == "system" then
  45. ret.data=sys_lang()
  46. end
  47. if req.query == "locales" then
  48. ret.data=default_locales()
  49. end
  50. end
  51. if req.method == "post" then
  52. if req.system then
  53. ret.data=sys_lang(req.system)
  54. end
  55. if req.keyboard then
  56. ret.data=keyb_lang(req.keyboard)
  57. end
  58. end
  59. else
  60. ret.api={
  61. info="Yerelliştirme Dil Yönetimi",
  62. auth=false,
  63. method={
  64. get={
  65. query="locales",
  66. },
  67. post={
  68. system="sistem dili",
  69. keyboard="klavye dili",
  70. }
  71. },
  72. }
  73. end
  74. response=json.encode(ret)
  75. end