40_Localization.as 6.2 KB

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