TranslateHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Vinno.IUS.Common.Configuration;
  5. namespace vCloud.Windows.ForceUpgrade.Common
  6. {
  7. public enum LanguageEnum
  8. {
  9. Chinese,
  10. English,
  11. }
  12. public class TranslateHelper
  13. {
  14. public const string ChineseLanguageKey = "Chinese";
  15. public const string EnglishLanguageKey = "English";
  16. private static ConfigManager _languageManager;
  17. private static string _currentLanguage;
  18. private static readonly Dictionary<string, string> DefaultNls = new Dictionary<string, string>
  19. {
  20. {ChineseLanguageKey, "zh-CN"},
  21. {EnglishLanguageKey, "en-US"},
  22. };
  23. public static readonly List<string> XingLingHuiServerRegion = new List<string>
  24. {
  25. "as","bn","bo","brx","dv","dz","ee", "en-hk","en-id","en-in","en-mo",
  26. "en-my","en-ph","en-pk","en-sg","fil","gu","hi","id","ja","jv","jv-java",
  27. "jv-latn","km","kn","ko","ks","ks-arab","ks-deva","ksb","ky","lo","ml",
  28. "mn","mn-cyrl","mn-mong","mni","mr","ms","my","ne","or","pa","pa-arab","ps",
  29. "sa","sd","sd-arab","sd-deva","si","ta","te","th","ug","ur","vi","zh",
  30. "zh-hans","zh-hant","zh-chs","zh-cht","zh-cn","zh-sg","zh-tw","zh-hk","zh-mo",
  31. };
  32. private static readonly Dictionary<string, string> CultureToLanguage = new Dictionary<string, string>();
  33. private static readonly Dictionary<string, string> LanguageResources = new Dictionary<string, string>();
  34. /// <summary>
  35. /// Gets the language names which helper supported.
  36. /// </summary>
  37. public static string[] SupportedLanguages => DefaultNls.Keys.ToArray();
  38. /// <summary>
  39. /// Gets or sets the current language.
  40. /// </summary>
  41. public static string CurrentLanguage
  42. {
  43. get { return _currentLanguage; }
  44. set
  45. {
  46. if (_currentLanguage != value)
  47. {
  48. _currentLanguage = value;
  49. Initialize(_currentLanguage);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// Add language and related resource into helper.
  55. /// </summary>
  56. /// <param name="language"></param>
  57. /// <param name="resource"></param>
  58. public static void AddLanguageResource(string language, string resource)
  59. {
  60. if (!LanguageResources.Keys.Contains(language))
  61. {
  62. LanguageResources.Add(language, resource);
  63. }
  64. }
  65. /// <summary>
  66. /// Initialize TranslateHelper with specific language.
  67. /// </summary>
  68. /// <param name="languageName">"Chinese" "English"</param>
  69. public static void Initialize(string languageName)
  70. {
  71. CurrentLanguage = languageName;
  72. LoadTranslationResource();
  73. }
  74. private static void LoadTranslationResource()
  75. {
  76. if (LanguageResources.ContainsKey(CurrentLanguage))
  77. {
  78. _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
  79. }
  80. }
  81. /// <summary>
  82. /// Translate to target Language with specified key.
  83. /// </summary>
  84. /// <param name="key"></param>
  85. /// <returns></returns>
  86. public static string Translate(string key)
  87. {
  88. if (_languageManager == null)
  89. {
  90. if (LanguageResources.ContainsKey(CurrentLanguage))
  91. {
  92. _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
  93. }
  94. }
  95. if (string.IsNullOrEmpty(key))
  96. {
  97. return string.Empty;
  98. }
  99. var translatedValue = _languageManager?.GetValue("Languages", key, string.Empty);
  100. if (string.IsNullOrEmpty(translatedValue))
  101. {
  102. return key;
  103. }
  104. return translatedValue.Replace("\\r\\n", Environment.NewLine);
  105. }
  106. }
  107. }