emrah aktas
emrah aktas

Reputation: 45

Asp.net core Localization in Razor Pages

How do I change the position of a string or html snippet to suit the language when displaying an html snippet or script in another language in razor

for example:

"Flights to Antalya for a price starting from €39.99"

would be change to

"€39,99’dan başlayan fiyatlarla Antalya uçuşları"

translating from turkish to english : their position will change

Upvotes: 3

Views: 1336

Answers (4)

diegosasw
diegosasw

Reputation: 15684

You mainly need 2 things:

  1. A automated way to set the user's current culture in an http requestso that CultureInfo.CurrentCulture contains the desired language + region (a.k.a culture)
  2. A way to read translated text from a resource file (resx) according to the specified culture

Things like Dates, for example, are automatically formatted according to the current culture.

[This is an article about localization with AspNet Core Razor Pages and/or MVC]to understand the process (https://sunnyatticsoftware.com/blog/webs-multilingue-localizacion-mvc-razor-pages) it is in Spanish (hopefully you can have the browser translate it in a good enough way), but at the end it shows how to achieve this with Razor Pages in C#.

Upvotes: 0

LazZiya
LazZiya

Reputation: 5729

just use formatted string in the related resource file:

resources.en.resx:

"Flights to Antalya for a price starting from {0}"

or resources.tr.resx:

"{0}’dan başlayan fiyatlarla Antalya uçuşları"

Then you can use the default IStringLocalizer as below:

_localizer["{0}’dan başlayan fiyatlarla Antalya uçuşları", price]

Upvotes: 2

ibrahimatay
ibrahimatay

Reputation: 57

I think, you can use Culture and UICulture. Every request set the language in layout.

Exp;

@{
    Layout = "~/_Layout.cshtml";
    if(!Request["lang"].IsEmpty()){
        Culture = UICulture = Request["lang"];
    }
}

Language dependent define for use ToString(iformatter).

Exp;

Currency: @(10000.ToString("c")

FR For: £

EN For: €

Upvotes: 1

emrah aktas
emrah aktas

Reputation: 45

I find from somewhere

ClientResources.en.resx

After you can take instance from the reference on the page

R = Settings.Resources !== undefined ? Settings.Resources : {},

you can use when needed

if (totalPassengerCount > maximumPassengerCount) { alert(R.MaximumPassengerCount);

Upvotes: 1

Related Questions