40_Localization.as 6.3 KB

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