What is PO files?
PO stands for portable
objects, these are the files that containing translated text string each for
language. It has some advantages over .resx (resource files).
1. It Support
pluralization
2. PO files are not
compiled like .resx
Example:
en.po
#: 1. Hello World
msgid "Hello_World"
msgstr "Hello World!"
hi.po
#: 1. Hello World
msgid "Hello_World"
msgstr "संसार को नमस्ते"
Configure PO files for ASP.Net Core MVC:
To configure PO files we have to include package reference:
<PackageReference Include="OrchardCore.Localization.Core"
Version="1.0.0-rc1-10004" />
Now the time is to configure the services and middleware
Add these required configuration method in startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
// Put your .po files in Resources folder
services.AddPortableObjectLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("hi-IN")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseRequestLocalization();
}
Now add the following code on view:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer
Localizer
<p>@Localizer["Hello
world!"]</p>
Test the application by Navigate the URL:
/Home/About?culture=en-US
/Home/About?culture=hi-IN
Model Localization:
public class RoleDto
{
[Display(Name = "Name")]
[Required(ErrorMessage = "this_field_is_required")]
public string Name { get; set; }
}
en.po
#: 1. Name
msgid "Name"
msgstr "Name"
#: 2. This field is required
msgid "this_field_is_required"
msgstr "This field is required."
hi.po
#: 1. Nam
msgid "Name"
msgstr "नाम"
#: 2. This field is required
msgid "this_field_is_required"
msgstr "यह फ़ील्ड आवश्यक है।"
Nice
ReplyDelete