示例#1
0
        /// <summary>
        ///     Finds the translation entry out of <paramref name="baseDictionary" /> for <paramref name="targetLanguage" />
        ///     and <paramref name="key" />, if no entry is found in <paramref name="targetLanguage" />, dictionaries of
        ///     compatible languages are searched. Returns default value (null) if no entry for
        ///     <paramref name="key" /> can be found in dictionaries campatible with <paramref name="targetLanguage" />.
        /// </summary>
        /// <param name="baseDictionary">Dictionary collection to be searched.</param>
        /// <param name="targetLanguage">CultureInfo with which the used dictionary has to be compatible.</param>
        /// <param name="key">Key to search for in available dictionarys.</param>
        /// <param name="inputLanguage">
        ///     The language the application was originally designed in. Used as a fallback.
        /// </param>
        /// <param name="useOnlyExactLanguage">If true, compatible languages and fallbacks are ignored.</param>
        /// <returns>
        ///     Value for <paramref name="key" /> out of dictionary for <paramref name="targetLanguage" />, its
        ///     parent (usually same as two letter name), its two letter name (e.g. en for en-US), its patents two
        ///     letter name, <see cref="CultureInfo.InvariantCulture" />, the <paramref name="inputLanguage" /> or
        ///     null if no compatible dictionary can be found in <paramref name="baseDictionary" />
        ///     (Dictionaries will be searched in this order).
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if either <paramref name="baseDictionary" />, <paramref name="targetLanguage" /> or
        ///     <paramref name="key" /> is null.
        /// </exception>
        public static string GetLanguageDictValueOrDefault(
            Dictionary <CultureInfo, Dictionary <string, string> > baseDictionary, CultureInfo targetLanguage, string key,
            CultureInfo inputLanguage, bool useOnlyExactLanguage)
        {
            //null checks.
            ExceptionLoggingUtils.VerifyMultiple(baseDictionary, nameof(baseDictionary))
            .AlsoVerify(targetLanguage, nameof(targetLanguage))
            .AlsoVerify(key, nameof(key))
            .AlsoVerify(inputLanguage, nameof(inputLanguage))
            .ThrowIfNull(Logger, nameof(GetLanguageDictValueOrDefault),
                         "Unable to pick dictionary with null parameter.");

            //searching baseDictionary.
            if (baseDictionary.ContainsKey(targetLanguage) && baseDictionary[targetLanguage].ContainsKey(key))
            {
                return(baseDictionary[targetLanguage][key]);
            }

            if (useOnlyExactLanguage)
            {
                return(null);
            }

            var parentCultureInfo = targetLanguage.Parent;

            if (baseDictionary.ContainsKey(parentCultureInfo) && baseDictionary[parentCultureInfo].ContainsKey(key))
            {
                return(baseDictionary[parentCultureInfo][key]);
            }

            var twoLetterTarget = new CultureInfo(targetLanguage.TwoLetterISOLanguageName);

            if (baseDictionary.ContainsKey(twoLetterTarget) && baseDictionary[twoLetterTarget].ContainsKey(key))
            {
                return(baseDictionary[twoLetterTarget][key]);
            }

            var twoLetterParent = new CultureInfo(parentCultureInfo.TwoLetterISOLanguageName);

            if (baseDictionary.ContainsKey(twoLetterParent) && baseDictionary[twoLetterParent].ContainsKey(key))
            {
                return(baseDictionary[twoLetterParent][key]);
            }

            if (baseDictionary.ContainsKey(CultureInfo.InvariantCulture) &&
                baseDictionary[CultureInfo.InvariantCulture].ContainsKey(key))
            {
                return(baseDictionary[CultureInfo.InvariantCulture][key]);
            }

            if (baseDictionary.ContainsKey(inputLanguage) && baseDictionary[inputLanguage].ContainsKey(key))
            {
                return(baseDictionary[inputLanguage][key]);
            }

            //default.
            return(null);
        }
        /// <summary>
        ///     Collectes translations of <paramref name="text" /> for <paramref name="targetLanguage" />
        ///     that share the same translation in <paramref name="inputLanguage" />.
        /// </summary>
        /// <param name="text">The text in <paramref name="inputLanguage" />.</param>
        /// <param name="targetLanguage">The language for which translations should be collected.</param>
        /// <param name="inputLanguage">The language of <paramref name="text" />.</param>
        /// <param name="allTranslations">
        ///     The collection of all translation to pull the needed translations from.
        /// </param>
        /// <returns>
        ///     All possible translations of <paramref name="text" /> into <paramref name="targetLanguage" />
        ///     based on which keys inside the inner Dictionary of <paramref name="allTranslations" /> share
        ///     the same translations in <paramref name="inputLanguage" />.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown, if <paramref name="targetLanguage" />, <paramref name="inputLanguage" /> or
        ///     <paramref name="allTranslations" /> is null.
        /// </exception>
        /// <exception cref="InputLanguageNotFoundException">
        ///     Thrown, if <paramref name="allTranslations" /> does not contain <paramref name="inputLanguage" />.
        /// </exception>
        public static IEnumerable <string> ExtractKnownTranslations(string text, CultureInfo targetLanguage,
                                                                    CultureInfo inputLanguage, Dictionary <CultureInfo, Dictionary <string, string> > allTranslations)
        {
            //null and argument checks.
            ExceptionLoggingUtils.VerifyMultiple(targetLanguage, nameof(targetLanguage))
            .AlsoVerify(inputLanguage, nameof(inputLanguage))
            .AlsoVerify(allTranslations, nameof(allTranslations))
            .ThrowIfNull(Logger, nameof(ExtractKnownTranslations),
                         "Unable to extract known translation for null parameter.");

            ExceptionLoggingUtils.ThrowIfInputLanguageMissing(Logger, allTranslations.Keys.ToList(), inputLanguage,
                                                              "InputLanguage is not part of languages collection.",
                                                              "Unable to generate translation recommendations for Localizaions without InputLanguage.");

            allTranslations.TryGetValue(inputLanguage, out var sourceDictionary);

            ICollection <string> knownTranslations = new Collection <string>();

            if (string.IsNullOrWhiteSpace(text) || Equals(targetLanguage, inputLanguage))
            {
                //no Exceptions should be thrown here.
                return(knownTranslations);
            }

            //get all entries out of sourceDictionary, where value matches given text
            var fittingDictionaryEntries =
                sourceDictionary.Where(x => text.Equals(x.Value));

            //collect possible translations
            foreach (var entry in fittingDictionaryEntries)
            {
                var value = "";
                allTranslations.TryGetValue(targetLanguage, out var langDict);
                langDict?.TryGetValue(entry.Key, out value);

                //do not recommend the same translation twice
                if (!string.IsNullOrWhiteSpace(value) && !knownTranslations.Contains(value))
                {
                    knownTranslations.Add(value);
                }
            }

            return(knownTranslations);
        }