/// <summary> /// on first invoke load in page cache all labels (labelsList) /// </summary> /// <param name="resourceSet">eg: MYSITE_PAGENAME</param> /// <param name="resourceId">eg: PageTitle</param> /// <param name="defaultValue">eg: MY web site --> on first call insert label in db with defaultValue</param> /// <returns>label value</returns> public string GetLabel( string resourceSet, string resourceId, string defaultValue, ContentEditorProvider.Configuration.EditorTypeEnum textMode = ContentEditorProvider.Configuration.EditorTypeEnum.Text, string forcedCultureCode = "") { if (string.IsNullOrEmpty(resourceSet)) throw new ArgumentException("empty resourceSet"); if (string.IsNullOrEmpty(resourceId)) throw new ArgumentException("empty resourceId"); string res = ""; try { if (!labelsList.ContainsKey(resourceSet)) { //preload all labels of current moduletype var labels = LabelsProvider.GetLabelsByResourceSet(resourceSet); labelsList.Add(resourceSet, labels); } res = LabelsProvider.GetLocalizedLabelFromList( resourceSet, labelsList[resourceSet], resourceId, defaultValue, textMode, forcedCultureCode); if (string.IsNullOrEmpty(res)) { res = defaultValue; } if (HttpContext.Current.Request.QueryString["tp"] == "1") { res = "[" + resourceId + "]" + res; } } catch (System.Data.SqlClient.SqlException ex) { Response.Redirect(Config.InstallationPath); } return res; }
/// <summary> /// automatically insert label in labels using CultureDev /// </summary> /// <returns>operation done sucessfully</returns> private static bool insertDefaultValue( string resourceSet, string resourceId, string defaultValue, ContentEditorProvider.Configuration.EditorTypeEnum textMode) { bool res = false; try { var man = new LabelsManager(); var filter = new LabelsFilter(); filter.ResourceSet = resourceSet; filter.ResourceId = resourceId; filter.CultureName = Config.CultureDev; var l = man.GetByFilter(filter, ""); if (l.Count == 0) { var o1 = new ResLabel(); o1.ResourceSet = resourceSet; o1.ResourceId = resourceId; o1.CultureName = Config.CultureDev; o1.Value = defaultValue; o1.Comment = "SYSTEM"; o1.TextMode = textMode; //insert default value for current culture man.Insert(o1); Tracer.Log("LabelsProvider.GetLocalizedLabelFromList()>Insert new label[" + resourceSet + "|" + resourceId + "|" + o1.CultureName + "|" + o1.TextMode.ToString() + "]=" + defaultValue, TracerItemType.Debug); res = true; } } catch (Exception ex) { Tracer.Log("LabelsProvider.GetLocalizedLabelFromList()>Insert new label ERR[" + resourceSet + "|" + resourceId + "|" + Config.CultureDefault + "]=" + defaultValue + " ERR:" + ex.ToString(), TracerItemType.Error); } return res; }
/// <summary> /// retrieve label result from cached list (in page or control var) /// try in order to retrieve: /// - current culture value /// - default culture value /// - defaultValue --> (firts request: insert in db) /// </summary> /// <param name="resourceSet"></param> /// <param name="labelsList"></param> /// <param name="resourceId"></param> /// <param name="defaultValue"></param> /// <param name="textMode">default=EditorTypeEnum.Text</param> /// <returns>the label value</returns> public static string GetLocalizedLabelFromList( string resourceSet, List<ResLabel>labelsList, string resourceId, string defaultValue, ContentEditorProvider.Configuration.EditorTypeEnum textMode, string forcedCultureCode) { string res = ""; //20150512 if (string.IsNullOrEmpty(forcedCultureCode)) forcedCultureCode = Utility.GetCurrCultureName(); if (labelsList != null) { //find the right value in labelsList try { //try current culture res = labelsList.Find( delegate(ResLabel labelToFind) { if (labelToFind.ResourceId.ToLower() == resourceId.ToLower() && labelToFind.CultureName.ToLower() == forcedCultureCode.ToLower()) return true; else return false; }).Value; //added 20150701 //try default culture if (string.IsNullOrEmpty(res) && Config.CultureDefault.ToLower() != forcedCultureCode.ToLower()) { try { res = labelsList.Find( delegate(ResLabel labelToFind) { if (labelToFind.ResourceId.ToLower() == resourceId.ToLower() && labelToFind.CultureName.ToLower() == Config.CultureDefault.ToLower()) return true; else return false; }).Value; } catch (NullReferenceException) { res = ""; } } } catch (NullReferenceException) { //try default culture if (Config.CultureDefault.ToLower() != forcedCultureCode.ToLower()) { try { res = labelsList.Find( delegate(ResLabel labelToFind) { if (labelToFind.ResourceId.ToLower() == resourceId.ToLower() && labelToFind.CultureName.ToLower() == Config.CultureDefault.ToLower()) return true; else return false; }).Value; } catch (NullReferenceException) { res = ""; } } //##20140519 //auto insert new label with default value for current culture if (string.IsNullOrEmpty(res) && !string.IsNullOrEmpty(defaultValue) && !string.IsNullOrEmpty(resourceSet)) { if (insertDefaultValue(resourceSet, resourceId, defaultValue, textMode)) { //cause label cache reload ClearCacheByResourceSet(resourceSet); } } } } return res; }