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?
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?
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.
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);
}
}
}
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
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
Hey Richard, Yes please that sounds super useful and should help alot of package devlopers out.
Thanks
Warren
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.
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?
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
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
is working on a reply...