Esempio n. 1
0
        /// <summary>
        /// Helper method
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private string Localize(string name)
        {
            // Just in case 2 threads with 2 different cultures invoked the same localizer concurrently
            // (It probably never happens, but I have seen signs that some MVC libraries might do that)
            lock (_translationsLock)
            {
                // This is to workaround the behavior of certain MVC libraries
                // which seem to re-use the same IStringLocalizer across multiple requests,
                // even though the current UI culture is a scoped value and the localizer is transient
                if (_translations == null || (_culture ?? CultureInfo.CurrentUICulture).Name != _translations.CultureName)
                {
                    _translations = _factory.GetTranslations(_culture);
                }

                // Go over the dictionaries one by one and return the first hit
                foreach (var translationDictionary in _translations.InDescendingOrderOfPrecedence())
                {
                    if (translationDictionary != null && translationDictionary.ContainsKey(name))
                    {
                        return(translationDictionary[name]);
                    }
                }

                // If all is a miss, return the name as is, forgiveness here is a virtue.
                return(name);
            }
        }