Пример #1
0
        /// <summary>
        /// Retrieving a string from resources using the UI culture of the current thread.
        /// By default, does not read strings from resources for the default language.
        /// </summary>
        /// <param name="prefix">Key prefix.</param>
        /// <param name="source">The text to be translated or the key for which we are looking for localization.</param>
        /// <param name="forceReadResources">If false - resources for default culture will not be read. Instead, the string
        /// passed as source will be returned. If source is a key and not the desired text, true must be set.</param>
        /// <param name="arguments">Arguments to substitute in the string format.</param>
        public string Get(string prefix, string source, bool forceReadResources = false, params object[] arguments)
        {
            var currentLanguage = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower();

            if (!String.IsNullOrEmpty(source))
            {
                string result;
                if (!forceReadResources && DefaultLanguage.Equals(currentLanguage, StringComparison.OrdinalIgnoreCase))
                {
                    result = source;
                }
                else
                {
                    result = CachedStrings
                             .GetOrAdd(
                        currentLanguage,
                        (lang) => new ConcurrentDictionary <string, string>())
                             .GetOrAdd(
                        ResourceKeyGenerator.Generate(prefix, source),
                        (key) => ResourceManager.GetString(key) ?? source);
                }

                if (arguments.Length == 0)
                {
                    return(result);
                }

                return(String.Format(result, arguments));
            }

            return(String.Empty);
        }
Пример #2
0
        /// <summary>
        /// Retrieving a string containing HTML from resources corresponding to the UI culture of the current thread.
        /// </summary>
        /// <param name="prefix">Key prefix.</param>
        /// <param name="source">The key for which we are looking for localization.</param>
        /// <param name="arguments">Substitution arguments.</param>
        public LocalizedHtmlString GetHtml(string prefix, string source, params object[] arguments)
        {
            var currentLanguage = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower();

            if (!String.IsNullOrEmpty(source))
            {
                var resourceKey = ResourceKeyGenerator.Generate(prefix, source);
                var result      = CachedStrings
                                  .GetOrAdd(
                    currentLanguage,
                    (lang) => new ConcurrentDictionary <string, string>())
                                  .GetOrAdd(
                    resourceKey,
                    (key) => ResourceManager.GetString(key) ?? source);
                return(new LocalizedHtmlString(resourceKey, result, false, arguments));
            }

            return(new LocalizedHtmlString(String.Empty, String.Empty));
        }