ToggleLocalizationTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using I2.Loc;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. public class ToggleLocalizationTest : MonoBehaviour
  7. {
  8. private LanguageSource languageSource;
  9. // Start is called before the first frame update
  10. void Awake()
  11. {
  12. //languageSource = this.gameObject.AddComponent<LanguageSource>();
  13. LocalizationConfig.localization = true;
  14. //ReadCSVFrom_StreamingFolder(Application.streamingAssetsPath + "/Localization/OperationUILocalization.csv");
  15. LocalizationManager.OnLocalizeEvent += LocalizationManager_OnLocalizeEvent;
  16. }
  17. private void LocalizationManager_OnLocalizeEvent()
  18. {
  19. Debug.Log("LocalizationManager_OnLocalizeEvent");
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (Input.GetKeyDown(KeyCode.Q))
  25. {
  26. if (LocalizationManager.HasLanguage("Chinese"))
  27. {
  28. LocalizationManager.CurrentLanguage = "Chinese";
  29. }
  30. }
  31. if (Input.GetKeyDown(KeyCode.E))
  32. {
  33. if (LocalizationManager.HasLanguage("English"))
  34. {
  35. LocalizationManager.CurrentLanguage = "English";
  36. Debug.Log("English");
  37. }
  38. else
  39. {
  40. Debug.Log("NoEnglish");
  41. }
  42. }
  43. }
  44. public void UpdateTemp()
  45. {
  46. var localize = this.GetComponent<Localize>();
  47. // Change the term
  48. localize.SetTerm("REWARD_1");
  49. if (Input.GetKeyDown(KeyCode.A))
  50. {
  51. var source = LocalizationManager.Sources[1];
  52. var termData = source.GetTermData("Term001");
  53. termData.Languages[0] = "Text002";
  54. }
  55. if (Input.GetKeyDown(KeyCode.B))
  56. {
  57. var source = LocalizationManager.Sources[2];
  58. var termData = source.GetTermData("Term001");
  59. termData.Languages[0] = "Text003";
  60. }
  61. }
  62. void ReadCSVFrom_Resources()
  63. {
  64. var asset = Resources.Load<TextAsset>("localization.csv");
  65. // Check for errors (file not found)
  66. if (asset == null)
  67. {
  68. Debug.LogWarning("Unable to load Localization data");
  69. return;
  70. }
  71. UseLocalizationCSV(asset.text);
  72. }
  73. // This approach is useful for letting the user modify the csv file
  74. void ReadCSVFrom_StreamingFolder(string filePath)
  75. {
  76. try
  77. {
  78. var CSVstring = File.ReadAllText(filePath);
  79. UseLocalizationCSV(CSVstring);
  80. }
  81. catch (System.Exception e)
  82. {
  83. Debug.LogWarning("Unable to load Localization data");
  84. }
  85. }
  86. void UseLocalizationCSV(string CSVfile)
  87. {
  88. // Source[0] is the I2Languages.asset
  89. languageSource.SourceData.Import_CSV(string.Empty, CSVfile, eSpreadsheetUpdateMode.Replace, ',');
  90. LocalizationManager.LocalizeAll(); // Force localing all enabled labels/sprites with the new data
  91. }
  92. }