Dotnet core projelerinde resource dosyası kullanımı

 

Aşağıdaki şekilde giriş yapabiliriz
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        #region Localization
        // REMARK: you may refactor this into a separate method as it's better to avoid long methods with regions
        var supportedCultures = new[]
        {
            new CultureInfo(defaultCultureName),
            new CultureInfo("pl-PL")
        };
        var localizationOptions = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture(defaultCultureName, defaultCultureName),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures,
            // you can change the list of providers, if you don't want the default behavior
            // e.g. the following line enables to pick up culture ONLY from cookies
            RequestCultureProviders = new[] { new CookieRequestCultureProvider() }
        };
        app.UseRequestLocalization(localizationOptions);
        #endregion

        app.UseStaticFiles();

        app.UseMvc(ConfigureRoutes);
    }
Sonra bu şekilde devam edelim
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult SetCulture(string culture, string returnUrl)
{
    HttpContext.Response.Cookies.Append(
        CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
        // making cookie valid for the actual app root path (which is not necessarily "/" e.g. if we're behind a reverse proxy)
        new CookieOptions { Path = Url.Content("~/") });

    return Redirect(returnUrl);
}
Enson dosyayı kullanacağımız yerde yapacağımız işler
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Http.Extensions

@{
    var httpContext = ViewContext.HttpContext;
    var currentCulture = httpContext.Features.Get<IRequestCultureFeature>().RequestCulture.UICulture;
    var currentUrl = UriHelper.BuildRelative(httpContext.Request.PathBase, httpContext.Request.Path, httpContext.Request.QueryString);
}
<form asp-action="SetCulture" method="post">
    Culture: <input type="text" name="culture" value="@currentCulture">
    <input type="hidden" name="returnUrl" value="@currentUrl">
    <input type="submit" value="Submit">
</form>
Program.cs içinde
builder.Services.AddLocalization();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]
    {
        new CultureInfo("tr-TR"),
        new CultureInfo("en-US")
    };
    options.DefaultRequestCulture = new RequestCulture(culture: "tr-TR", uiCulture: "tr-TR");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;

});
builder.Services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat
                     .Suffix).AddDataAnnotationsLocalization();
Kaynak link

 https://stackoverflow.com/questions/51785051/mvc-core-set-application-culture-from-controller-action 
https://www.yogihosting.com/globalization-localization-resource-files-aspnet-core/

Yorumlar

Bu blogdaki popüler yayınlar

22.06.2020 - 26.06.2020 arası işler

Asp.net RestSharp ile data post etmek

List Box Item içindeki elemanları aşağı veya yukarı taşımak