简体   繁体   中英

Localization on _Layout.cshtml

I'm trying to apply localization on _Layout.cshtml

_Layout.cshtml

@using Microsoft.AspNetCore.Http.Extensions
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization

@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
@inject IViewLocalizer LayoutLocalizer


<!DOCTYPE html>
<html>
<head>

</head>
    <body>
        <div style="margin:38px;margin-top:56px;" class="hidden-sm-down">
            <h2>@LayoutLocalizer["WelcomeHeader"] <b class="text_border_darkblue">MyTitle</b></h2>
            <div style="text-align:center;margin-top:42px;">
                @LayoutLocalizer["WelcomeSub"]
            </div>
        </div>
    </body>
</html>

The resource files are named named: Views.Shared._Layout.no.resx (I've tried renaming to views.shared.Layout.no.resx without any luck)

Solution

Implement SharedResources with resource files and use Localization on the SharedResources object in shared Views

You can combine IViewLocalizer and IHtmlLocalizer for _Layout.cshtml . If you define a resource folder inside your Visual Studio solution you can put the resource files there.

If you are using razor pages instead of MVC views inside your ASP.net core application you create a subfolder Pages instead of Views inside the folder Resources. Put the _Layout.no.resx file in there. eg Resources\\Pages\\_Layout.no.resx .

You must create a _Layout.no.resx file within the same path of the views but as a root the "Resources" folder. See sample image:

在此输入图像描述

There is a video clarifying the use of Location see here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM