/// <inheritdoc/>
        public string GetString(string name, CultureInfo culture)
        {
            var cultureCode = culture.Name;

            //Try to get from original dictionary
            ILocalizationDictionary originalDictionary;

            if (_dictionaries.TryGetValue(cultureCode, out originalDictionary))
            {
                var strOriginal = originalDictionary.GetOrNull(name);
                if (strOriginal != null)
                {
                    return(strOriginal.Value);
                }
            }

            //Try to get from same language dictionary
            if (cultureCode.Length == 5) //Example: "tr-TR" (length=5)
            {
                var langCode = cultureCode.Substring(0, 2);
                ILocalizationDictionary langDictionary;
                if (_dictionaries.TryGetValue(langCode, out langDictionary))
                {
                    var strLang = langDictionary.GetOrNull(name);
                    if (strLang != null)
                    {
                        return(strLang.Value);
                    }
                }
            }

            //Try to get from default language
            if (_defaultDictionary == null)
            {
                throw new AbpException("Can not find '" + name + "' and no default language is defined!");
            }

            var strDefault = _defaultDictionary.GetOrNull(name);

            if (strDefault != null)
            {
                return(strDefault.Value);
            }

            throw new AbpException("Can not find a localized string for '" + name + "' in source '" + Name + "'!");
        }
        public string GetStringOrNull(string name, CultureInfo culture, bool tryDefaults = true)
        {
            string cultureName = culture.Name;
            IDictionary <string, ILocalizationDictionary> dictionaries = DictionaryProvider.Dictionaries;

            //Try to get from original dictionary (with country code)
            if (dictionaries.TryGetValue(cultureName, out ILocalizationDictionary originalDictionary))
            {
                LocalizedString strOriginal = originalDictionary.GetOrNull(name);
                if (strOriginal != null)
                {
                    return(strOriginal.Value);
                }
            }

            if (!tryDefaults)
            {
                return(null);
            }

            //Try to get from same language dictionary (without country code)
            if (cultureName.Contains("-")) //Example: "tr-TR" (length=5)
            {
                if (dictionaries.TryGetValue(GetBaseCultureName(cultureName), out ILocalizationDictionary langDictionary))
                {
                    LocalizedString strLang = langDictionary.GetOrNull(name);
                    if (strLang != null)
                    {
                        return(strLang.Value);
                    }
                }
            }

            //Try to get from default language
            ILocalizationDictionary defaultDictionary = DictionaryProvider.DefaultDictionary;

            LocalizedString strDefault = defaultDictionary?.GetOrNull(name);

            return(strDefault?.Value);
        }
Esempio n. 3
0
        public LocalizedString GetOrNull(int?tenantId, string name)
        {
            //Get cache
            var cache = _cacheManager.GetMultiTenantLocalizationDictionaryCache();

            //Get for current tenant
            var dictionary = cache.Get(CalculateCacheKey(tenantId), () => GetAllValuesFromDatabase(tenantId));
            var value      = dictionary.GetOrDefault(name);

            if (value != null)
            {
                return(new LocalizedString(name, value, CultureInfo));
            }

            //Fall back to host
            if (tenantId != null)
            {
                dictionary = cache.Get(CalculateCacheKey(null), () => GetAllValuesFromDatabase(null));
                value      = dictionary.GetOrDefault(name);
                if (value != null)
                {
                    return(new LocalizedString(name, value, CultureInfo));
                }
            }

            //Not found in database, fall back to internal dictionary
            var internalLocalizedString = _internalDictionary.GetOrNull(name);

            if (internalLocalizedString != null)
            {
                return(internalLocalizedString);
            }

            //TODO: Fallback to internal's default dictionary!

            //Not found at all
            return(null);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public string GetString(string name, CultureInfo culture)
        {
            var cultureCode = culture.Name;

            //Try to get from original dictionary (with country code)

            ILocalizationDictionary originalDictionary;

            if (_dictionaries.TryGetValue(cultureCode, out originalDictionary))
            {
                var strOriginal = originalDictionary.GetOrNull(name);
                if (strOriginal != null)
                {
                    return(strOriginal.Value);
                }
            }

            //Try to get from same language dictionary (without country code)

            if (cultureCode.Length == 5) //Example: "tr-TR" (length=5)
            {
                var langCode = cultureCode.Substring(0, 2);
                ILocalizationDictionary langDictionary;
                if (_dictionaries.TryGetValue(langCode, out langDictionary))
                {
                    var strLang = langDictionary.GetOrNull(name);
                    if (strLang != null)
                    {
                        return(strLang.Value);
                    }
                }
            }

            //Try to get from default language

            if (_defaultDictionary == null)
            {
                var exceptionMessage = string.Format(
                    "Can not find '{0}' in localization source '{1}'! No default language is defined!",
                    name, Name
                    );

                if (_configuration.ReturnGivenTextIfNotFound)
                {
                    LogHelper.Logger.Warn(exceptionMessage);
                    return(_configuration.WrapGivenTextIfNotFound
                        ? string.Format("[{0}]", name)
                        : name);
                }

                throw new AbpException(exceptionMessage);
            }

            var strDefault = _defaultDictionary.GetOrNull(name);

            if (strDefault == null)
            {
                var exceptionMessage = string.Format(
                    "Can not find '{0}' in localization source '{1}'!",
                    name, Name
                    );

                if (_configuration.ReturnGivenTextIfNotFound)
                {
                    LogHelper.Logger.Warn(exceptionMessage);
                    return(_configuration.WrapGivenTextIfNotFound
                        ? string.Format("[{0}]", name)
                        : name);
                }

                throw new AbpException(exceptionMessage);
            }

            return(strDefault.Value);
        }
Esempio n. 5
0
 public LocalizedString this[string name] => _localizationDictionary.GetOrNull(name);