40_Localization.as 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  20. void InitLocalizationSystem()
  21. {
  22. // JSON files must be in UTF8 encoding without BOM
  23. // The first founded language will be set as current
  24. localization.LoadJSONFile("StringsEnRu.json");
  25. // You can load multiple files
  26. localization.LoadJSONFile("StringsDe.json");
  27. // Hook up to the change language
  28. SubscribeToEvent("ChangeLanguage", "HandleChangeLanguage");
  29. }
  30. void CreateGUI()
  31. {
  32. ui.root.defaultStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  33. Window@ window = Window();
  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. Text@ windowTitle = Text();
  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. String langName = localization.language;
  45. // Languages are numbered in the loading order
  46. int langIndex = localization.languageIndex; // == 0 at the beginning
  47. // Get string with identifier "title" in the current language
  48. String 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 + " (" + String(langIndex) + " " + langName + ")";
  52. Button@ b = Button();
  53. window.AddChild(b);
  54. b.SetStyle("Button");
  55. b.minHeight = 24;
  56. Text@ 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();
  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. }
  76. void CreateScene()
  77. {
  78. scene_ = Scene();
  79. scene_.CreateComponent("Octree");
  80. Zone@ zone = scene_.CreateComponent("Zone");
  81. zone.boundingBox = BoundingBox(-1000.0f, 1000.0f);
  82. zone.ambientColor = Color(0.5f, 0.5f, 0.5f);
  83. zone.fogColor = Color(0.4f, 0.5f, 0.8f);
  84. zone.fogStart = 1.0f;
  85. zone.fogEnd = 100.0f;
  86. Node@ planeNode = scene_.CreateChild("Plane");
  87. planeNode.scale = Vector3(300.0f, 1.0f, 300.0f);
  88. StaticModel@ planeObject = planeNode.CreateComponent("StaticModel");
  89. planeObject.model = cache.GetResource("Model", "Models/Plane.mdl");
  90. planeObject.material = cache.GetResource("Material", "Materials/StoneTiled.xml");
  91. Node@ lightNode = scene_.CreateChild("DirectionalLight");
  92. lightNode.direction = Vector3(0.6f, -1.0f, 0.8f);
  93. Light@ light = lightNode.CreateComponent("Light");
  94. light.lightType = LIGHT_DIRECTIONAL;
  95. light.color = Color(0.8f, 0.8f, 0.8f);
  96. cameraNode = scene_.CreateChild("Camera");
  97. cameraNode.CreateComponent("Camera");
  98. cameraNode.position = Vector3(0.0f, 10.0f, -30.0f);
  99. Node@ text3DNode = scene_.CreateChild("Text3D");
  100. text3DNode.position = Vector3(0.0f, 0.1f, 30.0f);
  101. text3DNode.SetScale(15);
  102. Text3D@ text3D = text3DNode.CreateComponent("Text3D");
  103. // Manually set text in the current language.
  104. text3D.text = localization.Get("lang");
  105. text3D.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30);
  106. text3D.color = BLACK;
  107. text3D.SetAlignment(HA_CENTER, VA_BOTTOM);
  108. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  109. renderer.viewports[0] = viewport;
  110. SubscribeToEvent("Update", "HandleUpdate");
  111. }
  112. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  113. {
  114. float timeStep = eventData["TimeStep"].GetFloat();
  115. const float MOUSE_SENSITIVITY = 0.1f;
  116. IntVector2 mouseMove = input.mouseMove;
  117. yaw += MOUSE_SENSITIVITY * mouseMove.x;
  118. pitch += MOUSE_SENSITIVITY * mouseMove.y;
  119. pitch = Clamp(pitch, -90.0f, 90.0f);
  120. cameraNode.rotation = Quaternion(pitch, yaw, 0.0f);
  121. }
  122. void HandleChangeLangButtonPressed(StringHash eventType, VariantMap& eventData)
  123. {
  124. // Languages are numbered in the loading order
  125. int lang = localization.languageIndex;
  126. lang++;
  127. if (lang >= localization.numLanguages)
  128. lang = 0;
  129. localization.SetLanguage(lang);
  130. }
  131. void HandleQuitButtonPressed(StringHash eventType, VariantMap& eventData)
  132. {
  133. engine.Exit();
  134. }
  135. // You can manually change texts, sprites and other aspects of the game when language is changed
  136. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
  137. {
  138. Text@ windowTitle = ui.root.GetChild("WindowTitle", true);
  139. windowTitle.text = localization.Get("title") + " (" +
  140. String(localization.languageIndex) + " " +
  141. localization.language + ")";
  142. Text@ buttonText = ui.root.GetChild("ButtonTextQuit", true);
  143. buttonText.text = localization.Get("quit");
  144. Text3D@ text3D = scene_.GetChild("Text3D").GetComponent("Text3D");
  145. text3D.text = localization.Get("lang");
  146. // A text on the button "Press this button" changes automatically
  147. }
  148. String patchInstructions = "";