コード例 #1
0
        /// <summary>
        /// Данная функция только инициализирует локализаторы
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        private bool TrySetCulture(string culture)
        {
            StringLocalizerWithCache LC = null, L = null;

            if (LocalizerControllerOriginal != null)
            {
                Logger.LogTrace("===================== LocalizerController LoadCulture");
                LC = LocalizerControllerOriginal.LoadCulture(culture, false, Logger);
            }

            if (LocalizerOriginal != null)
            {
                Logger.LogTrace("===================== Localizer LoadCulture");
                L = LocalizerOriginal.LoadCulture(culture, true, Logger);
            }
            // один из локализаторов должен существовать
            if (L != null || LC != null)
            {
                // это влияет тока на параметры форматирования
                var cultureInfo = new CultureInfo(culture);
                CultureInfo.DefaultThreadCurrentCulture   = cultureInfo;
                CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

                CultureInfo.CurrentCulture   = cultureInfo;
                CultureInfo.CurrentUICulture = cultureInfo;
                /**/
                Localizer           = L;
                LocalizerController = LC;
                return(true);
            }

            return(false);
        }
コード例 #2
0
        public Localizer2Garin(ILoggerFactory loggerFactory, IStringLocalizerFactory localizerFactory, IStringLocalizer localizer, IOptions <LocalizerConfigure> LocalizerOptionsAccessor)
        {
            LoggerFactory = loggerFactory;
            Logger        = LoggerFactory.CreateLogger(this.GetType().FullName);
            Logger.LogTrace("Localizer::Constructor");
            LocalizerFactory = localizerFactory;
            DefaultCulture   = LocalizerOptionsAccessor.Value.DefaultCulture;

            var type = typeof(SharedResource);

            LocalizerOriginal = LocalizerFactory.Create(type);
            //Localizer = LocalizerOriginal;
            //try { Localizer = StringLocalizerWithCache.Get(LocalizerOriginal, "k1"); } catch { }

            LocalizerControllerOriginal = localizer;
            //LocalizerController = LocalizerControllerOriginal;
            //try { LocalizerController = StringLocalizerWithCache.Get(LocalizerControllerOriginal, "k2"); } catch { }

            if (string.IsNullOrWhiteSpace(DefaultCulture))
            {
                DefaultCulture = DefaultLibraryCulture;
            }

            if (LocalizerOriginal != null)
            {
                Logger.LogTrace("------------LocalizerDefault LoadCulture");
                // установка дефолтного локализатора из библиотеки
                if (LibraryCultures.Contains(DefaultCulture))
                {
                    LocalizerDefault = LocalizerOriginal.LoadCulture(DefaultCulture, true, Logger);
                }
                else
                {
                    LocalizerDefault = LocalizerOriginal.LoadCulture(DefaultLibraryCulture, true, Logger);
                }
            }

            if (LocalizerControllerOriginal != null)
            {
                Logger.LogTrace("------------LocalizerControllerDefault LoadCulture");
                LocalizerControllerDefault = LocalizerControllerOriginal.LoadCulture(DefaultCulture, false, Logger);
            }
        }