40_Localization.lua 6.1 KB

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