using System; using System.Collections.Generic; using System.Linq; using Vinno.IUS.Common.Configuration; namespace vCloud.Windows.ForceUpgrade.Common { public enum LanguageEnum { Chinese, English, } public class TranslateHelper { public const string ChineseLanguageKey = "Chinese"; public const string EnglishLanguageKey = "English"; private static ConfigManager _languageManager; private static string _currentLanguage; private static readonly Dictionary DefaultNls = new Dictionary { {ChineseLanguageKey, "zh-CN"}, {EnglishLanguageKey, "en-US"}, }; public static readonly List XingLingHuiServerRegion = new List { "as","bn","bo","brx","dv","dz","ee", "en-hk","en-id","en-in","en-mo", "en-my","en-ph","en-pk","en-sg","fil","gu","hi","id","ja","jv","jv-java", "jv-latn","km","kn","ko","ks","ks-arab","ks-deva","ksb","ky","lo","ml", "mn","mn-cyrl","mn-mong","mni","mr","ms","my","ne","or","pa","pa-arab","ps", "sa","sd","sd-arab","sd-deva","si","ta","te","th","ug","ur","vi","zh", "zh-hans","zh-hant","zh-chs","zh-cht","zh-cn","zh-sg","zh-tw","zh-hk","zh-mo", }; private static readonly Dictionary CultureToLanguage = new Dictionary(); private static readonly Dictionary LanguageResources = new Dictionary(); /// /// Gets the language names which helper supported. /// public static string[] SupportedLanguages => DefaultNls.Keys.ToArray(); /// /// Gets or sets the current language. /// public static string CurrentLanguage { get { return _currentLanguage; } set { if (_currentLanguage != value) { _currentLanguage = value; Initialize(_currentLanguage); } } } /// /// Add language and related resource into helper. /// /// /// public static void AddLanguageResource(string language, string resource) { if (!LanguageResources.Keys.Contains(language)) { LanguageResources.Add(language, resource); } } /// /// Initialize TranslateHelper with specific language. /// /// "Chinese" "English" public static void Initialize(string languageName) { CurrentLanguage = languageName; LoadTranslationResource(); } private static void LoadTranslationResource() { if (LanguageResources.ContainsKey(CurrentLanguage)) { _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]); } } /// /// Translate to target Language with specified key. /// /// /// public static string Translate(string key) { if (_languageManager == null) { if (LanguageResources.ContainsKey(CurrentLanguage)) { _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]); } } if (string.IsNullOrEmpty(key)) { return string.Empty; } var translatedValue = _languageManager?.GetValue("Languages", key, string.Empty); if (string.IsNullOrEmpty(translatedValue)) { return key; } return translatedValue.Replace("\\r\\n", Environment.NewLine); } } }