开发者

Sitecore URL optimisation and languages

Working with multiple language sites in Sitecore 6.2 I'd like to be able to have Sitecore replace " " (empty space, traditionally replaced with %20) in URLs with a hyphen.

E.g. www.MySite.com/Hello World > www.MySite.com/Hello-World

So far no problem, we just add another entry to encodeNameReplacements

<replace mode="on" find=" " replaceWith="-" />

Then to prevent our content authors from creating items with a hyphen in their names (which Sitecore would understand as having a space when reading their URLs, resulting in a 404 Not Found) we can add the hyphen to the list of characters forbidden in item names, using

 <setting name="InvalidItemNameChars" value="\/:?&quot;&lt;&gt;|[]-"/>

This all works fine - until I try and create a new language definition. Languages in Sitecore can either use a simple language code (e.g. en, da, de etc) or a language and region code (ISO) such as en-GB, da-DK, de-DE etc. For our sites we will need the regional codes as we intend to support regional language variation (e.g. Swiss French and Swiss German). But of course I've now forbidden the hyphen in item names (and languages, as everything else in Sitecore, are defined as items). In fact Sitecore demands that a language name is properly constructed as a culture identifier, so I can't assign a language a descriptive name (e.g. American English) with the iSO 开发者_运维知识库code specified in the ISO code field of the template.

Can anyone suggest a suitable solution to this? Should I remove the hyphen from the InvalidItemNameChars node and write a custom code snippet that traps attempted creation of hyphenated item names i? If so, how should I interecpt the creation event?

I'm loathe to customise the interface where it isn't necessary, especially as I'm concerned this may create problems for Sitecore version updates, so if there are other solutions I'd love to hear them.

Thanks in advance.


This uses the rules engine to replace special characters such as spaces in item names:

https://marketplace.sitecore.net/en/Modules/Item_Naming_rules.aspx

If you didn't replace, but just translated, when processing URLs, Sitecore's item resolver would try to look for item names with dashes rather than spaces, and would not find matching items. If you don't actually replace the characters, then you would need to override the item resolver.

If you want to leave the names with spaces visible to the user, but actually name the items with dashes, then you could set the display name for the item to the original value with spaces when you rename the spaces to dashes, but I would recommend against that.


You can write a class and put it in the item saved event in the web.config. Just write the OnItemSaved method and have it check for the hyphen in the name. I guess you'd need to do it on rename and on copy as well.

<event name="item:saved">
  <handler type="Sitecore.Links.ItemEventHandler, Sitecore.Kernel" method="OnItemSaved" />
  <handler type="Sitecore.Tasks.ItemEventHandler, Sitecore.Kernel" method="OnItemSaved" />
  <handler type="Sitecore.Globalization.ItemEventHandler, Sitecore.Kernel" method="OnItemSaved" />
  <handler type="Sitecore.Rules.ItemEventHandler, Sitecore.Kernel" method="OnItemSaved" />
  <handler type="YourAssembly.ItemEventHandler, YourAssembly" method="OnItemSaved" />
</event>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜