3

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

1 Answers1

3

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.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks for replying, but using App_GlobalResources is not an option in MVC and I didn't get it to work when the resx file is in a normal folder. – Andreas Rudischhauser Dec 03 '13 at 10:51
  • What do you mean it is not an option in MVC? You just need to add the folder and it will work. – NightOwl888 Dec 03 '13 at 16:46
  • FYI - I found the solution for the publish command. You need to set build action to "Embedded", "Do Not Copy" to output directory, and set the custom tool to "PublicResXFileCodeGenerator" and custom tool namespace to "Resources": http://stackoverflow.com/questions/16789961/localization-in-asp-net-mvc-4-using-app-globalresources – NightOwl888 Dec 03 '13 at 17:19
  • 1
    Hm, i tried that. My resources file is called SiteMapRes.resx and is now located under App_GlobalResources (Embedded Resource, Do not copy, Resources). I access them in the MVC.sitemap file with title="$resources:SiteMapRes,Home". This configuration works locally! After publishing on the server the following error appears "The resource object with classname 'SiteMapRes' an key 'Home' was not found. – Andreas Rudischhauser Dec 04 '13 at 07:44
  • I don't know what to tell you. Clearly, this is a deployment problem, not a problem with MvcSiteMapProvider. Personally, I use MSDeploy and MSBuild to ensure the files are compiled and copied correctly (but admittedly, that is quite a bit more involved than Publish from VS). – NightOwl888 Dec 04 '13 at 11:35
  • Don't use app global resources folder on mvc. Strange to see u miss directing the op from a good solution to a bad one. Read some articles about app globalresources folder in mvc. It's adviced to not use it since it breaks things. – Frederik Prijck Jul 17 '14 at 03:58
  • 1
    Thanks for the advice. I would appreciate if you could provide a link or 2 to this info. The current implementation is based on Microsoft's SiteMapProvider design for ASP.NET (as was V3), but I didn't realize its global resources design is not supposed to be used for MVC. What is the recommended alternative? Also, what "good solution" are you referring to? App_GlobalResources is the only option discussed here, as it is currently the only option to localize a single SiteMap in MvcSiteMapProvider. – NightOwl888 Jul 17 '14 at 22:21