40_Localization.lua 6.1 KB

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