sitecoresitecore6

Rename language after item is created


I'm using sitecore 6.5 with two languages installed, en (default) and fr-CA. There are items in the tree with content in both en and fr-CA.

The problem is that the French url has 'fr-CA' in it and we want that to be 'fr', for example:

http://website.com/fr/page.aspx instead of http://website.com/fr-CA/page.aspx

I tried renaming the language from 'fr-CA' to 'fr' and that fixed the url but the content still points to the old language 'fr-CA', so the item shows three languages: en, fr and fr-CA. It's not recognizing the name change.

Any suggestions are much appreciated.

Thanks, Tarek


Solution

  • The problem is you have created fr-CA versions of your items which cannot be fixed by renaming the language .. you can now make a fr version but, like you are seeing, this means there are now 3 possible versions.

    One suggestion is to leave the languages in Sitecore alone and alter how links are served and processed instead.

    You would probably need to look at adding your own method into the httpRequestBegin pipeline in Sitecore. This would follow the LanguageResolver entry. You can then parse the RawUrl and set Sitecore.Context.Langauge' to French if the first element in it matched/fr/`.

    Extremely quick & dirty example:

    public class MyLanguageResolver : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            string languageText = WebUtil.ExtractLanguageName(args.Context.Request.RawUrl);
    
            if(languageText == "fr")
            {
                Sitecore.Context.Language = LanguageManager.GetLanguage("fr-CA");
            }
        }
    }
    

    You would probably also have to override the LinkProvider in the <linkManager> section of the web.config to format your URLs when they are resolved by Sitecore.

    Another extremely quick & dirty example:

    public class MyLinkProvider : LinkProvider
    {
        public override string GetItemUrl(Sitecore.Data.Items.Item item, UrlOptions options)
        {
            var url = base.GetItemUrl(item, options);
            url = url.Replace("/fr-CA/", "/fr/");
            return url;
        }    
    }
    

    Another way (slightly more long-winded as it will need to be executed via a script) is to copy the data from the fr-CA version to the fr version and then delete the fr-CA version of each item.

    Rough helper method that encompasses what you're trying to do

        private void CopyLanguage(ID id, Language sourceLanguage, Language destinationLanguage)
        {
            var master = Database.GetDatabase("master");
    
            var sourceLanguageItem = master.GetItem(id, sourceLanguage);
            var destinationLanguageItem = master.GetItem(id, destinationLanguage);
    
            using (new SecurityDisabler())
            {
                destinationLanguageItem.Editing.BeginEdit();
    
                //for each field in source, create in destination if it does not exist
                foreach (Field sf in sourceLanguageItem.Fields)
                {
                    if (sf.Name.Contains("_")) continue;
    
                    destinationLanguageItem.Fields[sf.Name].Value = sf.Value;
                }
    
                destinationLanguageItem.Editing.AcceptChanges();
    
                ////Remove the source language version
                ItemManager.RemoveVersions(sourceLanguageItem,sourceLanguage, SecurityCheck.Disable);
            }
        }
    

    Another way to update the languages on your content items is:

    1. Export the fr-CA language to a .xml file (Using the Control Panel)
    2. In the .xml file replace all and tags with the and
    3. Rename fr-CA language in the master database to the fr
    4. Import language from the .xml file
    5. Run Clean Up Databases task (from the Control Panel)

    Also you can create a sql script that will change fr-CA language with the fr for all records in the UnversionedFields and VersionedFields tables.

    If you need any more information or examples please let me know. :)