Translate the SiteTree contextmenu

Translating the SiteTree contextmenu - a fix (hack)

In versions 2.4.x the SiteTree context menu in the CMS can't be translated, and since it's not a fatal bug, SilverStripe is probably not going to fix it prior to version 3.0. A fix involves som hacking, but I find it worthwile:

 

/cms/javascript/CMSMain_left.js (line 663)

This is where the array for the contextmenu is created. Originally, in the TreeContextMenu array, SilverStripe uses the same text for key as wel as value. I replaced them with more 'translatable' symbols (EditPage, DuplicatePageAndChildren, ...)

TreeContextMenu = {
    'EditPage' : function(treeNode) {
        treeNode.selectTreeNode();
    },
    'DuplicatePageAndChildren' : function(treeNode) {
        // First save the page silently (without confirmation) and then 
        // duplicate the page.
        autoSave(false, treeNode.duplicatePageWithChildren.bind(treeNode));
    },
    'DuplicatePageOnly' : function(treeNode) {
        // First save the page silently (without confirmation) and then 
        // duplicate the page.uplicate the page.
        autoSave(false, treeNode.duplicatePage.bind(treeNode));
    },
    'SortChildpages' : function(treeNode) {
        var children = treeNode.treeNodeHolder().childTreeNodes();
        var sortedChildren = children.sort(function(a, b) {
            var titleA = a.aTag.innerHTML.replace(/<[^>]*>/g,'');
            var titleB = b.aTag.innerHTML.replace(/<[^>]*>/g,'');
            return titleA < titleB ? -1 : (titleA > titleB ? 1 : 0);
        });
 
        var i,child;
        for(i=0;child=sortedChildren[i];i++) {
            treeNode.appendTreeNode(child);
        }
         
        treeNode.onOrderChanged(sortedChildren,treeNode);
    }
};
 

/cms/javascript/LeftAndMain.js (around line 825)

This is where the array above is used to build the context menu 819. I replaced the menuItemName, where it is used for the InnerHTML of the contextmenu, by a translated i18n_menuItemName version:

for(menuItemName in menuItems) {
    menuItemTag = document.createElement("li");
 
    menuATag = document.createElement("a");
    menuATag.href = "#";
    menuATag.onclick = menuATag.oncontextmenu = contextmenu_onclick;
 
	// Don't use this:
	// menuATag.innerHTML = menuItemName;
 
	// Use a translation
	i18n_menuItemName = ss.i18n._t('CONTEXTMENU.' + menuItemName, menuItemName);
	menuATag.innerHTML = i18n_menuItemName;
 
	menuATag.handler = menuItems[menuItemName];
	menuATag.owner = owner;
 
	menuItemTag.appendChild(menuATag);
	menu.appendChild(menuItemTag);
}

/cms/javascript/lang/en_US.js

Finally add the translations to the language file:

'CONTEXTMENU.EditPage': 'Edit this page',
'CONTEXTMENU.DuplicatePageAndChildren': 'Duplicate page and children',
'CONTEXTMENU.DuplicatePageOnly': 'Duplicate just this page',
'CONTEXTMENU.SortChildpages': 'Sort sub-pages'

Comments

Het versturen van reacties is uitgeschakeld.

RSS feed voor reacties op deze pagina