/////////////////////////////////////////////////////////// // Bool.cs // Implementation of Class LocalizedTextTable // CSLA type: Read-only object // Created on: 29-Aug-2005 // Object design: John // Coded: 29-Aug-2005 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; using System.Collections; using System.Collections.Generic; namespace GZTW.AyaNova.BLL { /// /// Holds the localized text for use by user interface /// /// This is the class you want to use if you want to display /// localized text in your code and this is where you want to /// access it from: /// [Serializable] public class LocalizedTextTable : ReadOnlyBase { #region Attributes private Dictionary mLocalizedTextTable; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private LocalizedTextTable() { mLocalizedTextTable = new Dictionary(1400);//as of writing dec 15 2010 it's 1387 so this should give some leeway } #endregion #region Business properties //case 3379 - datadump [System.ComponentModel.Browsable(false)] public Dictionary LT { get { return mLocalizedTextTable; } } /// /// Returns key with the most characters in it /// Used for development purposes /// /// public string GetLongestKey() { string sLongest=""; int nLongest=0; foreach(KeyValuePair kvp in mLocalizedTextTable) { string sTemp = kvp.Key; if(sTemp.Length>nLongest) { nLongest=sTemp.Length; sLongest=sTemp; } } return sLongest + " " + nLongest.ToString() + " chars."; } #if (xDEBUG) //case 1543 private static System.Collections.Generic.List lsKeysUsed = null; public static System.Collections.Generic.List KeysUsed { get { if (lsKeysUsed == null) lsKeysUsed = new System.Collections.Generic.List(); return lsKeysUsed; } } private static void AddKeyUsed(string s) { if(!KeysUsed.Contains(s)) KeysUsed.Add(s); } #else #endif /// /// returns the localized text for the key provided /// from the in-memory Hashtable /// If key not found, returns key back /// /// /// public string GetLocalizedText(string LocaleKey)//case 1543 logpoint { if (mLocalizedTextTable.Count < 1) return LocaleKey; //bad or missing LocaleKey? if(string.IsNullOrWhiteSpace(LocaleKey)) return ""; //Is it in underscore notation style? if(LocaleKey.StartsWith("LT_")) { LocaleKey=LocaleKey.Replace("LT_","").Replace("_","."); } //Case 58 //Previously users could localize the RegionID values of User, client and head office //now that regionid is actually used it should always be the value of O.Region displayed //also there are a few new objects that now have a .RegionID field so rather than add them all //redundantly since they should display the same thing, just going to override it right here... if (LocaleKey.EndsWith(".RegionID")) return mLocalizedTextTable["O.Region"]; //Try to get localized text... string s = null; if(mLocalizedTextTable.ContainsKey(LocaleKey)) s=mLocalizedTextTable[LocaleKey]; //Localized text not found for provided key? if(s==null) { //"Common"-ize it and see if that returns the //localized text or not string AltKey=LocaleKey; int x=AltKey.IndexOf("."); if(x!=-1) { AltKey=AltKey.Substring(x); AltKey="Common" + AltKey; if(mLocalizedTextTable.ContainsKey(AltKey)) s=mLocalizedTextTable[AltKey]; if (s != null) { #if (xDEBUG) AddKeyUsed(s); #endif return s;//Yup, that did it, return localized text } else return LocaleKey;//Nope, still not found so return LocaleKey provided instead } else return LocaleKey;//No dot (.) found, not a valid key, send it back } //Localized text was found for the key provided so return it #if (xDEBUG) AddKeyUsed(s); #endif return s; } /// /// Get the localized text key from the localized string /// used to reverse lookup enum values in grid filters /// /// A string of localized text /// Fragment of key to avoid clashes with two keys that localize to the same text /// The key for the string provided or an empty string if nothing found that matches public string GetLocalizedTextKey(string hint, string localized) { //changed: 10-Nov-2006: Added Hint string and code to //ensure that localized text string matches don't falsely match //if an unrelated key has the same text //Done this way to avoid major changes to fragile grid filtering //code that is currently working. string probable = ""; foreach (KeyValuePair kvp in mLocalizedTextTable) { string sTemp = kvp.Value; if (sTemp==localized) { probable = kvp.Key; if(kvp.Key.Contains(hint)) return kvp.Key; } } return probable; } #endregion #region Static methods /// /// Load the localized text for the local provided /// /// Text key of locale i.e. "English", "Francais", "Espanol", "Klingon" etc /// public static LocalizedTextTable Load(string Locale) { return (LocalizedTextTable)DataPortal.Fetch(new Criteria( Locale)); } /// /// Given a LocalizedText key queries the database and returns the localized text /// This is used by the biz objects for reporting errors and for /// retrieving object names for reporting purposes /// It is not used by the UI which has a copy of this entire object instatiated for the /// lifetime of the UI code /// /// This is necessary because in a Remote DataPortal situation /// the biz objects at the server will not have access to an in-memory copy of the entire localized text table. /// /// (note that this method subcontracts this call to a dataPortal friendly fetcher class /// /// /// /// internal static string GetLocalizedTextDirect(string Key)//case 1543 logpoint { //part of case 53 but generally better performance usage here if (AyaBizUtils.LocaleText != null) return AyaBizUtils.LocaleText.GetLocalizedText(Key); else { string s=LocalizedTextFetcher.GetItem(Key).Text; #if (xDEBUG) AddKeyUsed(s); #endif return s; } } /// /// Given a LocalizedText key and Locale /// queries the database and returns the localized text string for that key and locale /// This is used by the biz objects for reporting errors and for /// retrieving object names for reporting purposes /// It is not used by the UI which has a copy of this entire object instatiated for the /// lifetime of the UI code /// /// This is necessary because in a Remote DataPortal situation /// the biz objects at the server will not have access to an in-memory copy of the entire localized text table. /// /// (note that this method subcontracts this call to a dataPortal friendly fetcher class /// /// /// /// /// internal static string GetLocalizedTextDirect(string Key, string Locale)//case 1543 logpoint { string s = LocalizedTextFetcher.GetItemForSpecificLocale(Key, Locale).Text; #if (xDEBUG) AddKeyUsed(s); #endif return s; } #endregion #region DAL DATA ACCESS /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { DBCommandWrapper dbCommandWrapper = DBUtil.DB.GetSqlStringCommandWrapper( "SELECT aKey, aDisplayText, aDisplayTextCustom FROM " + "aLocalizedText WHERE (aLocale = @Locale)" ); dbCommandWrapper.AddInParameter("@Locale",DbType.String,crit.Locale); dr = new SafeDataReader(DBUtil.DB.ExecuteReader(dbCommandWrapper)); mLocalizedTextTable.Clear(); while(dr.Read()) { //******************************************* //If there is no custom text then add the stock text if(dr.GetString("aDisplayTextCustom").Length<1) mLocalizedTextTable.Add(dr.GetString("aKey"),dr.GetString("aDisplayText")); else//It's custom, add the custom text instead mLocalizedTextTable.Add(dr.GetString("aKey"),dr.GetString("aDisplayTextCustom")); //******************************************* } //Add specialty keys here mLocalizedTextTable.Add("UI.Label.CurrentUserName", Thread.CurrentPrincipal.Identity.Name); } finally { if(dr!=null) dr.Close(); } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public string Locale; public Criteria(string _Locale) { Locale=_Locale; } } #endregion }//end Class }//end Namespace