meta info

Localizing Views

By Ziya Mollamahmut

XLocalizer has a handy extension pack; XLocalizer.TagHelpers for localizing views using a simple html tag.

Table of contents

Install

Install from nuget

PM > Install-Package XLocalizer.TagHelpers

Setup

Localize views using LocalizeTagHelper, first it must be added to `ViewImports.cshtml

@addTagHelper *, XLocalizer.TagHelpers

Localize texts

Localize texts/html contents using localize-content attribute with any html tag

<h1 localize-content>Sample header</h1>

<p localize-content>
    sample content...
</p>

Localize Html

Use <localize> html tag or to localize all its contents

<localize>
    <h1>Sample title</h1>
    <p>sample contents...</p>
<localize>

Localize Select Items

Use <localize-select> html tag to localize select items texts.

<localize-select asp-items="Model.MyItems"></localize-select>

Requires XLocalizer.TagHelpers v1.1.0 or later.

Localize attributes

Localize attributes like title inside <img tag using localize-att-*

<img src="/images/picture.jpg" localize-att-title="Picture title"/>

Provide arguments for localized contents

Localize a string that contains arguments by providing the arguments using localize-args

@{
    var args = new object[] { "http://demo.ziyad.info/en/localize", 8, "Asp.Net Core" }
}

<p localize-args="args">
    Visit <a href="{0}">demos website</a> to see a collection of {1} demos about "{2}".
</p>

Localized output sample: Visit demos website to see a collection of 8 demos about "Asp.Net Core".

Specify culture

Force specific culture regardless the request culture

<p localize-culture="tr">
    This content will always be localized with TR culture
</p>

Specify resource source

Specify localization resource type using localize-source

<p localize-source="typeof(MyResourceType)">
    This content will be localized from a specific shared resource.
</p>

See demo page.

Traditional localization for views

Default localization interfaces can be used to localize view contents:

@inject IStringLocalizer _localizer

<h1>@_localizer["Welcome"]</h1>