home *** CD-ROM | disk | FTP | other *** search
- <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
-
- <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
-
- <h4>Working with Resource Files</h4>
-
-
- <div class="indent" style="font-family:Verdana; font-size:8pt;">
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#create">Creating Resources</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#useresource">Using Resources on a Page</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#satellite">Using Satellite Assemblies for Controls</a><br>
- </div>
- <p>
- <hr>
-
-
-
- <!--BEGIN SECTION-->
- <a name="create">
- <span class="subhead">Creating Resources</span>
- <p>
-
- Resource management is a feature of the base class libraries, which can be used to extract
- localizable elements from source code and to store them with a string key as
- resources. At runtime an instance of the <b>ResourceManager</b> class can be used to resolve
- the key to the original resource or a localized version. Resources can be stores as
- independent ("loose") files or as a part of an assembly.
- <p>
- ASP+ pages can utilize resource files, whereas compiled code-behind controls also can use
- resources embedded or linked into their assembly.
- <p>
-
- Resources can be created via the <b>ResourceWriter</b> class programmatically or by the tool
- resgen.exe . The resgen tool uses a simple key=value format as input (note, that resgen
- will be updated to support more formats):
- <div class="code"><xmp>
- ;
- ; Lines beginning with a semicolon can be used for comments.
- ;
-
- [strings]
- greeting=Welcome !
- more=Read more ...
- ...
- </xmp></div>
-
- <b>ResourceWriter</b> and <b>ResGen</b> create a .resources file, which can be used as is or as part
- of an assembly. To include a .resources file in an assembly use the related compiler
- switch or the al.exe tool. Assemblies containing only localized resources and no code
- are called satellite assemblies.
-
-
-
- <!--BEGIN SECTION-->
- <br>
- <a name="useresource">
- <br>
- <span class="subhead">Using Resources on a Page</span>
- <p>
-
- The following sample implements only one .aspx page, which is localized
- for each request. The supported languages are English, German and Japanese.
- The language is determined by examining the <b>Content-Language</b> field
- of the HTTP header in the global.asax file. The contents of the
- field are accessible through the <b>UserLanguages</b> collection:
- <p>
-
- <div class="code"><xmp>
- Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[0]);
- </xmp></div>
-
- To change the initial language setting you can use differently localized clients or
- change the language setting on your browser. For Internet Explorer 5.x for
- example select Tools -> Internet Options from the menu and click the "Languages..."
- button at the bottom. In the following dialog you can add additional languages and
- define their priority. For simplicity the sample always chooses the first entry.
- <p>
- After the page is loaded the first time, the user can select another culture in the
- drop-down list control MyUICulture. If a valid culture is selected, this value
- will override the setting aquired from <b>UserLanguages</b>:
-
- <div class="code"><xmp>
- String SelectedCulture = MyUICulture.SelectedItem.Text;
- if(! SelectedCulture.StartsWith("Choose"))
- {
- // If another culture was selected, use that instead.
- Thread.CurrentThread.CurrentCulture = new CultureInfo(SelectedCulture);
- Thread.CurrentThread.CurrentUICulture = new CultureInfo(SelectedCulture);
- }
- </xmp></div>
-
-
- <p>
- Also in the global.asax file a <b>ResourceManager</b> instance with application scope
- is initialized. This way resources are only loaded once per application. As
- resources are readonly, no lock contention should occur.
- <div class="code"><xmp>
- public void Application_OnStart()
- {
- Application["RM"] = new ResourceManager("articles",
- Server.MapPath("resources") + Environment.DirectorySeparatorChar,
- null);
- }
- </xmp></div>
- <p>
-
- The resource manager then can easily be used on the page. The greeting string is
- simple localized by:
- <div class="code"><pre>
- <%=rm.GetString("greeting")%>
- </pre></div>
-
- <table><tr>
- <td>
- <Acme:SourceRef
- ViewSource="/quickstart/aspplus/samples/localize/resources/global_asax.src"
- Icon="/quickstart/images/genicon.gif"
- Caption="global.asax"
- runat="server" />
- </td>
- <td>
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/localize/resources/news.aspx"
- ViewSource="/quickstart/aspplus/samples/localize/resources/news.src"
- Icon="/quickstart/aspplus/images/resources1.gif"
- Caption="news.aspx"
- runat="server" />
- </td>
- </tr></table>
-
- <!--BEGIN SECTION-->
- <br>
- <a name="satellite">
- <br>
- <span class="subhead">Using Satellite Assemblies for Controls</span>
- <p>
-
- Compiled code-behind controls can also use satellite assemblies to supply
- localized content. From a deployment perspective this is an especially good
- thing, as satellite assemblies can be version independent from the code,
- so that support for additional languages can be provided just by copying
- the module of the satellite to the server. No code has to be changed for
- this.<p>
-
- The following sample contains the "LocalizedButton" control in the
- assembly "LocalizedControls" (module LocalizedControls.dll). On the page
- showcontrols.aspx the compiled control is registered and used later on:
-
- <div class="code"><pre>
- <%@Register TagPrefix="Loc" namespace="LocalizedControls" %>
- ...
- <Loc:LocalizedButton runat="server" Text="ok" />
- </pre></div>
-
- <p>
- The LocalizedButton control stores a <b>ResourceManager</b> instance, which is
- shared by all instances of LocalizedButton. Whenever a control is
- rendered, the value of the Text property is replaced with the localized
- version:
-
- <div class="code"><xmp>
- _rm = new ResourceManager("LocalizedStrings",
- Assembly.GetExecutingAssembly(),
- null,
- true );
- ...
- override protected void Render (HtmlTextWriter writer)
- {
- Text = ResourceFactory.RManager.GetString(Text);
- base.Render(writer);
- }
- </xmp></div>
-
- The <b>ResourceManager</b> instance is responsible to resolve the key to a
- localized resource. If a satellite assembly with the correct culture
- is not available and no related culture is found, the neutral resource
- of the main assembly is used ("en-us" <nobr>-></nobr> "en" <nobr>-></nobr>
- neutral). Support for another language is simply granted by copying the
- module file for the new satellite assembly in place.
- <p>
-
- <table><tr>
- <td>
- <Acme:SourceRef
- ViewSource="/quickstart/aspplus/samples/localize/resources/controls.src"
- Icon="/quickstart/images/genicon.gif"
- Caption="Localized Controls"
- runat="server" />
- </td>
- <td>
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/localize/resources/showcontrols.aspx"
- ViewSource="/quickstart/aspplus/samples/localize/resources/showcontrols.src"
- Icon="/quickstart/aspplus/images/resources2.gif"
- Caption="Using Localized Controls"
- runat="server" />
- </td>
- </tr></table>
-
- <p>
-
-
- <h4>Section Summary</h4>
- <ol>
- <li>ASP+ pages can utilize the resource classes to isolate localizable content
- in resources, which are selected at runtime.
- <li>Compiled controls can contain resources of their own and will select the
- right localized content depending on the UICulture of the hosting page.
- </ol>
-
-
- <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->
-