Resource File Description: Resourcefiles

Source: Internet
Author: User
Tags contains include mscorlib resource thread
<%@ Register tagprefix= "Acme" namespace= "Acme" assembly= "Qstools"%>
<%@ Register tagprefix= "Acme" tagname= "Sourceref" src= "/quickstart/aspplus/util/srcref.ascx"%>

<!--#include virtual= "/quickstart/aspplus/include/header.inc"-->



<div class= "indent" style= "Font-family:verdana; font-size:8pt; " > <b> </b><a class= "Toc2" href= "#create" > Create resources </a><br> <b> </b><a class=" toc2 "href=" #useresource "> Using Resources on page </a><br> <b> </b><a class= " Toc2 "href=" #satellite > Using satellite assemblies </a><br> <b> </b><a class=" toc2 "href=" #satellitecont "> Using satellite Assemblies for controls </a>< br> <b> </b><a class= "Toc2" href= "#summary" > Summary of this section </a><br>
</div>
<p>



<!--BEGIN section--> <a name= "Create" > <span class= "subhead" > Create Resources </span>
<p>

Resource management is a feature of the. NET Framework class Library, which can be used to extract localizable elements from source code and store them as resources along with string keys. At run time, you can use an instance of the <b>ResourceManager</b> class to resolve the key to the original resource or localized version. A resource can be stored as a stand-alone ("loose") file, or as part of an assembly.
<p> asp.net pages can use resource files, and compiled code-behind controls can use resources embedded or linked to their assemblies.
<p>

You can use the <b>ResourceWriter</b> class to create resources programmatically or use tool Resgen.exe to create resources. Resgen.exe can be entered using a simple "key = value" format, or an XML file using the. resx format.
<div class= "code" ><xmp>; Lines beginning with a semicolon can is used for comments. ;

[Strings] Greeting=welcome! More=read More ... </xmp></div>

<b>ResourceWriter</b> and Resgen.exe create a. resources file that can be used separately or as part of an assembly. To include the. resources file in the assembly, use the associated compiler switch or the Al.exe tool. Assemblies that contain only localized resources and that do not contain code are called satellite assemblies.



<!--BEGIN section--> <br> <a name= "Useresource" > <br> <span class= "subhead" > Use resources on page < /SPAN>
<p>

The following example implements only an. aspx page that is localized for each request. The supported languages are English, German, and Japanese. The method for determining the language is to check the <b>Content-Language</b> field of the HTTP headers in the Global.asax file. The contents of this field can be accessed through the <b>UserLanguages</b> collection:
<p>

<p><acme:tabcontrol runat= "Server" >
<tab name= "C #" > Thread.CurrentThread.CurrentCulture = new CultureInfo (request.userlanguages[0));
</Tab>

<tab name= "VB" > Thread.CurrentThread.CurrentCulture = New CultureInfo (request.userlanguages (0))
</Tab>

<tab name= "JScript" > Thread.CurrentThread.CurrentCulture = new CultureInfo (request.userlanguages[0));
</Tab>

</Acme:TabControl><p>

To change the initial language settings, you can use a client that is localized in different ways, or change the language settings on the browser. For example, for Internet Explorer 5.x, select <b> Tools </b>-><b>internet Options </b> from the menu and click the <b> language at the bottom </b > button. In the next dialog box, you can add additional languages and define their precedence. For simplicity, the example always selects the first item.
Once the <p> page is loaded for the first time, the user can select a different culture in the Drop-down list control <b>MyUICulture</b>. If the selected culture is valid, this value overrides the settings obtained from <b>UserLanguages</b>:

<p><acme:tabcontrol runat= "Server" >
<tab name= "C #" > String selectedculture = MyUICulture.SelectedItem.Text; if (! Selectedculture.startswith ("Choose")) {//If Another culture is selected, use that instead. Thread.CurrentThread.CurrentCulture = new CultureInfo (selectedculture); Thread.CurrentThread.CurrentUICulture = new CultureInfo (selectedculture); }
</Tab>

<tab name= "VB" > Dim selectedculture as String = MyUICulture.SelectedItem.Text If not (Selectedculture.startswith (" Choose ")) Then ' If Another culture is selected, use that instead. Thread.CurrentThread.CurrentCulture = new CultureInfo (selectedculture) Thread.CurrentThread.CurrentUICulture = new CultureInfo (selectedculture) End If
</Tab>

<tab name= "JScript" > var selectedculture:string = MyUICulture.SelectedItem.Text; if (! Selectedculture.startswith ("Choose")) {//If Another culture is selected, use that instead. Thread.CurrentThread.CurrentCulture = new CultureInfo (selectedculture); Thread.CurrentThread.CurrentUICulture = new CultureInfo (selectedculture); }
</Tab>

</Acme:TabControl><p>

<p> Similarly, in the Global.asax file, an application-scoped initialization <b>ResourceManager</b> instance is used. In this way, the resource is loaded only once for each application. Lock contention does not occur because the resource is read-only.
<p><acme:tabcontrol runat= "Server" >
<tab name= "C #" > public void Application_Start () {application["RM"] = new ResourceManager ("articles", Server.MapPath ("resources") + Environment.directoryseparatorchar, NULL); }
</Tab>

<tab name= "VB" > Public Sub Application_Start () application ("RM") = New ResourceManager ("articles", _ Server.MapPath ("resources") + Environment.directoryseparatorchar, _ No) end Sub
</Tab>

<tab name= "JScript" > Public Function Application_Start (): void {application["RM"] = new ResourceManager ("articles ", Server.MapPath (" resources ") + Environment.directoryseparatorchar, null);}
</Tab>

</Acme:TabControl><p>

<p>

You can then easily use the Resource Manager on the page. The greeting string is localized only to:
<div class= "code" ><pre> <%=rm. GetString ("greeting")%>
</pre></div>

<table><tr>
<td>
<acme:sourceref
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/NEWS/GLOBAL_ASAX.SRC"
icon= "/quickstart/images/genicon.gif"
Caption= "Global.asax"
runat= "Server"/>
</td>
<td>
<acme:langswitch runat= "Server" >
<CsTemplate>
<acme:sourceref
Runsample= "/quickstart/aspplus/samples/localize/resources/news/cs/news.aspx"
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/NEWS/NEWS.SRC"
icon= "/quickstart/aspplus/images/resources2.gif"
Caption= "C # news.aspx"
runat= "Server"/>
</CsTemplate>
<VbTemplate>
<acme:sourceref
Runsample= "/quickstart/aspplus/samples/localize/resources/news/vb/news.aspx"
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/NEWS/NEWS.SRC"
icon= "/quickstart/aspplus/images/resources2.gif"
caption= "VB news.aspx"
runat= "Server"/>
</VbTemplate>
<JsTemplate>
<acme:sourceref
Runsample= "/quickstart/aspplus/samples/localize/resources/news/js/news.aspx"
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/NEWS/NEWS.SRC"
icon= "/quickstart/aspplus/images/resources2.gif"
caption= "JScript news.aspx"
runat= "Server"/>
</JsTemplate>
</Acme:LangSwitch>
</td>
</tr></table>

<!--BEGIN section--> <br> <a name= "Satellite" > <br> <span class= "subhead" > Using satellite Assemblies </ Span>
<p>

Looking at the directory structure in the previous example, you can see that the sample resources are loaded from the. resource file instead of being loaded from the DLL. Although this is indeed a solution, you can also compile code into satellite assemblies. By definition, a satellite assembly is an assembly that contains only resources and does not contain executable code. For more information about satellite assemblies, see <a href= "/quickstart/howto/doc/createresources.aspx" > How ... Create a resource? </A> section. The <p> resources files are not replicated because they are not DLLs, so WEB sites may experience locking problems when they use them. Recognizing this, the advantages of using satellite assemblies become apparent.  Another approach is to use the parallel main assembly of an application resource. The main assembly contains the resources that can be relied upon, and a satellite assembly (one per culture) contains localized resources. The main assembly is installed in the \ Bin directory, and the satellite assembly is stored in the normal XX-XX subdirectory (see <a href= "/quickstart/howto/doc/createresources.aspx" > How ... Create a resource? </A>). As assemblies, they are shadow copied and are not locked. To create an. asp application that is recognized by an assembly:
<p>

<ol>
<li> Create a resource DLL and copy it to the \ Bin directory. For example:<p> ResGen qq.txt qq.resources<br> al/embed:qq.resources, qq.resources, y/out:qq.dll<p> "y" instructions  Whether a Blob should be visible to other assemblies.  Because <b>ResourceManager</b> is located in Mscorlib and is a different assembly from "QQ", the. resources file must be publicly visible. "Y" indicates whether this should be public. <p>
<li> include the following statement on the page. Note that the assembly name here is located in the <b>System.Reflection</b> namespace defined in Mscorlib (always refer to it at compile time):
<p><acme:tabcontrol runat= "Server" >
<tab name= "C #" > <% Assembly a = Assembly.Load ("QQ"); ResourceManager rm = new ResourceManager ("QQ", a); Response.Write (rm. GetString ("key")); %>
</Tab>
<tab name= "VB" > <% Dim A As Assembly = Assembly.Load ("QQ") Dim rm as ResourceManager = New ResourceManager ("QQ", A) Response.Write (rm. GetString ("key"))%>
</Tab>
<tab name= "JScript" > <% var a:assembly = Assembly.Load ("QQ"); var rm:resourcemanager = new ResourceManager ("QQ", a); Response.Write (rm. GetString ("key")); %>
</Tab>
</Acme:TabControl>
<P>
<li> compile each subordinate resource into its own assembly and put it into the correct directory structure that is required in the/bin directory:<p>

Al/embed:qq.en-us.resources,qq.en-us.resources, y/out:qq.resources.dll/c:en-us<p>

Replace the code with the culture you are localizing to <i>en-US</i>. Remember that the,<b>/c:</b> tag is a culture specifier.
</ol> After you have placed the DLL in the correct location (/bin and/bin/en-us in the example above), you can retrieve the resource appropriately. Note that all images are copied through the assembly cache and are therefore replaceable, thereby avoiding potential locking.
<p>

<!--BEGIN section--> <br> <a name= "Satellitecont" > <br> <span class= "Subhead" > Using satellite assemblies for controls </span>
<p>

Compiled code-behind controls can also use satellite assemblies to provide localized content. From a deployment standpoint, this is especially good because satellite assemblies can be version-independent of the code. Therefore, you can provide support for additional languages by simply replicating the satellite assembly's modules to the server without requiring code changes. <p>

The following example contains the <b>LocalizedButton</b> control in Assembly <b>LocalizedControls</b> (module LocalizedControls.dll). Compile the control to register on the page showcontrols.aspx and use it later:

<div class= "code" ><pre> <% @Register tagprefix= "Loc" namespace= "Localizedcontrols"%> ... <loc: Localizedbutton runat= "Server" text= "OK"/>
</pre></div>

<p> <b>LocalizedButton</b> Control Storage <b>ResourceManager</b> instance, which is provided by <b> All instances of localizedbutton</b> are shared. The value of the,<b>text</b> property is replaced by a localized version whenever a control is rendered:

<p><acme:tabcontrol runat= "Server" >
<tab name= "C #" > _rm = new ResourceManager ("Localizedstrings", assembly.getexecutingassembly (), NULL, true); ... override protected void Render (HtmlTextWriter writer) {text = ResourceFactory.RManager.GetString (text); Render (writer); }
</Tab>

<tab name= "VB" > _rm = New ResourceManager ("Localizedstrings", _ Assembly.getexecutingassembly (), _ Nothing, _ True) ... Overrides Protected Sub Render (writer as htmltextwriter) Text = ResourceFactory.RManager.GetString (text) base. Render (writer) End Sub
</Tab>

<tab name= "JScript" > _rm = new ResourceManager ("Localizedstrings", assembly.getexecutingassembly (), NULL, true);. .. Override protected function Render (writer:htmltextwriter): void {text = ResourceFactory.RManager.GetString (text); base . Render (writer); }
</Tab>

</Acme:TabControl><p>

The <b>ResourceManager</b> instance is responsible for resolving keys to localized resources. If a satellite assembly with the correct culture is not available and the associated culture is not found, the language-neutral resource for the main assembly is used ("en-us" <nobr>-></nobr> "en" <nobr>-></ Nobr> "Non-specific language"). You can provide another language support by simply copying the module files for the new satellite assembly to the appropriate location.
<p>

<table><tr>
<td>
<acme:sourceref
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/CONTROLS/CONTROLS.SRC"
icon= "/quickstart/images/genicon.gif"
caption= "Localized Controls"
runat= "Server"/>
</td>
<td>
<acme:langswitch runat= "Server" >
<CsTemplate>
<acme:sourceref
Runsample= "/quickstart/aspplus/samples/localize/resources/showcontrols/cs/showcontrols.aspx"
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/SHOWCONTROLS/SHOWCONTROLS.SRC"
icon= "/quickstart/aspplus/images/resources1.gif"
Caption= "C # Using localized Controls"
runat= "Server"/>
</CsTemplate>
<VbTemplate>
<acme:sourceref
Runsample= "/quickstart/aspplus/samples/localize/resources/showcontrols/vb/showcontrols.aspx"
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/SHOWCONTROLS/SHOWCONTROLS.SRC"
icon= "/quickstart/aspplus/images/resources1.gif"
caption= "VB Using localized Controls"
runat= "Server"/>
</VbTemplate>
<JsTemplate>
<acme:sourceref
Runsample= "/quickstart/aspplus/samples/localize/resources/showcontrols/js/showcontrols.aspx"
Viewsource= "/QUICKSTART/ASPPLUS/SAMPLES/LOCALIZE/RESOURCES/SHOWCONTROLS/SHOWCONTROLS.SRC"
icon= "/quickstart/aspplus/images/resources1.gif"
caption= "JScript Using localized Controls"
runat= "Server"/>
</JsTemplate>
</Acme:LangSwitch>
</td>
</tr></table>

<p>


<ol>
<li>asp.net pages can use resource classes to isolate localizable content from a resource, which is selected at run time.
<li> Another good approach is to use satellite assemblies instead of the intermediate. resources file to load the resource because it avoids locking problems.
<li> compiled controls can contain their own resources and will choose the correct localized content based on the <b>UICulture</b> of the host home page.
</ol>


<!--#include virtual= "/quickstart/aspplus/include/footer.inc"-->





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.