LazZiya / XLocalizer

Localizer package for Asp.Net Core web applications, powered by online translation and auto resource creating.
https://docs.ziyad.info
128 stars 14 forks source link

Set Arabic as default request culture #39

Closed faresayyad closed 1 year ago

faresayyad commented 1 year ago

Hello,

I am trying to make the primary language(Default request): Arabic, The code in ConfigureServices is the following:

    string defCulture = configuration.GetSection("Defaults").GetSection("DefalutLang").Value.Trim();
            var cultures = CultureHelper.GetArrOfCultureInfo(configuration);

            services.Configure<RequestLocalizationOptions>(ops =>
            {
                ops.DefaultRequestCulture = new RequestCulture(defCulture);
                ops.SupportedCultures = cultures;
                ops.SupportedUICultures = cultures;

                ops.RequestCultureProviders.Clear();
                ops.RequestCultureProviders.Insert(0, new RouteSegmentRequestCultureProvider(cultures));

            });

    services.AddControllersWithViews()
             .AddRazorRuntimeCompilation()
             .AddMvcOptions(ops => ops.Conventions.Insert(0, new RouteTemplateModelConventionMvc()))
             .AddRazorPagesOptions(ops => ops.Conventions.Insert(0, new RouteTemplateModelConventionRazorPages()))
             .AddXLocalizer<LocSource>(ops =>
             {
                 ops.ResourcesPath = "LocalizationResources";
             });

            services.AddMvc().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });

The defCulture variable is: ar and the cultures variable is an array of two values {'ar','en'}

In the Configure method:

app.UseResponseCompression();
            app.UseHttpsRedirection();
            app.UseStaticFiles(new StaticFileOptions
            {
                ServeUnknownFileTypes = false,
                OnPrepareResponse = ctx =>
                {
                    const int durationInSeconds = 60 * 60 * 24;
                    ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds;
                    ctx.Context.Response.Headers[HeaderNames.Expires] = new[] { DateTime.UtcNow.AddYears(1).ToString("R") }; // Format RFC1123
                }
            });
            app.UseCookiePolicy();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseSession();
            app.UseRequestLocalization();

//Endpoint
 string defCulture = configuration.GetSection("Defaults").GetSection("DefalutLang").Value.Trim();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                                     name: "default",
                                     pattern: "{culture=" + defCulture + "}/{controller=Home}/{action=Index}/{id?}",
                                     defaults: new { culture = defCulture, controller = "Home", action = "Index" });

                endpoints.MapRazorPages();
            });

In the view i have the following:

@inject IOptions<RequestLocalizationOptions> LocOps

@{

    var requestCulture = CultureInfo.CurrentCulture;

When debugging the code with breakpoint the variable requestCulture value is : ar

but the issue is with the values read from resx file, such as: @_loc.GetString("Search") the search name and value exist in the \LocalizationResources folder within LocSource.ar.resx file.

the value Search(as an example) is still in English, it's not translated into Arabic.!!?

faresayyad commented 1 year ago

Is this unexpected behavior regarding using .Net core 6 ??

LazZiya commented 1 year ago

Hi @faresayyad ,

When you set a culture as default culture, XLocalizer assumes that the default texts are written with the default culture, so it doesn't localize the default culture by default.

e.g.:

<!-- if default culture is ar -->
<!-- in the view it is expected to have the texts in Arabic -->
<h1 localize-content>مرحبا</h1>

But if you have these texts in a culture different than the default culture, "en" for exmple; then you can tell XLocalizer to localize default culture as well:

services.AddControllersWithViews()
             .AddXLocalizer<LocSource>(ops =>
             {
                 ...
                 ops.LocalizeDefaultCulture = true;
             });