Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Which best practice to read resource file.

I am trying below two approaches but each has its own drawback it seems.

Approach 1: Using CreateFileBasedResourceManger function. But due to below code..Handle count is getting increased.
C#
ResourceManager resourceReader = ResourceManager.CreateFileBasedResourceManager("MyResources", appFilePath, null);
               string translatedMessage = resourceReader.GetString(keyName);
               if (!string.IsNullOrEmpty(translatedMessage))
               {
                   return translatedMessage;

               }
               else
               {
                   englishKey = keyName.Substring(keyName.Length - 4, 4);
                   keyName = keyName.Replace(englishKey, "_ENG");
                   translatedMessage = resourceReader.GetString(keyName);
                   if (!string.IsNullOrEmpty(translatedMessage))
                   {
                       return translatedMessage;
                   }
                   throw new FormatterException("Key is not present in the resoucre file");
               }


Approach 2 : Using ResourceSet constructor..Handle count is getting reduced but performance has been down.
C#
using (ResourceSet resx = new ResourceSet(appFilePath + "\\MyResources.resources"))
            {
                    string translatedMessage=  resx.GetString(keyName);

                if (!string.IsNullOrEmpty(translatedMessage))
                {
                    return translatedMessage;

                }
                else
                {
                    englishKey = keyName.Substring(keyName.Length - 4, 4);
                    keyName = keyName.Replace(englishKey, "_ENG");
                    translatedMessage = resx.GetString(keyName);
                    if (!string.IsNullOrEmpty(translatedMessage))
                    {
                        return translatedMessage;
                    }
                    throw new FormatterException("Key is not present in the resoucre file");
                }
            }

Please suggest is there any other way to read resource file by keeping performance up and handle count.
Posted
Updated 5-Jun-12 23:56pm
v2

1 solution

Very good article on localization using resource file in .Net

Read this

.NET - Localization using Resource file[^]


Hope this helps , If yes then plz accept and vote the answer. Any queries / questions on this are always welcome.

Thanks & Regards
RDBurmon.Sr.Software Engineer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900