Globalization and localization - Portable Object localization

In this article

What is a PO file?

Example

#: Pages/Index.cshtml:13
msgid "Hello world!"
msgstr "Bonjour le monde!"

msgid "There is one item."
msgid_plural "There are {0} items."
msgstr[0] "Il y a un élément."
msgstr[1] "Il y a {0} éléments."

Configuring PO file support in ASP.NET Core

Referencing the package

<PackageReference Include="OrchardCore.Localization.Core" Version="1.5.0" />

Registering the service

builder.Services.AddPortableObjectLocalization();

builder.Services
    .Configure<RequestLocalizationOptions>(options => options
        .AddSupportedCultures("fr", "cs")
        .AddSupportedUICultures("fr", "cs"));

builder.Services
    .AddRazorPages()
    .AddViewLocalization();
@page
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
    ViewData["Title"] = "Home";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<p>@Localizer["Hello world!"]</p>

Creating a PO file

msgid "Hello world!"
msgstr "Bonjour le monde!"

Testing the application

Pluralization

Creating pluralization PO files

msgid "There is one item."
msgid_plural "There are {0} items."
msgstr[0] "Il y a un élément."
msgstr[1] "Il y a {0} éléments."

Adding a language using different pluralization forms

msgid "Hello world!"
msgstr "Ahoj světe!!"

msgid "There is one item."
msgid_plural "There are {0} items."
msgstr[0] "Existuje jedna položka."
msgstr[1] "Existují {0} položky."
msgstr[2] "Existuje {0} položek."
builder.Services
    .Configure<RequestLocalizationOptions>(options => options
        .AddSupportedCultures("fr", "cs")
        .AddSupportedUICultures("fr", "cs"));
<p>@Localizer.Plural(1, "There is one item.", "There are {0} items.")</p>
<p>@Localizer.Plural(2, "There is one item.", "There are {0} items.")</p>
<p>@Localizer.Plural(5, "There is one item.", "There are {0} items.")</p>
There is one item.
There are 2 items.
There are 5 items.
Il y a un élément.
Il y a 2 éléments.
Il y a 5 éléments.
Existuje jedna položka.
Existují 2 položky.
Existuje 5 položek.

Advanced tasks

Using additional arguments

<p>@Localizer.Plural(count, "There is one item with the color {1}.", "There are {0} items. The main color is {1}.", color)</p>

Contextualizing strings

msgctxt "Views.Home.About"
msgid "Hello world!"
msgstr "Bonjour le monde!"
msgid "Hello world!"
msgstr "Bonjour le monde!"

Changing the location of PO files

services.AddPortableObjectLocalization(options => options.ResourcesPath = "Localization");

Implementing a custom logic for finding localization files

Using a different default pluralized language

Ref: Configure portable object localization in ASP.NET Core