コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }