In a recent build we had a request from the client to automatically set the language of the content editor based on where in the tree they were viewing. The rules for selecting the language were quite simple:
– sitecore
— Content
— English site – field with value for default language set to be ‘en’
— French site – field with value for default language set to be ‘fr-fr’
If the user was making changes within the English site, they wanted the Content Editor to default to ‘en’. If they were editing within the French site, they wanted the Content Editor to default to ‘fr-fr’.
With the help of support, the following solution was suggested:
- Create your own class inherited from Sitecore.Shell.Applications.ContentManager.ContentEditorForm
- Add a new method:
12345678910111213141516171819202122232425262728public void ChangeLanguage(string id){Error.AssertString(id, "id", false);if (ShortID.IsShortID(id)){id = ShortID.Decode(id);}//get the selected itemItem item = Sitecore.Configuration.Factory.GetDatabase(Context.ContentDatabase.ToString()).GetItem(id);if (item != null){//get the current site rootItem currentSiteRoot = item.Axes.GetAncestors().FirstOrDefault(a => a.TemplateID == new ID(new Guid(....)));if (currentSiteRoot != null){//if the current language doesnt match the expected language, set the languageLanguage siteLanguage = Language.Parse(currentSiteRoot.Fields["...."].Value);if (ContentEditorDataContext.Language != siteLanguage){ContentEditorDataContext.Language = siteLanguage;}}}} - Edit \sitecore\shell\Applications\Content Manager\Default.aspx, changing the sc:CodeBeside reference to point to your new class:
1<sc:CodeBeside runat="server" Type="YourNamespase.YourClass,YourAssembly" />
Note, you need to keep the CodeBeside reference on one line as per the out the box version (the other controls are eg DataContext, RegisterKey etc) - Edit \sitecore\shell\Applications\Content Manager\Content editor.js, adding a call to your new method. This needs to happen in the onTreeNodeClick method after LoadItem.
1234567scContentEditor.prototype.onTreeNodeClick = function (sender, id) {...scForm.postRequest("", "", "", "LoadItem(\"" + id + "\")");//new call to ChangeLanguagescForm.postRequest("", "", "", "ChangeLanguage(\"" + id + "\")");...}
Pingback: Language settings in Sitecore | Sitecore Martina