示例#1
0
        protected string GetTranslation([CallerMemberName] string prop = null)
        {
            var pi = GetType().GetProperty(prop);

            if (pi == null)
            {
                return(null);
            }
            var jsonName = prop;
            var dm       = pi.GetCustomAttribute <DataMemberAttribute>();

            if (dm != null)
            {
                jsonName = dm.Name;
            }
            if (Translations != null && Translations.ContainsKey(jsonName) && Translations[jsonName].ContainsKey(LanguageContext.Instance.Culture.Name))
            {
                return(Translations[jsonName][LanguageContext.Instance.Culture.Name].ToString());
            }
            if (Defaults.ContainsKey(prop))
            {
                return(Defaults[prop].ToString());
            }
            return(null);
        }
示例#2
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DesignLanguageInfo)
            {
                return(parameter);
            }

            var lang = value as LanguageInfo;

            if (lang == null)
            {
                return(null);
            }

            var text = parameter.ToString();

            string translatedText;

            if (!Translations.ContainsKey(text) || !Translations[text].TryGetValue(lang.TwoLetterISOLanguageName, out translatedText) || string.IsNullOrEmpty(translatedText))
            {
                translatedText = text;
            }

            return(translatedText);
        }
示例#3
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DesignLanguageInfo)
            {
                return(parameter);
            }

            var lang = value as LanguageInfo;

            if (lang == null)
            {
                return(null);
            }

            var text = parameter.ToString();

#if DEBUG
            /*if (!Translations.ContainsKey(text))
             * {
             *  MessageBox.Show($"No localization for text : {text}");
             * }*/
#endif

            string translatedText;
            if (!Translations.ContainsKey(text) || !Translations[text].TryGetValue(lang.TwoLetterISOLanguageName, out translatedText) || string.IsNullOrEmpty(translatedText))
            {
                translatedText = text;
            }

            return(translatedText);
        }
示例#4
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DesignLanguageInfo)
            {
                return(parameter);
            }

            var lang = value as LanguageInfo;

            if (lang == null)
            {
                return(null);
            }

            var text = parameter.ToString();

#if DEBUG
            /*if (!Translations.ContainsKey(text) && text != "Konnga Ale" && text != "Lavian Brandy" && text != "Xihe Companions" && text != "Fujin Tea" && text != "Kamitra Cigars" && text != "Painite" && text != "Gold" && text != "Landmines" && text != "Meta-alloys" && text != "Soontill Relics")
             * {
             *  // MessageBox.Show($"No localization for text : {text}");
             * }*/
#endif

            string translatedText;
            if (!Translations.ContainsKey(text) || !Translations[text].TryGetValue(lang.TwoLetterISOLanguageName, out translatedText) || string.IsNullOrEmpty(translatedText))
            {
                translatedText = text;
            }

            return(translatedText);
        }
示例#5
0
        private void AddNewTranslation(object sender, RoutedEventArgs e)
        {
            var combo = (AvailableLanguages.SelectedItem as ComboBoxItem);

            if (combo != null)
            {
                var langID = (int)combo.Tag;
                var vm     = new TranslationViewModel()
                {
                    Translation    = "Translation",
                    Pronounciation = "Pronounce",
                    Significance   = 1.0,
                };

                if (Translations.ContainsKey(langID))
                {
                    Translations[langID].Add(vm);
                }
                else
                {
                    Translations.Add(langID, new List <TranslationViewModel>()
                    {
                        vm
                    });
                }

                FilteredTranslations.Add(vm);
            }
        }
示例#6
0
        public static string L(string developerText, params object[] parameters)
        {
            var format = developerText;

#if DEBUG
            format = FakeText(format);
            Trace.WriteLineIf(
                !Translations.ContainsKey(developerText),
                "Missing translation entry for: \"" + developerText + "\""
                );
#else
            if (LanguageIndex >= 0)
            {
                string[] localizations;
                if (Translations.TryGetValue(developerText, out localizations))
                {
                    if (localizations[LanguageIndex] != null)
                    {
                        format = localizations[LanguageIndex];
                    }
                }
            }
#endif

            return(string.Format(format, parameters));
        }
示例#7
0
        public Catalog GetCatalog(CultureInfo culture = null)
        {
            culture = culture ?? DefaultCultureInfo;

            return(Translations.ContainsKey(culture.Name)
                ? Translations[culture.Name]
                : (Translations.ContainsKey(culture.Parent.Name) ? Translations[culture.Parent.Name] : CreateCatalog(culture)));
        }
示例#8
0
        private static void UpgradeTranslations(Translations translations)
        {
            var defaults = Translations.CreateDefault();

            foreach (var item in defaults)
            {
                if (!translations.ContainsKey(item.Key))
                {
                    translations.Add(item.Key, item.Value);
                }
            }
        }
示例#9
0
 public string Translate(string key, string text)
 {
     if (Translations.ContainsKey(key))
     {
         return(Translations[key]);
     }
     else
     {
         Translations.Add(key, text);
         return(text);
     }
 }
示例#10
0
        private void filterTranslationIfAble()
        {
            FilteredTranslations.Clear();
            var combo = (AvailableLanguages.SelectedItem as ComboBoxItem);

            if (combo != null)
            {
                var langID = (int)combo.Tag;
                if (Translations.ContainsKey(langID))
                {
                    Translations[langID].ForEach(t => FilteredTranslations.Add(t));
                }
            }
        }
示例#11
0
        public static string Translate(string format, params object[] parameters)
        {
            // If header/format pair exists
            if (Translations.ContainsKey(format))
            {
                // Return properly formatted translation
                return(string.Format(Translations[format], parameters));
            }

            // Join all parameters in one string separated by spaces
            string joined = string.Join(" ", parameters);

            // Return header and parameters in format of fallback
            return(string.Format(BackupTranslationFormat, format, joined));
        }
示例#12
0
        /// <summary>
        /// Merges translations from ts1 into ts2 and returns the result.
        /// Source translations will overwrite target ones if there is a collision.
        /// </summary>
        /// <param name="ts1">Source Translations</param>
        /// <param name="ts2">Target Translations</param>
        /// <returns>Merged Translations</returns>
        private Translations mergeTranslations(Translations ts1, Translations ts2)
        {
            ts1.ToList().ForEach(t =>
            {
                if (ts2.ContainsKey(t.Key))
                {
                    t.Value.ToList().ForEach(kv => ts2[t.Key][kv.Key] = kv.Value);
                }
                else
                {
                    ts2[t.Key] = t.Value;
                }
            });

            return(ts2);
        }
示例#13
0
 public string GetActiveMessage()
 {
     if (TranslationState == UITranslationState.Original)
     {
         return(Body);
     }
     else
     {
         if (Translations != null && Translations.ContainsKey(Core.Utils.GetSystemLanguage()))
         {
             return(Translations[Core.Utils.GetSystemLanguage()]);
         }
         else
         {
             return(Body);
         }
     }
 }
 public string GetActiveMessage()
 {
     if (TranslationState == FizzChatCellTranslationState.Original)
     {
         return(Body);
     }
     else
     {
         string langCode = GetLanguageCode();
         if (Translations != null && Translations.ContainsKey(langCode))
         {
             return(Translations[langCode]);
         }
         else
         {
             return(Body);
         }
     }
 }
        private IDictionary <string, string> GetTranslations(params string[] keys)
        {
            var key = string.Empty;

            foreach (var keyPart in keys)
            {
                if (!string.IsNullOrEmpty(key))
                {
                    key += ".";
                }
                key += keyPart;
            }

            if (Translations.ContainsKey(key))
            {
                return(Translations[key]);
            }

            return(new Dictionary <string, string>());
        }
示例#16
0
        /// <summary>
        /// Translate a reference text
        /// </summary>
        public string Translate(string culture, string context, string reference)
        {
            string result = reference;

            try
            {
                var key = context + "\r" + reference;
                RepositoryTranslation myTranslation = Translations.ContainsKey(key) ? Translations[key] : null;
                if (myTranslation != null)
                {
#if DEBUG
                    myTranslation.Usage++;
#endif
                    if (!string.IsNullOrEmpty(culture) && myTranslation.Translations.ContainsKey(culture))
                    {
                        result = myTranslation.Translations[culture];
                        if (string.IsNullOrEmpty(result))
                        {
                            result = reference;
                        }
                    }
                }
                else
                {
#if DEBUG
                    if (!UnkownTranslations.Exists(i => i.Context == context && i.Reference == reference))
                    {
                        UnkownTranslations.Add(new RepositoryTranslation()
                        {
                            Context = context, Reference = reference
                        });
                    }
#endif
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(result);
        }
        public static string GetLocalizedString(string text, params object[] args)
        {
            var defaultTable = LocalizationSettings.Instance.GetStringDatabase().GetDefaultTableAsync().Result;

            if (defaultTable != null)
            {
                var entry = defaultTable.GetEntry(text);
                if (entry != null)
                {
                    var localizedTitle = entry.GetLocalizedString(args);
                    return(localizedTitle);
                }
            }

            if (Translations.ContainsKey(text) && Translations[text].ContainsKey(GetCurrentLocale()))
            {
                return(Translations[text][GetCurrentLocale()]);
            }

            return(text);
        }
        public static string Translate(string format, params object[] parameters)
        {
            object[] copyParameters = parameters;

            // For each passed object
            for (int i = 0; i < copyParameters.Length; i++)
            {
                // If object is boolean
                if (copyParameters[i] is bool)
                {
                    // Set object to translated string value of boolean
                    copyParameters[i] = (bool)copyParameters[i] ? Translate("True") : Translate("False");
                }
            }

            // If header/format pair exists
            if (Translations.ContainsKey(format))
            {
                // Return properly formatted translation
                return(string.Format(Translations[format], parameters));
            }

            // Join all parameters in one string separated by spaces
            string joined = string.Join(" ", parameters);

            // If joined is empty string
            if (joined == "")
            {
                // Set equal to format
                joined = format;
            }
            else
            {
                // Precede with format
                joined = format + " " + joined;
            }

            // Return header and parameters in format of fallback
            return(string.Format(BackupTranslationFormat, joined));
        }
示例#19
0
        public static string _(string textToTranslate)
        {
            var show = string.Empty;

            if (string.IsNullOrEmpty(textToTranslate))
            {
                return("");
            }
            var textToTranslateToLower = textToTranslate.ToLower();

            if (Translations.ContainsKey(textToTranslateToLower))
            {
                show = Translations[textToTranslateToLower];
            }
            else if (Translations.ContainsKey(textToTranslate))
            {
                show = Translations[textToTranslate];
            }
            else
            {
                show = textToTranslate;
            }
            return(show);
        }
示例#20
0
 /// <summary>
 ///     Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an element with the specified key.
 /// </summary>
 /// <returns>
 ///     true if the <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an element with the key; otherwise, false.
 /// </returns>
 /// <param name="key">
 ///     The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2" />.
 /// </param>
 /// <exception cref="T:System.ArgumentNullException">
 ///     <paramref name="key" /> is null.
 /// </exception>
 public bool ContainsKey(string key)
 {
     return(Translations.ContainsKey(key));
 }
 /// <summary>
 /// Easy translation with string formatting
 /// </summary>
 /// <param name="key">The translation key</param>
 /// <param name="data">The arguments for string formatting</param>
 /// <returns>The formatted message</returns>
 public string Translate(string key, params object[] data) => (Translations.ContainsKey(key) ? string.Format(Translations[key], data) : "#" + key);
示例#22
0
 public bool HasTranslation(string lang)
 {
     return(DefaultCultureInfo.Name == lang || (Translations.ContainsKey(lang) && Translations[lang].Translations.Count > 0));
 }