123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using PackingPress.Common.Configuration;
- namespace PackingPress.Common
- {
- public enum LanguageEnum
- {
- Chinese,
- English,
- Romania
- }
- 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);
- }
- }
- }
- // Constructors
- static TranslateHelper()
- {
- foreach (KeyValuePair<string, string> keyValuePair in DefaultNls)
- {
- CultureToLanguage[keyValuePair.Value.ToLower()] = keyValuePair.Key;
- }
- var currentLanguage = CultureInfo.CurrentCulture.Name.ToLower();
- if (currentLanguage == "zh-sg")//if Singapore is zh-sg ,system language is chinese
- {
- currentLanguage = "zh-cn";
- }
- CurrentLanguage = CultureToLanguage.ContainsKey(currentLanguage)
- ? CultureToLanguage[currentLanguage]
- : "English";
- }
- // Methods
- public static string GetDefaultSystemLanguage()
- {
- var currentLanguage = CultureInfo.CurrentCulture.Name.ToLower();
- return CultureToLanguage.ContainsKey(currentLanguage)
- ? CultureToLanguage[currentLanguage]
- : "English";
- }
- /// <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);
- }
- }
- public static LanguageEnum IsChineseCultureInfo()
- {
- var currentLanguage = CultureInfo.CurrentCulture.Name.ToLower();
- if (currentLanguage.StartsWith("zh")|| XingLingHuiServerRegion.Contains(currentLanguage))
- {
- return LanguageEnum.Chinese;
- }
- if (currentLanguage.StartsWith("ro"))
- {
- return LanguageEnum.Romania;
- }
- return LanguageEnum.English;
- }
- /// <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>
- /// Update a translation of current language.
- /// </summary>
- /// <param name="key">The key of the translation</param>
- /// <param name="value">The updated value.</param>
- public static void UpdateTranslation(string key, string value)
- {
- if (_languageManager == null)
- {
- if (LanguageResources.ContainsKey(CurrentLanguage))
- {
- _languageManager = new ConfigManager(LanguageResources[CurrentLanguage]);
- }
- }
- if (!string.IsNullOrEmpty(key))
- {
- ((IConfigUpdater)_languageManager).SetValue("Languages", key, value);
- }
- }
- /// <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);
- }
- }
- }
|