localization_helper.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. local M = {}
  2. -- Attach the font resource to the default font resource
  3. -- and prewarm the glyphs for smooth rendering.
  4. function M.set_runtime_font_and_prewarm(self, on_font_ready_callback)
  5. -- If the requested language has a proxy, attach the runtime font:
  6. if self.languages[self.requested_lang].proxy then
  7. -- Add the runtime font to the default font resource.
  8. font.add_font(self.default_font_resource, self.languages[self.requested_lang].ttf_hash)
  9. end
  10. -- Prewarm the glyphs for smooth rendering for the given requested text.
  11. local text = self.requested_text.text
  12. font.prewarm_text(self.default_font_resource, text, on_font_ready_callback)
  13. end
  14. -- Finalize language switch once the required font can render requested text.
  15. function M.finish_language_change(self, on_font_ready_callback)
  16. -- Prepare a callback function that will be called when fonts are ready.
  17. local on_language_changed = function()
  18. -- New language has been set, change the current language variable.
  19. self.current_lang = self.requested_lang
  20. on_font_ready_callback(self)
  21. end
  22. -- Set the runtime font and prewarm the glyphs for smooth rendering.
  23. M.set_runtime_font_and_prewarm(self, on_language_changed)
  24. end
  25. -- Process the language change.
  26. function M.process_language_change(self, callback_on_font_ready)
  27. -- If the requested language is the same as the current language:
  28. if self.requested_lang == self.current_lang then
  29. -- No change needed, skip the rest of the language change process.
  30. return
  31. end
  32. -- Otherwise:
  33. -- Release the input focus during the language change,
  34. -- so the user cannot interact in the meantime.
  35. -- You can still allow it, if needed in your game,
  36. -- but in that case keep track of the status of (un)loading.
  37. msg.post(".", "release_input_focus")
  38. local requested_language_proxy = self.languages[self.requested_lang].proxy
  39. local current_language_proxy = self.languages[self.current_lang].proxy
  40. -- If the requested language font is the same as the current:
  41. if requested_language_proxy == current_language_proxy then
  42. -- Finish the language change with prewarm.
  43. M.finish_language_change(self, callback_on_font_ready)
  44. -- And skip the rest of the language change process.
  45. return
  46. end
  47. -- Otherwise:
  48. -- If the current language is with a loaded proxy:
  49. if current_language_proxy then
  50. -- Unload this collection with the font resource first.
  51. msg.post(current_language_proxy, "unload")
  52. -- And skip the rest of the language change process.
  53. return
  54. end
  55. -- Load the new font.
  56. msg.post(requested_language_proxy, "async_load")
  57. end
  58. -- JSON file handling ------------------------------------------
  59. -- Get the text for the requested language from the JSON file.
  60. function M.get_content_from_json(self)
  61. -- Load the JSON file for the requested language.
  62. local language = self.languages[self.requested_lang]
  63. local json_file = sys.load_resource(language.json)
  64. assert(json_file, "Failed to load JSON file for language: " .. self.requested_lang)
  65. -- Return the decoded JSON file as a table.
  66. return json.decode(json_file) or {}
  67. end
  68. return M