40_Localization.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. -- Localization example.
  2. -- This sample demonstrates:
  3. -- - Loading a collection of strings from JSON-files
  4. -- - Creating text elements that automatically translates itself by changing the language
  5. -- - The manually reaction to change language
  6. require "LuaScripts/Utilities/Sample"
  7. function Start()
  8. -- Execute the common startup for samples
  9. SampleStart()
  10. -- Enable and center OS cursor
  11. input.mouseVisible = true
  12. input:CenterMousePosition()
  13. -- Load strings from JSON files and subscribe to the change language event
  14. InitLocalizationSystem()
  15. -- Init the 3D space
  16. CreateScene()
  17. -- Init the user interface
  18. CreateGUI()
  19. -- Set the mouse mode to use in the sample
  20. SampleInitMouseMode(MM_FREE)
  21. end
  22. function InitLocalizationSystem()
  23. -- JSON files must be in UTF8 encoding without BOM
  24. -- The first found language will be set as current
  25. localization:LoadJSONFile("StringsEnRu.json")
  26. -- You can load multiple files
  27. localization:LoadJSONFile("StringsDe.json")
  28. localization:LoadJSONFile("StringsLv.json", "lv")
  29. -- Hook up to the change language
  30. SubscribeToEvent("ChangeLanguage", "HandleChangeLanguage")
  31. end
  32. function CreateGUI()
  33. ui.root.defaultStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  34. local window = Window:new()
  35. ui.root:AddChild(window)
  36. window:SetMinSize(384, 192)
  37. window:SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6))
  38. window:SetAlignment(HA_CENTER, VA_CENTER)
  39. window:SetStyleAuto()
  40. local windowTitle = Text:new()
  41. windowTitle.name = "WindowTitle"
  42. windowTitle:SetStyleAuto()
  43. window:AddChild(windowTitle)
  44. -- In this place the current language is "en" because it was found first when loading the JSON files
  45. local langName = localization.language
  46. -- Languages are numbered in the loading order
  47. local langIndex = localization.languageIndex -- == 0 at the beginning
  48. -- Get string with identifier "title" in the current language
  49. local localizedString = localization:Get("title")
  50. -- Localization:Get returns String.EMPTY if the id is empty.
  51. -- Localization:Get returns the id if translation is not found and will be added a warning into the log.
  52. windowTitle.text = localizedString .. " (" .. langIndex .. " " .. langName .. ")"
  53. local b = Button:new()
  54. window:AddChild(b)
  55. b:SetStyle("Button")
  56. b.minHeight = 24
  57. local t = b:CreateChild("Text", "ButtonTextChangeLang")
  58. -- The showing text value will automatically change when language is changed
  59. t.autoLocalizable = true
  60. -- The text value used as a string identifier in this mode.
  61. -- Remember that a letter case of the id and of the lang name is important.
  62. t.text = "Press this button"
  63. t:SetAlignment(HA_CENTER, VA_CENTER)
  64. t:SetStyle("Text")
  65. SubscribeToEvent(b, "Released", "HandleChangeLangButtonPressed")
  66. b = Button:new()
  67. window:AddChild(b)
  68. b:SetStyle("Button")
  69. b.minHeight = 24
  70. t = b:CreateChild("Text", "ButtonTextQuit")
  71. t:SetAlignment(HA_CENTER, VA_CENTER)
  72. t:SetStyle("Text")
  73. -- Manually set text in the current language
  74. t.text = localization:Get("quit")
  75. SubscribeToEvent(b, "Released", "HandleQuitButtonPressed")
  76. end
  77. function CreateScene()
  78. scene_ = Scene:new()
  79. scene_:CreateComponent("Octree")
  80. local zone = scene_:CreateComponent("Zone")
  81. zone.boundingBox = BoundingBox:new(-1000.0, 1000.0)
  82. zone.ambientColor = Color:new(0.5, 0.5, 0.5)
  83. zone.fogColor = Color:new(0.4, 0.5, 0.8)
  84. zone.fogStart = 1.0
  85. zone.fogEnd = 100.0
  86. local planeNode = scene_:CreateChild("Plane")
  87. planeNode.scale = Vector3:new(300.0, 1.0, 300.0)
  88. local planeObject = planeNode:CreateComponent("StaticModel")
  89. planeObject.model = cache:GetResource("Model", "Models/Plane.mdl")
  90. planeObject.material = cache:GetResource("Material", "Materials/StoneTiled.xml")
  91. local lightNode = scene_:CreateChild("DirectionalLight")
  92. lightNode.direction = Vector3:new(0.6, -1.0, 0.8)
  93. local light = lightNode:CreateComponent("Light")
  94. light.lightType = LIGHT_DIRECTIONAL
  95. light.color = Color:new(0.8, 0.8, 0.8)
  96. cameraNode = scene_:CreateChild("Camera")
  97. cameraNode:CreateComponent("Camera")
  98. cameraNode.position = Vector3:new(0.0, 10.0, -30.0)
  99. local text3DNode = scene_:CreateChild("Text3D")
  100. text3DNode.position = Vector3:new(0.0, 0.1, 30.0)
  101. text3DNode:SetScale(15)
  102. local text3D = text3DNode:CreateComponent("Text3D")
  103. -- Manually set text in the current language.
  104. text3D.text = localization:Get("lang")
  105. text3D:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30)
  106. text3D.color = Color.BLACK
  107. text3D:SetAlignment(HA_CENTER, VA_BOTTOM)
  108. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  109. renderer:SetViewport(0, viewport)
  110. SubscribeToEvent("Update", "HandleUpdate")
  111. end
  112. function HandleUpdate(eventType, eventData)
  113. local timeStep = eventData["TimeStep"]:GetFloat()
  114. local MOUSE_SENSITIVITY = 0.1
  115. local mouseMove = input.mouseMove
  116. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  117. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  118. pitch = Clamp(pitch, -90.0, 90.0)
  119. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  120. end
  121. function HandleChangeLangButtonPressed(eventType, eventData)
  122. -- Languages are numbered in the loading order
  123. local lang = localization.languageIndex
  124. lang = lang + 1
  125. if lang >= localization.numLanguages then
  126. lang = 0
  127. end
  128. localization:SetLanguage(lang)
  129. end
  130. function HandleQuitButtonPressed(eventType, eventData)
  131. engine:Exit()
  132. end
  133. -- You can manually change texts, sprites and other aspects of the game when language is changed
  134. function HandleChangeLanguage(eventType, eventData)
  135. local windowTitle = ui.root:GetChild("WindowTitle", true)
  136. windowTitle.text = localization:Get("title") .. " (" ..
  137. localization.languageIndex .. " " ..
  138. localization.language .. ")"
  139. local buttonText = ui.root:GetChild("ButtonTextQuit", true)
  140. buttonText.text = localization:Get("quit")
  141. local text3D = scene_:GetChild("Text3D"):GetComponent("Text3D")
  142. text3D.text = localization:Get("lang")
  143. -- A text on the button "Press this button" changes automatically
  144. end