/// <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("~", ""); } DbResourceDataManager Data = new DbResourceDataManager(); 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, null); } } }
/// <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 = 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 = resourceKey; if (DbResourceConfiguration.Current.AddMissingResources) { lock (_SyncLock) { value = ResourceManager.GetObject(resourceKey, culture); if (value == null) { // Add invariant resource DbResourceDataManager data = new DbResourceDataManager(); if (!data.ResourceExists(resourceKey, "", _className)) { data.AddResource(resourceKey, resourceKey, "", _className, null); } value = resourceKey; } } } } return(value); }
/// <summary> /// Add a new resource to the base resource set /// </summary> /// <param name="name"></param> /// <param name="value"></param> public void AddMissingResource(string name, string value, CultureInfo culture = null) { DbResourceDataManager manager = new DbResourceDataManager(); string cultureName = string.Empty; if (culture != null) { cultureName = culture.IetfLanguageTag; } lock (AddSyncLock) { // double check if culture neutral version exists string item = manager.GetResourceObject(name, BaseName, cultureName) as string; if (item != null) { return; } manager.AddResource(name, value, cultureName, BaseName, null); } }
/// <summary> /// Internal lookup method that handles retrieving a resource /// by its resource id and culture. Realistically this method /// is always called with the culture being null or empty /// but the routine handles resource fallback in case the /// code is manually called. /// </summary> /// <param name="resourceKey"></param> /// <param name="cultureName"></param> /// <returns></returns> object GetObjectInternal(string resourceKey, string cultureName) { IDictionary resources = GetResourceCache(cultureName); object value = null; if (resources == null) { value = null; } else { value = resources[resourceKey]; } // If we're at a specific culture (en-Us) and there's no value fall back // to the generic culture (en) if (value == null && cultureName.Length > 3) { // try again with the 2 letter locale return(GetObjectInternal(resourceKey, cultureName.Substring(0, 2))); } // If the value is still null get the invariant value if (value == null) { resources = GetResourceCache(""); if (resources == null) { value = null; } else { value = resources[resourceKey]; } } // 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) { // No entry there value = resourceKey; // DEPENDENCY HERE (#2): using DbResourceConfiguration and DbResourceDataManager to optionally // add missing resource keys // Add a key in the repository at least for the Invariant culture // Something's referencing but nothing's there if (DbResourceConfiguration.Current.AddMissingResources) { lock (_SyncLock) { if (resources[resourceKey] == null) { var data = new DbResourceDataManager(); if (!data.ResourceExists(resourceKey, "", _ResourceSetName)) { data.AddResource(resourceKey, resourceKey, "", _ResourceSetName, null); } // add to current invariant resource set resources.Add(resourceKey, resourceKey); } } } } return(value); }