private static bool TryTranslateOrThrow(
            ResourceManager resourceManager,
            string key,
            CultureInfo culture,
            ErrorHandling errorHandling,
            out string result)
        {
            var shouldThrow = ShouldThrow(errorHandling);

            if (resourceManager == null)
            {
                if (shouldThrow)
                {
                    throw new ArgumentNullException(nameof(resourceManager));
                }

                result = string.Format(Properties.Resources.NullManagerFormat, key);
                return(false);
            }

            if (string.IsNullOrEmpty(key))
            {
                if (shouldThrow)
                {
                    throw new ArgumentNullException(nameof(key));
                }

                result = "key == null";
                return(false);
            }

            if (culture != null &&
                !CultureInfoComparer.DefaultEquals(culture, CultureInfo.InvariantCulture) &&
                cultures?.Contains(culture, CultureInfoComparer.Default) == false)
            {
                if (resourceManager.HasCulture(culture))
                {
                    if (cultures == null)
                    {
                        cultures = new SortedSet <CultureInfo>();
                    }

                    cultures.Add(culture);
                }
                else
                {
                    if (shouldThrow)
                    {
                        var message = $"The resourcemanager {resourceManager.BaseName} does not have a translation for the culture: {culture.Name}";
                        throw new ArgumentOutOfRangeException(nameof(culture), message);
                    }

                    var trnslated = resourceManager.GetString(key, culture);
                    if (!string.IsNullOrEmpty(trnslated))
                    {
                        result = string.Format(Properties.Resources.MissingCultureFormat, trnslated);
                        return(false);
                    }

                    result = string.Format(Properties.Resources.MissingCultureFormat, key);
                    return(false);
                }
            }

            var translated = resourceManager.GetString(key, culture);

            if (translated == null)
            {
                if (shouldThrow)
                {
                    var message = $"The resourcemanager {resourceManager.BaseName} does not have the key: {key}";
                    throw new ArgumentOutOfRangeException(nameof(key), message);
                }

                result = string.Format(Properties.Resources.MissingKeyFormat, key);
                return(false);
            }

            if (translated == string.Empty)
            {
                if (!resourceManager.HasKey(key, culture))
                {
                    if (shouldThrow)
                    {
                        var message = $"The resourcemanager {resourceManager.BaseName} does not have a translation for the key: {key} for the culture: {culture.Name}";
                        throw new ArgumentOutOfRangeException(nameof(key), message);
                    }

                    result = string.Format(Properties.Resources.MissingTranslationFormat, key);
                    return(false);
                }
            }

            result = translated;
            return(true);
        }
Example #2
0
 internal static bool IsInvariant(this CultureInfo culture)
 {
     return(CultureInfoComparer.DefaultEquals(culture, CultureInfo.InvariantCulture));
 }