TranslateHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using PackingPress.Common.Configuration;
  7. namespace PackingPress.Common
  8. {
  9. public enum LanguageEnum
  10. {
  11. Chinese,
  12. English,
  13. Romania
  14. }
  15. public class TranslateHelper
  16. {
  17. public const string ChineseLanguageKey = "Chinese";
  18. public const string EnglishLanguageKey = "English";
  19. private static ConfigManager _languageManager;
  20. private static string _currentLanguage;
  21. private static readonly Dictionary<string, string> DefaultNls = new Dictionary<string, string>
  22. {
  23. {ChineseLanguageKey, "zh-CN"},
  24. {EnglishLanguageKey, "en-US"},
  25. };
  26. public static readonly List<string> XingLingHuiServerRegion = new List<string>
  27. {
  28. "as","bn","bo","brx","dv","dz","ee", "en-hk","en-id","en-in","en-mo",
  29. "en-my","en-ph","en-pk","en-sg","fil","gu","hi","id","ja","jv","jv-java",
  30. "jv-latn","km","kn","ko","ks","ks-arab","ks-deva","ksb","ky","lo","ml",
  31. "mn","mn-cyrl","mn-mong","mni","mr","ms","my","ne","or","pa","pa-arab","ps",
  32. "sa","sd","sd-arab","sd-deva","si","ta","te","th","ug","ur","vi","zh",
  33. "zh-hans","zh-hant","zh-chs","zh-cht","zh-cn","zh-sg","zh-tw","zh-hk","zh-mo",
  34. };
  35. private static readonly Dictionary<string, string> CultureToLanguage = new Dictionary<string, string>();
  36. private static readonly Dictionary<string, string> LanguageResources = new Dictionary<string, string>();
  37. /// <summary>
  38. /// Gets the language names which helper supported.
  39. /// </summary>
  40. public static string[] SupportedLanguages => DefaultNls.Keys.ToArray();
  41. /// <summary>
  42. /// Gets or sets the current language.
  43. /// </summary>
  44. public static string CurrentLanguage
  45. {
  46. get { return _currentLanguage; }
  47. set
  48. {
  49. if (_currentLanguage != value)
  50. {
  51. _currentLanguage = value;
  52. Initialize(_currentLanguage);
  53. }
  54. }
  55. }
  56. // Constructors 
  57. static TranslateHelper()
  58. {
  59. foreach (KeyValuePair<string, string> keyValuePair in DefaultNls)
  60. {
  61. CultureToLanguage[keyValuePair.Value.ToLower()] = keyValuePair.Key;
  62. }
  63. var currentLanguage = CultureInfo.CurrentCulture.Name.ToLower();
  64. if (currentLanguage == "zh-sg")//if Singapore is zh-sg ,system language is chinese
  65. {
  66. currentLanguage = "zh-cn";
  67. }
  68. CurrentLanguage = CultureToLanguage.ContainsKey(currentLanguage)
  69. ? CultureToLanguage[currentLanguage]
  70. : "English";
  71. }
  72. // Methods 
  73. public static string GetDefaultSystemLanguage()
  74. {
  75. var currentLanguage = CultureInfo.CurrentCulture.Name.ToLower();
  76. return CultureToLanguage.ContainsKey(currentLanguage)
  77. ? CultureToLanguage[currentLanguage]
  78. : "English";
  79. }
  80. /// <summary>
  81. /// Add language and related resource into helper.
  82. /// </summary>
  83. /// <param name="language"></param>
  84. /// <param name="resource"></param>
  85. public static void AddLanguageResource(string language, string resource)
  86. {
  87. if (!LanguageResources.Keys.Contains(language))
  88. {
  89. LanguageResources.Add(language, resource);
  90. }
  91. }
  92. public static LanguageEnum IsChineseCultureInfo()
  93. {
  94. var currentLanguage = CultureInfo.CurrentCulture.Name.ToLower();
  95. if (currentLanguage.StartsWith("zh")|| XingLingHuiServerRegion.Contains(currentLanguage))
  96. {
  97. return LanguageEnum.Chinese;
  98. }
  99. if (currentLanguage.StartsWith("ro"))
  100. {
  101. return LanguageEnum.Romania;
  102. }
  103. return LanguageEnum.English;
  104. }
  105. /// <summary>
  106. /// Initialize TranslateHelper with specific language.
  107. /// </summary>
  108. /// <param name="languageName">"Chinese" "English"</param>
  109. public static void Initialize(string languageName)
  110. {
  111. CurrentLanguage = languageName;
  112. LoadTranslationResource();
  113. }
  114. private static void LoadTranslationResource()
  115. {
  116. if (LanguageResources.ContainsKey(CurrentLanguage))
  117. {
  118. _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
  119. }
  120. }
  121. /// <summary>
  122. /// Update a translation of current language.
  123. /// </summary>
  124. /// <param name="key">The key of the translation</param>
  125. /// <param name="value">The updated value.</param>
  126. public static void UpdateTranslation(string key, string value)
  127. {
  128. if (_languageManager == null)
  129. {
  130. if (LanguageResources.ContainsKey(CurrentLanguage))
  131. {
  132. _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
  133. }
  134. }
  135. if (!string.IsNullOrEmpty(key))
  136. {
  137. ((IConfigUpdater)_languageManager).SetValue("Languages", key, value);
  138. }
  139. }
  140. /// <summary>
  141. /// Translate to target Language with specified key.
  142. /// </summary>
  143. /// <param name="key"></param>
  144. /// <returns></returns>
  145. public static string Translate(string key)
  146. {
  147. if (_languageManager == null)
  148. {
  149. if (LanguageResources.ContainsKey(CurrentLanguage))
  150. {
  151. _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
  152. }
  153. }
  154. if (string.IsNullOrEmpty(key))
  155. {
  156. return string.Empty;
  157. }
  158. var translatedValue = _languageManager?.GetValue("Languages", key, string.Empty);
  159. if (string.IsNullOrEmpty(translatedValue))
  160. {
  161. return key;
  162. }
  163. return translatedValue.Replace("\\r\\n",Environment.NewLine);
  164. }
  165. }
  166. }