/// <summary> /// Writes all resources out to the resource store. Optional flag that /// allows deleting all resources for a given culture and basename first /// so that you get 'clean set' of resource with no orphaned values. /// </summary> /// <param name="DeleteAllRowsFirst"></param> public void Generate(bool DeleteAllRowsFirst) { // *** DEPENDENCY HERE wwDbResourceDataManager Data = new wwDbResourceDataManager(); Data.GenerateResources(resourceList, this.cultureInfo.Name, this.baseName, DeleteAllRowsFirst); }
/// <summary> /// Generates the Database resources for a given form /// </summary> /// <param name="ParentControl"></param> /// <param name="ResourcePrefix"></param> public void AddResourceToResourceFile(Control ParentControl, string ResourcePrefix, string ResourceSet) { if (ResourcePrefix == null) { ResourcePrefix = "Resource1"; } if (ResourceSet == null) { ResourceSet = this.Context.Request.ApplicationPath + this.Parent.TemplateControl.AppRelativeVirtualPath.Replace("~", ""); } wwDbResourceDataManager Data = new wwDbResourceDataManager(); List <LocalizableProperty> ResourceList = this.GetAllLocalizableControls(ParentControl); foreach (LocalizableProperty Resource in ResourceList) { string ResourceKey = Resource.ControlId + ResourcePrefix + "." + Resource.Property; if (!Data.ResourceExists(ResourceKey, "", ResourceSet)) { Data.AddResource(ResourceKey, Resource.Value, "", ResourceSet); } } }
/// <summary> /// The main method to retrieve a specific resource key. The provider /// internally handles resource fallback based on the ResourceSet implementation. /// </summary> /// <param name="ResourceKey"></param> /// <param name="culture"></param> /// <returns></returns> object IResourceProvider.GetObject(string ResourceKey, CultureInfo culture) { object value = this.ResourceManager.GetObject(ResourceKey, culture); // *** If the value is still null and we're at the invariant culture // *** let's add a marker that the value is missing // *** this also allows the pre-compiler to work and never return null if (value == null && (culture == null || culture == CultureInfo.InvariantCulture)) { // *** No entry there value = ""; if (wwDbResourceConfiguration.Current.AddMissingResources) { // *** Add invariant resource wwDbResourceDataManager Data = new wwDbResourceDataManager(); if (!Data.ResourceExists(ResourceKey, "", this._className)) { Data.UpdateOrAdd(ResourceKey, "*** Missing", "", this._className); } } } return(value); }
/// <summary> /// Manages caching of the Resource Sets. Once loaded the values are loaded from the /// cache only. /// </summary> /// <param name="cultureName"></param> /// <returns></returns> private IDictionary GetResourceCache(string cultureName) { if (cultureName == null) { cultureName = ""; } if (this._resourceCache == null) { this._resourceCache = new ListDictionary(); } IDictionary Resources = this._resourceCache[cultureName] as IDictionary; if (Resources == null) { // *** DEPENDENCY HERE (#1): Using wwDbResourceDataManager to retrieve resources // *** Use datamanager to retrieve the resource keys from the database wwDbResourceDataManager Data = new wwDbResourceDataManager(); Resources = Data.GetResourceSet(cultureName as string, this._ResourceSetName); this._resourceCache[cultureName] = Resources; } return(Resources); }
private static void AddResourceToStore(string key, object value, string resourceSet, IServiceProvider serviceProvider) { // *** Use custom data manager to write the values into the database wwDbResourceDataManager Manager = new wwDbResourceDataManager(); if (Manager.UpdateOrAdd(key, value, "", resourceSet) == -1) { throw new InvalidOperationException("Resource update error: " + Manager.ErrorMessage); } }
private void Load() { // *** RAS Modified: Read the full page path ie. /internationalization/test.aspx string ResourceSet = GetFullPagePath(); // *** Load IDictionary data using the DataManager (same code as provider) wwDbResourceDataManager Manager = new wwDbResourceDataManager(); this._reader = Manager.GetResourceSet("", ResourceSet); }
/// <summary> /// Imports an individual ResX Resource file into the database /// </summary> /// <param name="FileName">Full path to the the ResX file</param> /// <param name="ResourceSetName">Name of the file or for local resources the app relative path plus filename (admin/default.aspx or default.aspx)</param> /// <param name="LocaleId">Locale Id of the file to import. Use "" for Invariant</param> /// <returns></returns> public bool ImportResourceFile(string FileName, string ResourceSetName, string LocaleId) { string FilePath = Path.GetDirectoryName(FileName) + "\\"; wwDbResourceDataManager Data = new wwDbResourceDataManager(); XmlDocument Dom = new XmlDocument(); try { Dom.Load(FileName); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(false); } XmlNodeList nodes = Dom.DocumentElement.SelectNodes("data"); foreach (XmlNode Node in nodes) { string Value = Node.ChildNodes[0].InnerText; string Name = Node.Attributes["name"].Value; string Type = null; if (Node.Attributes["type"] != null) { Type = Node.Attributes["type"].Value; } if (string.IsNullOrEmpty(Type)) { Data.UpdateOrAdd(Name, Value, LocaleId, ResourceSetName); } else { // *** File based resources are formatted: filename;full type name string[] tokens = Value.Split(';'); if (tokens.Length > 0) { string ResFileName = FilePath + tokens[0]; if (File.Exists(ResFileName)) { // *** DataManager knows about file resources and can figure type info Data.UpdateOrAdd(Name, ResFileName, LocaleId, ResourceSetName, true); } } } } return(true); }
/// <summary> /// This is the worker method responsible for actually retrieving resources from the resource /// store. This method goes out queries the database by asking for a specific ResourceSet and /// Culture and it returns a Hashtable (as IEnumerable) to use as a ResourceSet. /// /// The ResourceSet manages access to resources via IEnumerable access which is ultimately used /// to return resources to the front end. /// /// Resources are read once and cached into an internal Items field. A ResourceReader instance /// is specific to a ResourceSet and Culture combination so there should never be a need to /// reload this data, except when explicitly clearing the reader/resourceset (in which case /// Items can be set to null via ClearResources()). /// </summary> /// <returns>An IDictionaryEnumerator of the resources for this reader</returns> public IDictionaryEnumerator GetEnumerator() { if (this.Items != null) { return(this.Items.GetEnumerator()); } // *** DEPENDENCY HERE // *** Here's the only place we really access the database and return // *** a specific ResourceSet for a given ResourceSet Id and Culture wwDbResourceDataManager Manager = new wwDbResourceDataManager(); this.Items = Manager.GetResourceSet(this.cultureInfo.Name, this.baseNameField); return(this.Items.GetEnumerator()); }
/// <summary> /// Creates strongly typed classes from all global resources in the current application /// from the active wwDbResourceManager. One class is created which contains each of the /// resource classes. Classnames are not necessarily named with /// /// Uses the default wwDbResourceConfiguration.Current settings for connecting /// to the database. /// </summary> /// <param name="Namespace">Optional namespace for the generated file</param> /// <param name="FileName">Output class file. .cs or .vb determines code language</param> /// <returns>Generated class as a string</returns> public string CreateClassFromAllDatabaseResources(string Namespace, string FileName) { bool IsVb = this.IsFileVb(FileName); wwDbResourceDataManager man = new wwDbResourceDataManager(); DataTable Resources = man.GetAllResourceSets(ResourceListingTypes.GlobalResourcesOnly); StringBuilder sbClasses = new StringBuilder(); foreach (DataRow row in Resources.Rows) { string ResourceSet = row["resourceset"] as string; string Class = this.CreateClassFromDatabaseResource(ResourceSet, null, CultureInfo.InvariantCulture.TextInfo.ToTitleCase(ResourceSet), null); sbClasses.Append(Class); } string Output = this.CreateNameSpaceWrapper(Namespace, IsVb, sbClasses.ToString()); File.WriteAllText(FileName, Output); return(Output); }
/// <summary> /// Work method that goes through the database resources and dumps them /// into resource files. Note - will overwrite existing files. /// </summary> /// <returns></returns> protected bool GenerateResXFiles(bool LocalResources) { wwDbResourceDataManager Data = new wwDbResourceDataManager(); // *** Retrieve all resources for a ResourceSet for all cultures // *** The data is ordered by ResourceSet, LocaleId and resource ID as each // *** ResourceSet or Locale changes a new file is written DataTable dtResources = Data.GetAllResourcesForResourceSet(LocalResources); if (dtResources == null) { return(false); } string LastSet = ""; string LastLocale = "@!"; XmlWriter xWriter = null; XmlWriterSettings XmlSettings = new XmlWriterSettings(); // *** Make sure we use fragment syntax so there's no validation // *** otherwise loading the original string will fail XmlSettings.ConformanceLevel = ConformanceLevel.Fragment; // *** Pretty format XmlSettings.Indent = true; foreach (DataRow dr in dtResources.Rows) { // *** Read into vars for easier usage below string ResourceId = dr["ResourceId"] as string; string Value = dr["Value"] as string; string Type = dr["Type"] as string; string TextFile = dr["TextFile"] as string; byte[] BinFile = dr["BinFile"] as byte[]; string FileName = dr["FileName"] as string; string ResourceSet = dr["ResourceSet"] as string; ResourceSet = ResourceSet.ToLower(); string LocaleId = dr["LocaleId"] as string; LocaleId = LocaleId.ToLower(); // *** Create a new output file if the resource set or locale changes if (ResourceSet != LastSet || LocaleId != LastLocale) { if (xWriter != null) { xWriter.WriteRaw("</root>"); xWriter.Close(); } string Loc = ".resx"; if (LocaleId != "") { Loc = "." + LocaleId + ".resx"; } xWriter = XmlWriter.Create(this.FormatResourceSetPath(ResourceSet, LocalResources) + Loc, XmlSettings); xWriter.WriteRaw(ResXDocumentTemplate); LastSet = ResourceSet; LastLocale = LocaleId; } //<data name="LinkButton1Resource1.Text" xml:space="preserve"> // <value>LinkButton</value> //</data> if (Type == "") // plain string value { xWriter.WriteStartElement("data"); xWriter.WriteAttributeString("name", ResourceId); xWriter.WriteAttributeString("space", "xml", "preserve"); xWriter.WriteElementString("value", Value); xWriter.WriteEndElement(); // data } // *** File Resources get written to disk else if (Type == "FileResource") { string ResourceFilePath = this.FormatResourceSetPath(ResourceSet, LocalResources); string ResourcePath = new FileInfo(ResourceFilePath).DirectoryName; if (Value.IndexOf("System.String") > -1) { string[] Tokens = Value.Split(';'); Encoding Encode = Encoding.Default; try { if (Tokens.Length == 3) { Encode = Encoding.GetEncoding(Tokens[2]); } // *** Write out the file to disk File.WriteAllText(ResourcePath + "\\" + FileName, TextFile, Encode); } catch { } } else { File.WriteAllBytes(ResourcePath + "\\" + FileName, BinFile); } //<data name="Scratch" type="System.Resources.ResXFileRef, System.Windows.Forms"> // <value>Scratch.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value> //</data> xWriter.WriteStartElement("data"); xWriter.WriteAttributeString("name", ResourceId); xWriter.WriteAttributeString("type", "System.Resources.ResXFileRef, System.Windows.Forms"); // *** values are already formatted in the database xWriter.WriteElementString("value", Value); xWriter.WriteEndElement(); // data } } // foreach dr if (xWriter != null) { xWriter.WriteRaw("</root>"); xWriter.Close(); } return(true); }