Resources are xml files containing string key-value pairs to support globalization. There are two types of resources - local and global. Global resources are shared across whole application, they stay in a specific location(~\App_LocalResources). On the contrary, local resource files are used only for specific pages, they can be stored anywhere, but the file names are formated as [page.aspx.language.resx], as such, a local resource file is linked to a page. The default resource, which is used if no specific resource file matched, is named [page.aspx.resx]
Local resource keys have to follow a naming convention - [control].[property], e.g. "button1.Text" because it is accessed implicitly. When define a control instance, specify a reference to the control part in the resource key, the resource values will be automatically applied to properties. E.g.
<asp:Button ID="Button1" runat="server" Text="DefaultText"
meta:resourcekey="Button1" />
Above, meta:attribute is the implicit resource accessor. A resources can also be accessed explicitly, e.g.
<asp:Button ID="Button1" runat="server"
Text="<%$ Resources:WebResources, Button1Caption %>" />
This is refered as resource expression, formmated as <%$Resources:Class,ResourceID%>, used to access local and global resources. Local resources override global ones if there is a conflict key.
For paragraphs of static text, <asp:Localize> control is just to render that. E.g.
<asp:Localize runat=server
ID="WelcomeMessage"
Text="Welcome!" meta:resourcekey="LiteralResource1" />
Default resource file is compiled into the main assembly, while culture specific resources are compiled into satelite assemblies.
Embedded Javascript Library
Javascript library can also be embeded like resurces in an assembly. These javacript code can even use localized strings to support globalization. To do this, create a class library project, set reference to System.Web and System.Web.Extensions, then create a .js file and specify the Build Action as Embedded Resource, so the script file will be embedded into the assemply.
Mark the assembly
[assembly: System.Web.UI.WebResource("LocalizingScriptResources.CheckAnswer.js", "application/x-javascript")]
[assembly: System.Web.UI.ScriptResource("LocalizingScriptResources.CheckAnswer.js", "LocalizingScriptResources.VerificationResources", "Answer")]
Following is the js script file that embedded into the assembly. In it, Answer is a resource key reference to let the script to use local resources.
//CheckAnswer.js
function CheckAnswer()
{
var firstInt = $get('firstNumber').innerText;
var secondInt = $get('secondNumber').innerText;
var userAnswer = $get('userAnswer');
if ((Number.parseLocale(firstInt) + Number.parseLocale(secondInt)) == userAnswer.value)
{
alert(Answer.Correct);
return true;
}
else
{
alert(Answer.Incorrect);
return false;
}
}
Programatically in backend code we use the local resources by ResourceManager, whose GetString() is for accessing a resource.
ResourceManager rm = new ResourceManager("LocalizingScriptResources.VerificationResources", this.GetType().Assembly);
_button.Text = rm.GetString("Verify");
_button.OnClientClick = "return CheckAnswer();";
VerificationResources.resx
Correct => "Yes, your answer is correct."
Incorrect => "No, your answer is incorrect."
Verify => "Verify Answer"
VerificationResources.it.resx
Correct => "Si, la risposta e’ corretta."
Incorrect => "No, la risposta e’ sbagliata."
Verify => "Verificare la risposta"
Above two resource files and the js script file is embeded into the assembly named LocalizingScriptResources when compiled.
It also generated a LocalizingScriptResources.resources.dll as "satelite" assembly.
Asp.Net form use the script and resources
<%@ Page Language="C#" AutoEventWireup="true" UICulture="auto" Culture="auto" %>
<%@ Register TagPrefix="Samples" Namespace="LocalizingScriptResources" Assembly="LocalizingScriptResources" %>
<asp:ScriptManager ID="ScriptManager1" EnableScriptLocalization="true" runat="server">
<Scripts>
<asp:ScriptReference Assembly="LocalizingScriptResources" Name="LocalizingScriptResources.CheckAnswer.js" />
</Scripts>
Above, the ScriptManager tag is to generate javascript client library customized with given parameters, e.g. EnableScriptLocalization="true" in above code snippet. The followed Scritps tag is familiar to us, under the hood it is like we do with html <script> tag, embeds an external javascript file into the page. The only difference is that it doesn't get it from file path, but rather from an assembly.
No comments:
Post a Comment