本地化和在Blazor Server中重定向

huangapple go评论74阅读模式
英文:

Localization and redirect in blazor Server

问题

I have added localization to my Blazor app to manage the auto-translation of my app's text. I made it using ApiController.

To be more clear:

On Program.cs, I have added:

builder.Services.AddControllers();
builder.Services.AddLocalization(x => x.ResourcesPath = "ResourcesFolder");

and

var localizeoptions = new RequestLocalizationOptions().SetDefaultCulture("it-IT")
    .AddSupportedCultures("it-IT", "en-US")
    .AddSupportedUICultures("it-IT", "en-US");

app.UseRequestLocalization(localizeoptions);
app.MapControllers();

On Index.razor:

@inject Microsoft.Extensions.Localization.IStringLocalizer<Index> localizer

and in the HTML, the auto-translate text:

<h2>@localizer["Customer"]</h2>

"Customer" is in every resx file (in my app, en-US, and it-IT translation).

The translation is managed using a controller for every language. Example for en-US:

[Route("/[controller]")]
[ApiController]
public class enUS : ControllerBase
{

    public ActionResult SetCulture()
    {
        IRequestCultureFeature culture = HttpContext.Features.Get<IRequestCultureFeature>();
        Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture("en-US")));

        return Redirect("/");
    }
}

This is working pretty well. If I open the web app with https://localhost:7058/enUS, it is in English, and with https://localhost:7058/itIT, it is in Italian.

The problem is that if I want to specify a parameter in the link (for example, https://localhost:7058/enUS/?link=OQ==) with the line of code return Redirect("/"); on the controller, I will be redirected to https://localhost:7058 with the English language. The parameter "link" is lost.

Is there a way to read the parameter inside the controller and then redirect to the correct link?

英文:

i have added localization to my blazor app to manage auto-traslation of my app's text.
I made it using Apicontroller.

To be more clear:

On Program.cs i have added:

    builder.Services.AddControllers();
    builder.Services.AddLocalization(x =&gt; x.ResourcesPath = &quot;ResourcesFolder&quot;);

and

var localizeoptions = new RequestLocalizationOptions().SetDefaultCulture(&quot;it-IT&quot;)
    .AddSupportedCultures(&quot;it-IT&quot;, &quot;en-US&quot;)
    .AddSupportedUICultures(&quot;it-IT&quot;, &quot;en-US&quot;);

app.UseRequestLocalization(localizeoptions);
app.MapControllers();

On Index.razor:

@inject Microsoft.Extensions.Localization.IStringLocalizer<Index> localizer

and in the html the auto translate text:

 &lt;h2&gt;@localizer[&quot;Customer&quot;]&lt;/h2&gt;

"Customer" is in every resx file ( in my app en-US e it-IT translation).

本地化和在Blazor Server中重定向

The traslation is managed using a controllor for every languge. Example for en-US:

[Route(&quot;/[controller]&quot;)]
[ApiController]
public class enUS : ControllerBase
{

    public ActionResult SetCulture()
    {
        IRequestCultureFeature culture = HttpContext.Features.Get&lt;IRequestCultureFeature&gt;();
        Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(&quot;en-US&quot;)));


        return Redirect(&quot;/&quot;);
    }
}

This is working pretty well. If i open the web app with https://localhost:7058/enUS is in english and with https://localhost:7058/itIT is in intalian.

The problem is that if i want to specify parameter in the link ( for example https://localhost:7058/enUS/?link=OQ== ) with the line code return Redirect(&quot;/&quot;); on the controller i will be redirected to https://localhost:7058 with english languange. The parameter "link" is lost.

There is a way to read parameter inside the controllor and then redirect to the correct link?

答案1

得分: 1

以下是您要翻译的内容:

如果您在重定向时传递查询字符串,就可以获取链接参数,像这样:

return Redirect("/" + Request.QueryString);

如果您检查Request属性,可以找到正确的URL并重定向用户。

但是,根据您当前的实现,这是一个只在访问https://localhost:7058/enUS时设置文化的端点。如果您想在路径中包含文化并通过URL更改它。例如像这样:https://www.microsoft.com/en-us/windows/。您必须创建一个BaseController,并让您的控制器继承它。例如:

[Route("/[controller]/{culture?}")]
[ApiController]
public class MyBaseController : Controller
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string culture = filterContext.RouteData.Values["culture"]?.ToString() ?? "en";

    var cultureInfo = CultureInfo.GetCultureInfo(culture);
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;

    Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName,
    CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)));

    base.OnActionExecuting(filterContext);
}

}

根据您的需求进行调整。或者也可以通过中间件来完成,并更改默认的路由,我没有尝试过,但在网上看到了很多示例。

英文:

You can get the link parameter if you pass the query string in the redirect, like this:

return Redirect(&quot;/&quot; + Request.QueryString);

If you check the Request property you can find the correct URL and redirect the user.

But with you current implementation this is an endpoint that only sets the culture if you go here: https://localhost:7058/enUS . If you want to have the culture in your path and change it via url. For example like this: https://www.microsoft.com/en-us/windows/ . You have to create a BaseController and have your controllers inherit from it. For example:

[Route(&quot;/[controller]/{culture?}&quot;)]
[ApiController]
public class MyBaseController : Controller
{
    public override void OnActionExecuting( ActionExecutingContext filterContext)
    {
        string culture = filterContext.RouteData.Values[&quot;culture&quot;]?.ToString() ?? &quot;en&quot;;

        var cultureInfo = CultureInfo.GetCultureInfo(culture);
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;

        Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)));

        base.OnActionExecuting(filterContext);
    }
}

And adjust it to your needs. Or it can also be done with a Middleware and change the default Routing, I haven't tried it but I see a lot of examples online.

huangapple
  • 本文由 发表于 2023年5月25日 17:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定