GetString() публичный Метод

public GetString ( String name ) : String
name String
Результат String
Пример #1
0
        public virtual string GetString(string name, CultureInfo culture)
        {
            if (name == null)
            {
                throw new ArgumentNullException("Name is null.");
            }

            if (culture == null)
            {
                culture = CultureInfo.CurrentUICulture;
            }

            lock (this) {
                ResourceSet set = InternalGetResourceSet(culture, true, true);
                string      str = null;

                if (set != null)
                {
                    str = set.GetString(name, ignoreCase);
                    if (str != null)
                    {
                        return(str);
                    }
                }

                /* Try parent cultures */

                do
                {
                    culture = culture.Parent;

                    set = InternalGetResourceSet(culture, true, true);
                    if (set != null)
                    {
                        str = set.GetString(name, ignoreCase);
                        if (str != null)
                        {
                            return(str);
                        }
                    }
                } while(!culture.Equals(neutral_culture) &&
                        !culture.Equals(CultureInfo.InvariantCulture));
            }

            return(null);
        }
Пример #2
0
        /// <summary>Gets the value of the <see cref="T:System.String" /> resource localized for the specified culture.</summary>
        /// <returns>The value of the resource localized for the specified culture. If a best match is not possible, null is returned.</returns>
        /// <param name="name">The name of the resource to get. </param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo" /> object that represents the culture for which the resource is localized. Note that if the resource is not localized for this culture, the lookup will fall back using the current thread's <see cref="P:System.Globalization.CultureInfo.Parent" /> property, stopping after looking in the neutral culture.If this value is null, the <see cref="T:System.Globalization.CultureInfo" /> is obtained using the current thread's <see cref="P:System.Globalization.CultureInfo.CurrentUICulture" /> property. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="name" /> parameter is null. </exception>
        /// <exception cref="T:System.InvalidOperationException">The value of the specified resource is not a <see cref="T:System.String" />. </exception>
        /// <exception cref="T:System.Resources.MissingManifestResourceException">No usable set of resources has been found, and there are no neutral culture resources. </exception>
        public virtual string GetString(string name, CultureInfo culture)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (culture == null)
            {
                culture = CultureInfo.CurrentUICulture;
            }
            lock (this)
            {
                ResourceSet resourceSet = this.InternalGetResourceSet(culture, true, true);
                string      @string;
                if (resourceSet != null)
                {
                    @string = resourceSet.GetString(name, this.ignoreCase);
                    if (@string != null)
                    {
                        return(@string);
                    }
                }
                for (;;)
                {
                    culture     = culture.Parent;
                    resourceSet = this.InternalGetResourceSet(culture, true, true);
                    if (resourceSet != null)
                    {
                        @string = resourceSet.GetString(name, this.ignoreCase);
                        if (@string != null)
                        {
                            break;
                        }
                    }
                    if (culture.Equals(this.neutral_culture) || culture.Equals(CultureInfo.InvariantCulture))
                    {
                        goto IL_A7;
                    }
                }
                return(@string);

                IL_A7 :;
            }
            return(null);
        }
        public virtual string GetString(string name, CultureInfo culture)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (culture == null)
            {
                culture = CultureInfo.CurrentUICulture;
            }
            if (FrameworkEventSource.IsInitialized)
            {
                FrameworkEventSource.Log.ResourceManagerLookupStarted(this.BaseNameField, this.MainAssembly, culture.Name);
            }
            ResourceFallbackManager manager = new ResourceFallbackManager(culture, this._neutralResourcesCulture, true);
            ResourceSet             set     = null;

            foreach (CultureInfo info in manager)
            {
                ResourceSet set2 = this.InternalGetResourceSet(info, true, true);
                if (set2 == null)
                {
                    break;
                }
                if (set2 != set)
                {
                    string str = set2.GetString(name, this._ignoreCase);
                    if (str != null)
                    {
                        return(str);
                    }
                    set = set2;
                }
            }
            if (FrameworkEventSource.IsInitialized)
            {
                FrameworkEventSource.Log.ResourceManagerLookupFailed(this.BaseNameField, this.MainAssembly, culture.Name);
            }
            return(null);
        }
Пример #4
0
 public virtual String GetString(String name, CultureInfo culture)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (culture == null)
     {
         culture = CultureInfo.CurrentUICulture;
     }
     lock (this)
     {
         // Scan up through the parent cultures until we
         // find a string that matches the tag.  We do this
         // so that we can pick up the neutral fallbacks if
         // language-specific versions are not available.
         do
         {
             ResourceSet set = InternalGetResourceSet
                                   (culture, true, true);
             if (set != null)
             {
                 String ret = set.GetString(name);
                 if (ret != null)
                 {
                     return(ret);
                 }
             }
             if (culture.Equals(CultureInfo.InvariantCulture))
             {
                 break;
             }
             culture = culture.Parent;
         }while(culture != null);
     }
     return(name);
 }
        private ResourceSet GetResourceSet(CultureInfo culture, bool custom) {
            ResourceSet resource;
            String resourceKey = GetResourceKey(culture, custom);
            lock(_resourceLock) {

                // if the resource data for this culture has not yet been loaded, load it
                if(!ResourceData.TryGetValue(resourceKey, out resource)) {

                    // set the filename according to the cuture
                    String filename;
                    if(null == culture) {
                        filename = "resources.custom.txt";
                    } else if(culture.Equals(CultureInfo.InvariantCulture)) {
                        filename = "resources.txt";
                    } else {
                        filename = "resources.";
                        if(custom) {
                            filename += "custom.";
                        }
                        filename += culture.Name.ToLowerInvariant() + ".txt";
                    }
                    filename = Path.Combine(_resourcePath, filename);

                    // load the resource set and add it into the resource table
                    resource = new ResourceSet(new PlainTextResourceReader(filename));

                    // Note (arnec): We call GetString to force the lazy loading of the resource stream, thereby making
                    // GetString(), at least theoretically, thread-safe for subsequent calls.
                    resource.GetString("", true);
                    ResourceData.Add(resourceKey, resource);
                }
            }
            return resource;
        }
Пример #6
0
 static string ReadToken(EmbeddedResource resources, string token)
 {
     using (var resourceStream = resources.GetResourceStream())
     using (var resourceSet = new ResourceSet(resourceStream))
         return resourceSet.GetString(token, true);
 }
Пример #7
0
        // Looks up a resource value for a particular name.  Looks in the
        // specified CultureInfo, and if not found, all parent CultureInfos.
        // Returns null if the resource wasn't found.
        //
        public virtual string GetString(string name, CultureInfo culture)
        {
            if (null == name)
            {
                throw new ArgumentNullException(nameof(name));
            }

#if FEATURE_APPX
            if (_bUsingModernResourceManagement)
            {
                // If the caller explicitly passed in a culture that was obtained by calling CultureInfo.CurrentUICulture,
                // null it out, so that we re-compute it.  If we use modern resource lookup, we may end up getting a "better"
                // match, since CultureInfo objects can't represent all the different languages the AppX resource model supports.
                if (object.ReferenceEquals(culture, CultureInfo.CurrentUICulture))
                {
                    culture = null;
                }

                if (_PRIonAppXInitialized == false)
                {
                    // Always throw if we did not fully succeed in initializing the WinRT Resource Manager.

                    if (_PRIExceptionInfo != null && _PRIExceptionInfo.PackageSimpleName != null && _PRIExceptionInfo.ResWFile != null)
                    {
                        throw new MissingManifestResourceException(SR.Format(SR.MissingManifestResource_ResWFileNotLoaded, _PRIExceptionInfo.ResWFile, _PRIExceptionInfo.PackageSimpleName));
                    }

                    throw new MissingManifestResourceException(SR.MissingManifestResource_NoPRIresources);
                }

                // Throws WinRT hresults.
                return(GetStringFromPRI(name,
                                        culture == null ? null : culture.Name,
                                        _neutralResourcesCulture.Name));
            }
            else
#endif // FEATURE_APPX
            {
                if (culture == null)
                {
                    culture = CultureInfo.CurrentUICulture;
                }

                ResourceSet last = GetFirstResourceSet(culture);

                if (last != null)
                {
                    string value = last.GetString(name, _ignoreCase);
                    if (value != null)
                    {
                        return(value);
                    }
                }


                // This is the CultureInfo hierarchy traversal code for resource
                // lookups, similar but necessarily orthogonal to the ResourceSet
                // lookup logic.
                ResourceFallbackManager mgr = new ResourceFallbackManager(culture, _neutralResourcesCulture, true);
                foreach (CultureInfo currentCultureInfo in mgr)
                {
                    ResourceSet rs = InternalGetResourceSet(currentCultureInfo, true, true);
                    if (rs == null)
                    {
                        break;
                    }

                    if (rs != last)
                    {
                        string value = rs.GetString(name, _ignoreCase);
                        if (value != null)
                        {
                            // update last used ResourceSet
                            if (_lastUsedResourceCache != null)
                            {
                                lock (_lastUsedResourceCache)
                                {
                                    _lastUsedResourceCache.lastCultureName = currentCultureInfo.Name;
                                    _lastUsedResourceCache.lastResourceSet = rs;
                                }
                            }
                            return(value);
                        }

                        last = rs;
                    }
                }
            }

            return(null);
        }
Пример #8
0
 /// <summary>
 /// Get the value corresponding to specified key from specified resource file
 /// </summary>
 /// <param name="fileName">Name of the resource file</param>
 /// <param name="keyName">Name of the key</param>
 /// <param name="resourceFileLocation">Resource file location</param>
 /// <returns>
 /// Value of the key
 /// </returns>
 public static string GetConfigurationFromResourceFile(string fileName, string keyName, Enumerators.ResourceFileLocation resourceFileLocation)
 {
     string resourceValue = string.Empty;
     ResXResourceReader resxReader = null;
     try
     {
         resxReader = new ResXResourceReader(HttpContext.Current.Server.MapPath(@"~/" + resourceFileLocation + ConstantStrings.ForwardSlash + fileName + ConstantStrings.ResourceFileExtension));
         ResourceSet resourceSet = new ResourceSet(resxReader);
         resourceValue = resourceSet.GetString(keyName);
     }
     catch (Exception exception)
     {
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ConstantStrings.LogTableName);
     }
     finally
     {
         resxReader.Close();
     }
     return resourceValue;
 }
		private static string GetLoadStringFromCall (MethodReference mr)
		{
			MethodDefinition md = mr.Resolve ();
			if ((md == null) || !IsResource (md))
				return null;

			string resourceName = GetResourceNameFromResourceGetter (md);
			if (resourceName == null)
				return null;

			AssemblyDefinition ad = md.GetAssembly ();
			string resourceClassName = md.DeclaringType.FullName + ".resources";
			EmbeddedResource resource = GetEmbeddedResource (ad, resourceClassName);
			if (resource == null)
				return null;

			using (MemoryStream ms = new MemoryStream (resource.GetResourceData ()))
			using (ResourceSet resourceSet = new ResourceSet (ms)) {
				return resourceSet.GetString (resourceName);
			}
		}
Пример #10
0
        // Looks up a resource value for a particular name.  Looks in the
        // specified CultureInfo, and if not found, all parent CultureInfos.
        // Returns null if the resource wasn't found.
        //
        public virtual string GetString(string name, CultureInfo culture)
        {
            if (null == name)
            {
                throw new ArgumentNullException(nameof(name));
            }

#if FEATURE_APPX || ENABLE_WINRT
            if (_useUapResourceManagement)
            {
                // Throws WinRT hresults.
                return(GetStringFromPRI(name, culture, _neutralResourcesCulture.Name));
            }
#endif

            if (culture == null)
            {
                culture = CultureInfo.CurrentUICulture;
            }

            ResourceSet last = GetFirstResourceSet(culture);

            if (last != null)
            {
                string value = last.GetString(name, _ignoreCase);
                if (value != null)
                {
                    return(value);
                }
            }

            // This is the CultureInfo hierarchy traversal code for resource
            // lookups, similar but necessarily orthogonal to the ResourceSet
            // lookup logic.
            ResourceFallbackManager mgr = new ResourceFallbackManager(culture, _neutralResourcesCulture, true);
            foreach (CultureInfo currentCultureInfo in mgr)
            {
                ResourceSet rs = InternalGetResourceSet(currentCultureInfo, true, true);
                if (rs == null)
                {
                    break;
                }

                if (rs != last)
                {
                    string value = rs.GetString(name, _ignoreCase);
                    if (value != null)
                    {
                        // update last used ResourceSet
                        if (_lastUsedResourceCache != null)
                        {
                            lock (_lastUsedResourceCache)
                            {
                                _lastUsedResourceCache.lastCultureName = currentCultureInfo.Name;
                                _lastUsedResourceCache.lastResourceSet = rs;
                            }
                        }
                        return(value);
                    }

                    last = rs;
                }
            }

            return(null);
        }
Пример #11
0
        /// <summary>
        /// Checks whether the provided resource set contains an entry matching the provided key.
        /// </summary>
        /// <param name="resources"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ResourceSetMatches(ResourceSet resources, string key)
        {
            if (resources == null)
                return false;

            string output = resources.GetString(key);

            return (output != null && output != String.Empty);
        }
Пример #12
0
        /// <summary>
        /// Retrieves the resource set containing the specified key from the provided assembly.
        /// </summary>
        /// <param name="assembly">The assembly containing the target resource set.</param>
        /// <param name="key">The key of the target entry.</param>
        /// <returns>The resource set from the provided assembly containing the provided key.</returns>
        public static ResourceSet GetResourceSetFromAssembly(Assembly assembly, string key)
        {
            string[] resourceNames = assembly.GetManifestResourceNames();

            ResourceSet resources = null;

            foreach (string resourceName in resourceNames)
            {
                string text = String.Empty;

                using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                {
                    resources = new ResourceSet(stream);

                    stream.Close();

                }

                text = resources.GetString(key);

                if (ResourceSetMatches(resources, key))
                {
                    return resources;
                }
            }

            return resources;
        }