예제 #1
0
 public AppTextBridge(
     IServiceProvider serviceProvider,
     IMemoryCache cache,
     IOptions <AppTextLocalizationOptions> options,
     ILogger <AppTextBridge> logger
     )
 {
     _options         = options.Value;
     _cache           = cache;
     _logger          = logger;
     _serviceProvider = serviceProvider;
     _cacheKeys       = new HashSet <string>();
 }
예제 #2
0
        /// <summary>
        /// Adds AppText as localization implementation for standard .NET Core Localization functionality. When added to an application, you can use the
        /// standard .NET Core localization facilities (IStringLocalizer, IViewLocalizer), but with AppText as content backend.
        /// </summary>
        /// <param name="appTextBuilder">AppText components builder</param>
        /// <param name="setupAction">Configures AppText.Localization options</param>
        /// <returns></returns>
        public static AppTextBuilder AddAppTextLocalization(this AppTextBuilder appTextBuilder, Action <AppTextLocalizationOptions> setupAction = null)
        {
            var services = appTextBuilder.Services;

            services.AddSingleton <IStringLocalizerFactory, AppTextStringLocalizerFactory>();
            services.AddSingleton <IHtmlLocalizerFactory, AppTextHtmlLocalizerFactory>();
            services.AddSingleton <AppTextBridge>();
            services.TryAddTransient(typeof(IStringLocalizer <>), typeof(StringLocalizer <>));

            services.AddScoped <IEventHandler <ContentItemChangedEvent>, ContentItemChangedEventHandler>();
            services.AddScoped <IEventHandler <AppChangedEvent>, AppChangedEventHandler>();

            // Register for IOptions<AppTextLocalizationOptions>
            if (setupAction != null)
            {
                services.Configure(setupAction);
            }

            // Enable Translations module for our own translations.
            appTextBuilder.AddTranslations();

            // Add initializer as hosted service
            appTextBuilder.Services.AddHostedService <LocalizationInitializer>();

            // Configure RequestLocalizationOptions
            var enrichOptions = setupAction ?? delegate { };
            var options       = new AppTextLocalizationOptions();

            enrichOptions(options);

            if (options.ConfigureRequestLocalizationOptions)
            {
                services.AddOptions <RequestLocalizationOptions>()
                .Configure <IServiceScopeFactory>((locOptions, serviceScopeFactory) =>
                {
                    using (var scope = serviceScopeFactory.CreateScope())
                    {
                        var applicationStore = scope.ServiceProvider.GetRequiredService <IApplicationStore>();
                        var app = AsyncHelper.RunSync(() => applicationStore.GetApp(App.SanitizeAppId(options.AppId)));
                        locOptions.SupportedUICultures   = app.Languages.Select(lang => new CultureInfo(lang)).ToList();
                        locOptions.DefaultRequestCulture = new RequestCulture(CultureInfo.CurrentCulture, new CultureInfo(app.DefaultLanguage));
                    }
                });
            }

            return(appTextBuilder);
        }
예제 #3
0
 public AppChangedEventHandler(AppTextBridge appTextBridge, IOptions <AppTextLocalizationOptions> options, IHostApplicationLifetime hostApplicationLifetime)
 {
     _appTextBridge           = appTextBridge;
     _options                 = options.Value;
     _hostApplicationLifetime = hostApplicationLifetime;
 }
 public ContentItemChangedEventHandler(IContentStore contentStore, AppTextBridge appTextBridge, IOptions <AppTextLocalizationOptions> options)
 {
     _appTextBridge = appTextBridge;
     _options       = options.Value;
     _contentStore  = contentStore;
 }