40_Localization.lua 6.0 KB

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