Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Dec 10, 2013 @ 16:51
    Warren Buckley
    0

    Creating new Sections - Can we auto generate dictionary item?

    Hello all, I am working on a new package that creates a new section using the V6+ SectionsService API.

    Is there a smart way for the section for the name of the section to appear without the [sectionAlias] in the users section to enable it for the user and when the section is loaded in the backoffice the root node of the tree also shows as [sectionAlias]

    So come on smart people is there anyway we can deal with this?

    Thanks,

    Warren

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Dec 10, 2013 @ 17:35
    Richard Soeteman
    0

    Hi Warren,

    Those section items are stored in the language xml files. if you search for the alias sections () You can add your key in this area.

    I've created a simple LanguageInstaller class in my packages that I execute when installing packages that have a custom section to automatically update the language files for all language files. I can share that if you like?

    Cheers,

    Richard

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Dec 10, 2013 @ 17:58
    Warren Buckley
    0

    Hey Richard, Yes please that sounds super useful and should help alot of package devlopers out.

    Thanks

    Warren

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Dec 14, 2013 @ 11:30
    Warren Buckley
    0

    Hi Richard, Do you still have the code snippet for this please. I have done it with the following code but would like to see how you achieved it.

         public void AddSectionLanguageKeys()
        {
            bool saveFile = false;
    
            //Open up language file
            //umbraco/config/lang/en.xml
            var langPath = "~/umbraco/config/lang/en.xml";
    
            //Path to the file resolved
            var langFilePath = HostingEnvironment.MapPath(langPath);
    
            //Load settings.config XML file
            XmlDocument langXml = new XmlDocument();
            langXml.Load(langFilePath);
    
            // Section Node
            // <area alias="sections">
            XmlNode sectionNode = langXml.SelectSingleNode("//area [@alias='sections']");
    
            if (sectionNode != null)
            {
                XmlNode findSectionKey = sectionNode.SelectSingleNode("./key [@alias='analytics']");
    
                if (findSectionKey == null)
                {
                    //Let's add the key
                    var attrToAdd       = langXml.CreateAttribute("alias");
                    attrToAdd.Value     = "analytics";
    
                    var keyToAdd        = langXml.CreateElement("key");
                    keyToAdd.InnerText  = "Analytics";
                    keyToAdd.Attributes.Append(attrToAdd);
    
                    sectionNode.AppendChild(keyToAdd);
    
                    //Save the file flag to true
                    saveFile = true;
                }
            }
    
            // Section Node
            // <area alias="treeHeaders">
            XmlNode treeNode = langXml.SelectSingleNode("//area [@alias='treeHeaders']");
    
            if (treeNode != null)
            {
                XmlNode findTreeKey = treeNode.SelectSingleNode("./key [@alias='analytics']");
    
                if (findTreeKey == null)
                {
                    //Let's add the key
                    var attrToAdd       = langXml.CreateAttribute("alias");
                    attrToAdd.Value     = "analytics";
    
                    var keyToAdd        = langXml.CreateElement("key");
                    keyToAdd.InnerText  = "Analytics";
                    keyToAdd.Attributes.Append(attrToAdd);
    
                    treeNode.AppendChild(keyToAdd);
    
                    //Save the file flag to true
                    saveFile = true;
                }
            }
    
    
            //If saveFile flag is true then save the file
            if (saveFile)
            {
                //Save the XML file
                langXml.Save(langFilePath);
            }
        }
    
  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Dec 14, 2013 @ 13:59
    Warren Buckley
    0

    Thinking aloud now... Should this be an Umbraco Service much like Trees & Sections now are in V7+ which seem to be reading, adding & removing items from these XML files.

    So it surely makes sense to do the same for the Language File/s.

    Thoughts?

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Dec 14, 2013 @ 14:09
    Warren Buckley
    0

    If anyone agrees with my idea about a new serivce API, I have added a feature request on the issue tracker to vote up please

    http://issues.umbraco.org/issue/U4-3863

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Dec 15, 2013 @ 07:39
    Richard Soeteman
    1

    Hi Warren,

    Totally forgot about this. I'll just paste my code here. When installing a package and on application start I'll execute the CheckAndInstallLanguageActions. This method will check if the language key exists and if not install it. Not only for the english version but also for all other language files

    using System;
    using System.IO;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Logging;
    using umbraco;
    
    namespace MediaSecurity.Core.Installer
    {
    /// <summary>
    /// Checks and/or installs language settings on first request
    /// </summary>
    public  class LanguageInstaller
    {
        private static bool _executed;
    
        /// <summary>
        /// We need to add the text label on the actions otherwise they don;t appear on the context menu,
        /// Check each label and if not in the Umbraco langua file, add it to the actions node
        /// </summary>
        public static void CheckAndInstallLanguageActions()
        {
            if (!_executed)
            {
                InstallLanguageKey(Constants.LanguageFileAreas.General, Constants.LanguageFileKeys.DialogIntro, Constants.TextResources.DialogIntro);
                InstallLanguageKey(Constants.LanguageFileAreas.Actions, Constants.LanguageFileKeys.MediaSecurityMenuAction  ,Constants.TextResources.MediaSecurityMenuAction);
                _executed = true;
            }
        }
    
        private static bool KeyMissing(string area, string key)
        {
            return ui.GetText(area, key) == string.Format("[{0}]", key);
        }
    
        /// <summary>
        /// Loop through the language config folder and add language nodes to the language files
        /// If the language is not in our language file install the english variant.
        /// </summary>
        private static void InstallLanguageKey(string area, string key, string value)
        {
            if (KeyMissing(area, key))
            {
                var directory = HttpContext.Current.Server.MapPath(FormatUrl("/config/lang"));
                var languageFiles = Directory.GetFiles(directory);
    
                foreach (var languagefile in languageFiles)
                {
                    try
                    {
                        //Strip 2digit langcode from filename
                        var langcode = languagefile.Substring(languagefile.Length - 6, 2).ToLower();
                        UpdateActionsForLanguageFile(string.Format("{0}.xml", langcode), area, key, value);
    
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error<LanguageInstaller>("MediaSecurity Error in language installer",ex);
                    }
                }
            }
        }
    
        /// <summary>
        /// Update a language file withe the language xml
        /// </summary>
        private static void UpdateActionsForLanguageFile(string languageFile, string area, string key, string value)
        {
            var doc = XmlHelper.OpenAsXmlDocument(string.Format("{0}/config/lang/{1}", GlobalSettings.Path, languageFile));
            var actionNode = doc.SelectSingleNode(string.Format("//area[@alias='{0}']", area));
    
            if (actionNode != null)
            {
                var node = actionNode.AppendChild(doc.CreateElement("key"));
                if (node.Attributes != null)
                {
                    var att = node.Attributes.Append(doc.CreateAttribute("alias"));
                    att.InnerText = key;
                }
                node.InnerText = value;
            }
    
            doc.Save(HttpContext.Current.Server.MapPath(string.Format("{0}/config/lang/{1}", GlobalSettings.Path, languageFile)));
        }
    
        /// <summary>
        /// Returns the url with the correct Umbraco folder
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        private static string FormatUrl(string url)
        {
            return VirtualPathUtility.ToAbsolute(GlobalSettings.Path + url);
        }
    
    }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft