15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating Custom Cultures ❘ 601<br />

In the implementation of InternalGetResourceSet(), check first if the resource set for the culture queried<br />

for a resource is already in the hash table; if it already exists, return it to the caller. If the resource set is not<br />

available, create a new DatabaseResourceSet object with the queried culture, add it to the dictionary, <strong>and</strong><br />

return it to the caller:<br />

protected override ResourceSet InternalGetResourceSet(<br />

CultureInfo culture, bool createIfNotExists, bool tryParents)<br />

{<br />

DatabaseResourceSet rs = null;<br />

}<br />

}<br />

if (resourceSets.ContainsKey(culture.Name))<br />

{<br />

rs = resourceSets[culture.Name];<br />

}<br />

else<br />

{<br />

rs = new DatabaseResourceSet(connectionString, culture);<br />

resourceSets.Add(culture.Name, rs);<br />

}<br />

return rs;<br />

Client application for databaseresourcereader<br />

The way the class ResourceManager is used from the client application here does not differ much<br />

from the previous use of the ResourceManager class. The only difference is that the custom class<br />

DatabaseResourceManager is used instead of the class ResourceManager. The following code snippet<br />

demonstrates how you can use your own resource manager.<br />

A new DatabaseResourceManager object is created by passing the database connection string to the<br />

constructor. Then, you can invoke the GetString() method that is implemented in the base class as you did<br />

earlier, passing the key <strong>and</strong> an optional object of type CultureInfo to specify a culture. In turn, you get a<br />

resource value from the database because this resource manager is using the classes DatabaseResourceSet<br />

<strong>and</strong> DatabaseResourceReader.<br />

var rm = new DatabaseResourceManager(<br />

@"server=(local)\sqlexpress;database=LocalizationDemo;" +<br />

"trusted_connection=true");<br />

string spanishWelcome = rm.GetString("Welcome", new CultureInfo("es-ES"));<br />

string italianThankyou = rm.GetString("ThankYou", new CultureInfo("it"));<br />

string threadDefaultGoodMorning = rm.GetString("GoodMorning");<br />

code snippet DatabaseResourceReaderClient/Program.cs<br />

CreaTing CusTom CulTures<br />

Over time, more <strong>and</strong> more languages have become supported by the .<strong>NET</strong> Framework. However, not all<br />

languages of the world are available with .<strong>NET</strong>. You can create a custom culture. Some examples of when<br />

creating custom cultures can be useful are to support a minority within a region or to create subcultures for<br />

different dialects.<br />

Custom cultures <strong>and</strong> regions can be created with the class CultureAndRegionInfoBuilder in the<br />

namespace System.Globalization. This class is located in the assembly sysglobl.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!