Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net mvc - How to localize the MvcSiteMapProvider

I tried to localize the MvcSiteMapProvider in almost any way, but I don't get it. As I figured out it works when you use app_globalresources folder, but that is not an option, if you work with MVC because MVC does not support them (at least the publish feature does not publish the files).

Currently my resource file for the web site is in a file called words.resx (and words.de-DE.resx) which is located under a new folder called Resources. I've set the build action to "Embedded Resource", "Copy always" and changed the tool to PublicResXFileCodeGenerator.

For the SiteMap, i don't care if it is implicit or explicit localization.... I tried both and used different namings of the resx files and different settings for the custom tool, but none of them worked.

Did anyone got this to work?

Kind regards Andi

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

NOTE: An alternative approach to putting your resources in the App_GlobalResources folder (which isn't supported very well by MVC) would be to implement IStringLocalizer.

Localization has remained largely unchanged from Microsoft's SiteMapProvider, which MvcSiteMapProvider was originally based on. For localization, you can still follow the How to: Localize Site-Map Data document for the most part.

Enabling localization is done in the MvcSiteMapProvider_EnableLocalization setting in appSettings, or if using external DI, you would set it in your DI configuration by passing the setting to the enableLocalization constructor parameter of SiteMapBuilderSet. It is enabled by default.

I recommend using explicit localization, as implicit localization requires some hoops to get working and is likely to change in a future version.

The fields that are localizable are:

  • Title
  • Description
  • ImageUrl
  • Attributes (that are string datatype)

Here is an explicit localization example from the MvcMusicStore demo, which has localization used on certain nodes.

<mvcSiteMapNode title="$resources:SiteMapLocalizations,BrowseGenresTitle" controller="Store" action="Index"/>

It is referring to a file named SiteMapLocalizations.resx (and an item named BrowseGenresTitle) in the App_GlobalResources folder.

Alternatively, you can use multiple sitemaps in the same application for each locale which enables a way to have a different site structure (and different URLs) per locale.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...