40_Localization.as 6.3 KB

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