/// <summary> /// Creates a strongly typed resource class for a ResoureSet from the DbResourceManager. /// /// Note: Uses the default ResourceProvider settings as set in the DbResourceConfiguration.Current /// property for opening the database and otherwise setting values. /// </summary> /// <param name="ResourceSetName">The name of the resource set. Should be a GLOBAL resource</param> /// <param name="Namespace">The namespace for the generated class. Null or string.Empty to not generate a namespace</param> /// <param name="Classname">Name of the class to be generated</param> /// <param name="FileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns>string of the generated class</returns> public string CreateClassFromDatabaseResource(string ResourceSetName, string Namespace, string Classname, string FileName) { // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet ResourceSet = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); return(CreateClassFromResourceSet(ResourceSet, Namespace, Classname, FileName)); }
/// <summary> /// Localization helper function that Translates a resource /// Id to a resource value string. Easy access that allows full /// control over the resource to retrieve or default UiCulture /// locale retrieval. /// </summary> /// <param name="resId">The Resource Id to retrieve /// Note resource Ids can be *any* string and if no /// matching resource is found the id is returned. /// </param> /// <param name="resourceSet">Name of the ResourceSet that houses this resource. If null or empty resources are used.</param> /// <param name="lang">5 letter or 2 letter language ieetf code: en-US, de-DE or en, de etc.</param> /// <returns> /// Localized resource or the resource Id if no match is found. /// This value *always* returns a string unless you pass in null. /// </returns> public static string T(string resId, string resourceSet = null, string lang = null) { if (string.IsNullOrEmpty(resId)) { return(resId); } if (resourceSet == null) { resourceSet = string.Empty; } // check if the res manager exists DbResourceManager manager; ResourceManagers.TryGetValue(resourceSet, out manager); // if not we have to create it and add it to static collection if (manager == null) { lock (ResourceManagers) { ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { manager = new DbResourceManager(resourceSet); ResourceManagers.Add(resourceSet, manager); manager.AutoAddMissingEntries = AutoAddResources; } } } // no manager no resources if (manager == null) { return(resId); } CultureInfo ci; if (string.IsNullOrEmpty(lang)) { ci = CultureInfo.CurrentUICulture; } else { ci = new CultureInfo(lang); } string result = manager.GetObject(resId, ci) as string; if (string.IsNullOrEmpty(result)) { return(resId); } return(result); }
/// <summary> /// Generates a strongly typed assembly from the resources /// /// UNDER CONSTRUCTION. /// Doesn't work correctly for Web forms due to hard coded resource managers. /// </summary> /// <param name="ResourceSetName"></param> /// <param name="Namespace"></param> /// <param name="Classname"></param> /// <param name="FileName"></param> /// <returns></returns> public bool CreateStronglyTypedResource(string ResourceSetName, string Namespace, string Classname, string FileName) { try { //wwDbResourceDataManager Data = new wwDbResourceDataManager(); //IDictionary ResourceSet = Data.GetResourceSet("", ResourceSetName); // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet rs = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IDictionaryEnumerator Enumerator = rs.GetEnumerator(); // We have to turn into a concret Dictionary Dictionary <string, object> Resources = new Dictionary <string, object>(); while (Enumerator.MoveNext()) { DictionaryEntry Item = (DictionaryEntry)Enumerator.Current; Resources.Add(Item.Key as string, Item.Value); } string[] UnmatchedElements; CodeDomProvider CodeProvider = null; string FileExtension = Path.GetExtension(FileName).TrimStart('.').ToLower(); if (FileExtension == "cs") { CodeProvider = new Microsoft.CSharp.CSharpCodeProvider(); } else if (FileExtension == "vb") { CodeProvider = new Microsoft.VisualBasic.VBCodeProvider(); } CodeCompileUnit Code = StronglyTypedResourceBuilder.Create(Resources, ResourceSetName, Namespace, CodeProvider, false, out UnmatchedElements); StreamWriter writer = new StreamWriter(FileName); CodeProvider.GenerateCodeFromCompileUnit(Code, writer, new CodeGeneratorOptions()); writer.Close(); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(false); } return(true); }
/// <summary> /// Localization helper function that Translates a resource /// Id to a resource value string. Easy access that allows full /// control over the resource to retrieve or default UiCulture /// locale retrieval. /// </summary> /// <param name="resId">The Resource Id to retrieve /// Note resource Ids can be *any* string and if no /// matching resource is found the id is returned. /// </param> /// <param name="resourceSet">Name of the ResourceSet that houses this resource. If null or empty resources are used.</param> /// <param name="lang">5 letter or 2 letter language ieetf code: en-US, de-DE or en, de etc.</param> /// <returns> /// Localized resource or the resource Id if no match is found. /// This value *always* returns a string unless you pass in null. /// </returns> public static string T(string resId, string resourceSet = null, string lang = null) { if (string.IsNullOrEmpty(resId)) return resId; if (resourceSet == null) resourceSet = string.Empty; // check if the res manager exists DbResourceManager manager; ResourceManagers.TryGetValue(resourceSet, out manager); // if not we have to create it and add it to static collection if (manager == null) { lock (ResourceManagers) { ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { manager = new DbResourceManager(resourceSet); ResourceManagers.Add(resourceSet, manager); manager.AutoAddMissingEntries = AutoAddResources; } } } // no manager no resources if (manager == null) return resId; CultureInfo ci; if (string.IsNullOrEmpty(lang)) ci = CultureInfo.CurrentUICulture; else ci = new CultureInfo(lang); string result = manager.GetObject(resId, ci) as string; if (string.IsNullOrEmpty(result)) return resId; return result; }
/// <summary> /// Localization helper function that Translates a resource /// Id to a resource value object. Use this function if you're /// retrieving non-string values - for string values just use T. /// </summary> /// <param name="resId">The Resource Id to retrieve /// Note resource Ids can be *any* string and if no /// matching resource is found the id is returned. /// </param> /// <param name="resourceSet">Name of the ResourceSet that houses this resource. If null or empty resources are used.</param> /// <param name="lang">5 letter or 2 letter language ieetf code: en-US, de-DE or en, de etc.</param> /// <param name="autoAdd">If true if a resource cannot be found a new entry is added in the invariant locale</param> /// <returns> /// The resource as an object. /// </returns> public static object TObject(string resId, string resourceSet = null, string lang = null, bool autoAdd = false) { if (string.IsNullOrEmpty(resId)) return resId; if (resourceSet == null) resourceSet = string.Empty; // check if the res manager exists DbResourceManager manager = null; ResourceManagers.TryGetValue(resourceSet, out manager); // if not we have to create it and add it to static collection if (manager == null) { lock (ResourceManagers) { ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { manager = new DbResourceManager(resourceSet); ResourceManagers.Add(resourceSet, manager); } } } // no manager no resources if (manager == null) return resId; CultureInfo ci = null; if (string.IsNullOrEmpty(lang)) ci = CultureInfo.CurrentUICulture; else ci = new CultureInfo(lang); manager.AutoAddMissingEntries = AutoAddResources; object result = manager.GetObject(resId, ci); if (result == null) return resId; return result; }
/// <summary> /// Localization helper function that Translates a resource /// Id to a resource value object. Use this function if you're /// retrieving non-string values - for string values just use T. /// </summary> /// <param name="resId">The Resource Id to retrieve /// Note resource Ids can be *any* string and if no /// matching resource is found the id is returned. /// </param> /// <param name="resourceSet">Name of the ResourceSet that houses this resource. If null or empty resources are used.</param> /// <param name="lang">5 letter or 2 letter language ieetf code: en-US, de-DE or en, de etc.</param> /// <param name="autoAdd">If true if a resource cannot be found a new entry is added in the invariant locale</param> /// <returns> /// The resource as an object. /// </returns> public static object TObject(string resId, string resourceSet = null, string lang = null, bool autoAdd = false) { if (string.IsNullOrEmpty(resId)) { return(resId); } if (resourceSet == null) { resourceSet = string.Empty; } // check if the res manager exists DbResourceManager manager = GetResourceManager(resourceSet); // no manager no resources if (manager == null) { return(resId); } CultureInfo ci = null; if (string.IsNullOrEmpty(lang)) { ci = CultureInfo.CurrentUICulture; } else { ci = new CultureInfo(lang); } manager.AutoAddMissingEntries = AutoAddResources; object result = manager.GetObject(resId, ci); if (result == null) { return(resId); } return(result); }
/// <summary> /// Localization function /// </summary> /// <param name="resId"></param> /// <param name="resourceSet"></param> /// <param name="lang">Language as ieetf code: en-US, de-DE etc.</param> /// <returns></returns> public static string T(string resId, string resourceSet = null, string lang = null, bool autoAdd = false) { if (resourceSet == null) resourceSet = string.Empty; DbResourceManager manager = null; ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { lock (ResourceManagers) { ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { manager = new DbResourceManager(resourceSet); if (manager != null) { ResourceManagers.Add(resourceSet, manager); } } } } if (manager == null) return resId; CultureInfo ci = null; if (string.IsNullOrEmpty(lang)) ci = CultureInfo.CurrentUICulture; else ci = new CultureInfo(lang); manager.AutoAddMissingEntries = AutoAddResources; string result = manager.GetObject(resId, ci) as string; if (string.IsNullOrEmpty(result)) return resId; return result; }
/// <summary> /// Returns an instance of a DbResourceManager /// </summary> /// <param name="resourceSet"></param> /// <returns></returns> public ResourceManager GetResourceManager(string resourceSet) { // check if the res manager exists DbResourceManager manager = null; ResourceManagers.TryGetValue(resourceSet, out manager); // if not we have to create it and add it to static collection if (manager == null) { lock (ResourceManagers) { ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { manager = new DbResourceManager(resourceSet); ResourceManagers.Add(resourceSet, manager); } } } return(manager); }
/// <summary> /// Creates a strongly typed resource from a ResourceSet object. The ResourceSet passed should always /// be the invariant resourceset that contains all the ResourceIds. /// /// Creates strongly typed keys for each of the keys/values. /// </summary> /// <param name="resourceSet"></param> /// <param name="Namespace">Namespace of the generated class. Pass Null or string.Empty to not generate a namespace</param> /// <param name="classname">Name of the class to generate. Pass null to use the ResourceSet name</param> /// <param name="fileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns></returns> public string CreateResxDesignerClassFromResourceSet(string resourceSetName, string nameSpace, string classname, string fileName) { // Use the custom ResourceManage to retrieve a ResourceSet var man = new DbResourceManager(resourceSetName); var resourceSet = man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IsVb = IsFileVb(fileName); StringBuilder sbClass = new StringBuilder(); CreateResxDesignerClassHeader(classname, nameSpace, IsVb, sbClass); string indent = "\t\t"; // Any resource set that contains a '.' is considered a Local Resource bool IsGlobalResource = !classname.Contains("."); // We'll enumerate through the Recordset to get all the resources IDictionaryEnumerator Enumerator = resourceSet.GetEnumerator(); // We have to turn into a concrete Dictionary while (Enumerator.MoveNext()) { DictionaryEntry item = (DictionaryEntry)Enumerator.Current; if (item.Value == null) item.Value = string.Empty; string typeName = item.Value.GetType().FullName; string key = item.Key as string; if (string.IsNullOrEmpty(key)) continue; string varName = SafeVarName(key); // It's a string if (!IsVb) { sbClass.AppendLine(indent + "public static " + typeName + " " + varName + "\r\n" + indent + "{"); sbClass.AppendFormat(indent + "\tget\r\n" + indent + "\t{{\r\n" + indent + "\t\t" + (string.IsNullOrEmpty(typeName) || typeName == "System.String" ? "return ResourceManager.GetString(\"{0}\", resourceCulture);\r\n" : "return ({1})ResourceManager.GetObject(\"{0}\", resourceCulture);\r\n") + indent + "\t}}\r\n", key,typeName); sbClass.AppendLine(indent + "}\r\n"); } else { sbClass.Append(indent + "Public Shared Property " + varName + "() as " + typeName + "\r\n"); sbClass.AppendFormat(indent + "\tGet\r\n" + indent + "\t\treturn CType( ResourceManager.GetObject(\"{1}\",resourceCulture)\r\n", classname, key,typeName); sbClass.Append(indent + "\tEnd Get\r\n"); sbClass.Append(indent + "End Property\r\n\r\n"); } } if (!IsVb) sbClass.Append("\t}\r\n\r\n"); else sbClass.Append("End Class\r\n\r\n"); string Output = CreateNameSpaceWrapper(nameSpace,IsVb,sbClass.ToString() ); if (!string.IsNullOrEmpty(fileName)) { File.WriteAllText(fileName, Output); return Output; } return sbClass.ToString(); }
/// <summary> /// Creates a strongly typed resource class for a ResoureSet from the DbResourceManager. /// /// Note: Uses the default ResourceProvider settings as set in the DbResourceConfiguration.Current /// property for opening the database and otherwise setting values. /// </summary> /// <param name="ResourceSetName">The name of the resource set. Should be a GLOBAL resource</param> /// <param name="Namespace">The namespace for the generated class. Null or string.Empty to not generate a namespace</param> /// <param name="Classname">Name of the class to be generated</param> /// <param name="FileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns>string of the generated class</returns> public string CreateClassFromDatabaseResource(string ResourceSetName, string Namespace, string Classname, string FileName) { // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet ResourceSet = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); return CreateClassFromResourceSet(ResourceSet, Namespace, Classname, FileName); }
/// <summary> /// Generates a strongly typed assembly from the resources /// /// UNDER CONSTRUCTION. /// Doesn't work correctly for Web forms due to hard coded resource managers. /// </summary> /// <param name="ResourceSetName"></param> /// <param name="Namespace"></param> /// <param name="Classname"></param> /// <param name="FileName"></param> /// <returns></returns> public bool CreateStronglyTypedResource(string ResourceSetName,string Namespace, string Classname, string FileName) { try { //wwDbResourceDataManager Data = new wwDbResourceDataManager(); //IDictionary ResourceSet = Data.GetResourceSet("", ResourceSetName); // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet rs = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IDictionaryEnumerator Enumerator = rs.GetEnumerator(); // We have to turn into a concret Dictionary Dictionary<string, object> Resources = new Dictionary<string, object>(); while (Enumerator.MoveNext()) { DictionaryEntry Item = (DictionaryEntry) Enumerator.Current; Resources.Add(Item.Key as string, Item.Value); } string[] UnmatchedElements; CodeDomProvider CodeProvider = null; string FileExtension = Path.GetExtension(FileName).TrimStart('.').ToLower(); if (FileExtension == "cs") CodeProvider = new Microsoft.CSharp.CSharpCodeProvider(); else if(FileExtension == "vb") CodeProvider = new Microsoft.VisualBasic.VBCodeProvider(); CodeCompileUnit Code = StronglyTypedResourceBuilder.Create(Resources, ResourceSetName, Namespace, CodeProvider, false, out UnmatchedElements); StreamWriter writer = new StreamWriter(FileName); CodeProvider.GenerateCodeFromCompileUnit(Code, writer, new CodeGeneratorOptions()); writer.Close(); } catch (Exception ex) { this.ErrorMessage = ex.Message; return false; } return true; }
/// <summary> /// Creates a strongly typed resource from a ResourceSet object. The ResourceSet passed should always /// be the invariant resourceset that contains all the ResourceIds. /// /// Creates strongly typed keys for each of the keys/values. /// </summary> /// <param name="resourceSetName"></param> /// <param name="nameSpace">Namespace of the generated class. Pass Null or string.Empty to not generate a namespace</param> /// <param name="classname">Name of the class to generate. Pass null to use the ResourceSet name</param> /// <param name="fileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns></returns> public string CreateResxDesignerClassFromResourceSet(string resourceSetName, string nameSpace, string classname, string fileName) { classname = SafeClassName(classname); // Use the custom ResourceManage to retrieve a ResourceSet var man = new DbResourceManager(resourceSetName); var resourceSet = man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IsVb = IsFileVb(fileName); StringBuilder sbClass = new StringBuilder(); CreateResxDesignerClassHeader(classname, nameSpace, IsVb, sbClass); string indent = "\t\t"; // We'll enumerate through the Recordset to get all the resources IDictionaryEnumerator enumerator = resourceSet.GetEnumerator(); // We have to turn into a concrete Dictionary while (enumerator.MoveNext()) { DictionaryEntry item = (DictionaryEntry)enumerator.Current; if (item.Value == null) { item.Value = string.Empty; } string typeName = item.Value.GetType().FullName; string key = item.Key as string; if (string.IsNullOrEmpty(key)) { continue; } string varName = SafeVarName(key); // It's a string if (!IsVb) { sbClass.AppendLine(indent + "public static " + typeName + " " + varName + "\r\n" + indent + "{"); sbClass.AppendFormat(indent + "\tget\r\n" + indent + "\t{{\r\n" + indent + "\t\t" + (string.IsNullOrEmpty(typeName) || typeName == "System.String" ? "return ResourceManager.GetString(\"{0}\", resourceCulture);\r\n" : "return ({1})ResourceManager.GetObject(\"{0}\", resourceCulture);\r\n") + indent + "\t}}\r\n", key, typeName); sbClass.AppendLine(indent + "}\r\n"); } else { sbClass.Append(indent + "Public Shared Property " + varName + "() as " + typeName + "\r\n"); sbClass.AppendFormat(indent + "\tGet\r\n" + indent + "\t\treturn CType( ResourceManager.GetObject(\"{1}\",resourceCulture)\r\n", classname, key, typeName); sbClass.Append(indent + "\tEnd Get\r\n"); sbClass.Append(indent + "End Property\r\n\r\n"); } } if (!IsVb) { sbClass.Append("\t}\r\n\r\n"); } else { sbClass.Append("End Class\r\n\r\n"); } string Output = CreateNameSpaceWrapper(nameSpace, IsVb, sbClass.ToString(), false); if (!string.IsNullOrEmpty(fileName)) { File.WriteAllText(fileName + ".designer" + (IsVb ? ".vb" : ".cs"), Output); return(Output); } return(sbClass.ToString()); }
/// <summary> /// Returns an instance of a DbResourceManager /// </summary> /// <param name="resourceSet"></param> /// <returns></returns> public static ResourceManager GetResourceManager(string resourceSet) { // check if the res manager exists DbResourceManager manager = null; ResourceManagers.TryGetValue(resourceSet, out manager); // if not we have to create it and add it to static collection if (manager == null) { lock (ResourceManagers) { ResourceManagers.TryGetValue(resourceSet, out manager); if (manager == null) { manager = new DbResourceManager(resourceSet); ResourceManagers.Add(resourceSet, manager); } } } return manager; }