123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 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<string, string> DefaultNls = new Dictionary<string, string>
- {
- {ChineseLanguageKey, "zh-CN"},
- {EnglishLanguageKey, "en-US"},
- };
- public static readonly List<string> XingLingHuiServerRegion = new List<string>
- {
- "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<string, string> CultureToLanguage = new Dictionary<string, string>();
- private static readonly Dictionary<string, string> LanguageResources = new Dictionary<string, string>();
- /// <summary>
- /// Gets the language names which helper supported.
- /// </summary>
- public static string[] SupportedLanguages => DefaultNls.Keys.ToArray();
- /// <summary>
- /// Gets or sets the current language.
- /// </summary>
- public static string CurrentLanguage
- {
- get { return _currentLanguage; }
- set
- {
- if (_currentLanguage != value)
- {
- _currentLanguage = value;
- Initialize(_currentLanguage);
- }
- }
- }
- /// <summary>
- /// Add language and related resource into helper.
- /// </summary>
- /// <param name="language"></param>
- /// <param name="resource"></param>
- public static void AddLanguageResource(string language, string resource)
- {
- if (!LanguageResources.Keys.Contains(language))
- {
- LanguageResources.Add(language, resource);
- }
- }
- /// <summary>
- /// Initialize TranslateHelper with specific language.
- /// </summary>
- /// <param name="languageName">"Chinese" "English"</param>
- public static void Initialize(string languageName)
- {
- CurrentLanguage = languageName;
- LoadTranslationResource();
- }
- private static void LoadTranslationResource()
- {
- if (LanguageResources.ContainsKey(CurrentLanguage))
- {
- _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
- }
- }
- /// <summary>
- /// Translate to target Language with specified key.
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|