RickStrahl / Westwind.Globalization

Database driven resource localization for .NET applications
544 stars 135 forks source link

How to change the language #133

Closed LearnGrowAndShare closed 5 years ago

LearnGrowAndShare commented 6 years ago

Hi,

I have a drop down which has language list.

I am not sure how to get it working.

I am using below package: Westwind.Globalization.AspnetCore v - 3.0.1

Using the below api to change the language

 // GET: api/<controller>
        [HttpPost]
        public void Get(string lang = "en")
        {
           // SetUserLocale(lang, lang, "INR", true, null, HttpContext);
            Westwind.Globalization.AspnetCore.Utilities.WebUtils.SetUserLocale(lang, lang, "$", true, null);
        }

Westwind service config

   private static void WestWindServices(IServiceCollection services)
        {
            // Standard ASP.NET Localization features are recommended
            // Make sure this is done FIRST!
            services.AddLocalization(options =>
            {
                // I prefer Properties over the default `Resources` folder
                // due to namespace issues if you have a Resources type as
                // most people do for shared resources.
                options.ResourcesPath = "Properties";
            });

            // Replace StringLocalizers with Db Resource Implementation
            services.AddSingleton(typeof(IStringLocalizerFactory),
                                  typeof(DbResStringLocalizerFactory));
            services.AddSingleton(typeof(IHtmlLocalizerFactory),
                                  typeof(DbResHtmlLocalizerFactory));

            // Required: Enable Westwind.Globalization (opt parm is optional)
            // shown here with optional manual configuration code
            services.AddWestwindGlobalization(opt =>
            {
                // the default settings comme from DbResourceConfiguration.json if exists
                // you can override the settings here, the config you create is added
                // to the DI system (DbResourceConfiguration)

                opt.ResourceSetValueConverters.Add(new MarkdownResourceSetValueConverter());

                // Resource Mode - from Database (or Resx for serving from Resources)
                opt.ResourceAccessMode = ResourceAccessMode.DbResourceManager;  // .Resx

                // Make sure the database you connect to exists
                opt.ConnectionString = "server=.;database=localizations;integrated security=true;";

                // Database provider used - Sql Server is the default
                opt.DataProvider = DbResourceProviderTypes.SqlServer;

                // The table in which resources are stored
                opt.ResourceTableName = "localizations";

                opt.AddMissingResources = false;
                opt.ResxBaseFolder = "~/Properties/";

                // Set up security for Localization Administration form
                opt.ConfigureAuthorizeLocalizationAdministration(actionContext =>
                {
                    // return true or false whether this request is authorized
                    return true;   //actionContext.HttpContext.User.Identity.IsAuthenticated;
                });

            });
        }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

           var supportedCultures = new[]
           {
                new CultureInfo("en-US"),
                new CultureInfo("en"),
                new CultureInfo("es")
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en-US"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            });

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Please help how to make it work properly.

And also any documentation is there how to integrate the JavaScript resource to it, so that i can try out the client side language change .

RickStrahl commented 5 years ago

Off topic.