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 from nuget

PM > Install-Package XLocalizer.TagHelpers


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...

Localize Html

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

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

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[] { "", 8, "Asp.Net Core" }

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

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

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.

See demo page.

Traditional localization for views

Default localization interfaces can be used to localize view contents:

@inject IStringLocalizer _localizer


Next: Localizing DataAnnotations