Create a custom data-driven localization resource provider

Source: Internet
Author: User

The original article is very long. In order to facilitate reading and understanding, this article is rewritten into a plain and easy-to-understand Chinese text with refined content.

Prerequisites: the default method for processing resources and localization is to use resx file storage resources.

To use a custom resource provider, two steps are required:
A) modify the web. config file so that the system can use the Custom Resource Provider.
B) create a custom resource provider Class, which consists of at least three:
1. ResourceProviderFactory, a factory class, used to create a ResourceProvider object.
2. ResourceProvider, which implements the IResourceProvider, IImplicitResourceProvider, and IwwResourceProvider interfaces.
3. ResourceReader implements IResourceReader.


Modify the web. config file to use the Custom Resource Provider.
Copy codeThe Code is as follows:
<Configuration>
<System. web>
<Globalization resourceProviderFactoryType = "Westwind. Globalization. DbSimpleResourceProviderFactory, Westwind. Globalization"/>
</System. web>
</Configuration>


Create a custom resource provider class:
1. Factory
Copy codeThe Code is as follows:
[DesignTimeResourceProviderFactoryAttribute (typeof (DbDesignTimeResourceProviderFactory)]
Public class DbSimpleResourceProviderFactory: ResourceProviderFactory
{

Public override IResourceProvider CreateGlobalResourceProvider (string classname)
{
Return new DbSimpleResourceProvider (null, classname );
}


Public override IResourceProvider CreateLocalResourceProvider (string virtualPath)
{

String ResourceSetName = DbResourceConfiguration. Current. StripVirtualPath (virtualPath );
Return new DbSimpleResourceProvider (null, ResourceSetName. ToLower ());
}
}

2. provider class
Copy codeThe Code is as follows:
Public class DbSimpleResourceProvider: IResourceProvider, IImplicitResourceProvider
{

Private string _ ResourceSetName;


Private IDictionary _ resourceCache;


Private DbSimpleResourceProvider ()
{}


Public DbSimpleResourceProvider (string virtualPath, string className)
{
_ ResourceSetName = className;
}



Private IDictionary GetResourceCache (string cultureName)
{
If (cultureName = null)
CultureName = "";


If (this. _ resourceCache = null)
This. _ resourceCache = new ListDictionary ();


IDictionary Resources = this. _ resourceCache [cultureName] as IDictionary;
If (Resources = null)
{
// *** Dependency here (#1): Using DbResourceDataManager to retrieve resources


// *** Use datamanager to retrieve the resource keys from the database
DbResourceDataManager Data = new DbResourceDataManager ();
Resources = Data. GetResourceSet (cultureName as string, this. _ ResourceSetName );
This. _ resourceCache [cultureName] = Resources;
}


Return Resources;
}



Public void ClearResourceCache ()
{
This. _ resourceCache. Clear ();
}



Object IResourceProvider. GetObject (string ResourceKey, CultureInfo Culture)
{
String CultureName = null;
If (Culture! = Null)
CultureName = Culture. Name;
Else
CultureName = CultureInfo. CurrentUICulture. Name;


Return this. GetObjectInternal (ResourceKey, CultureName );
}



Object GetObjectInternal (string ResourceKey, string CultureName)
{
IDictionary Resources = this. 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 = this. 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 & string. IsNullOrEmpty (CultureName ))
{
// *** No entry there
Value = "";


// *** 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)
New DbResourceDataManager (). AddResource (ResourceKey, value. ToString (), "", this. _ ResourceSetName );


}


Return value;
}


3. Reader class
Copy codeThe Code is as follows:
Public class DbSimpleResourceReader: IResourceReader
{
Private IDictionary _ resources;


Public DbSimpleResourceReader (IDictionary resources)
{
_ Resources = resources;
}
IDictionaryEnumerator IResourceReader. GetEnumerator ()
{
Return _ resources. GetEnumerator ();
}
Void IResourceReader. Close ()
{
}
IEnumerator IEnumerable. GetEnumerator ()
{
Return _ resources. GetEnumerator ();
}
Void IDisposable. Dispose ()
{
}
}

Complete.
I have not tested it. If the test passes, I will provide the most refined source code. Please wait.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.