home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / resourcefiles.aspx < prev    next >
Encoding:
Text File  |  2000-06-11  |  7.6 KB  |  216 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  4.  
  5. <h4>Working with Resource Files</h4>
  6.  
  7.  
  8. <div class="indent" style="font-family:Verdana; font-size:8pt;">
  9. <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  10. <a class="toc2" target="content" href="#create">Creating Resources</a><br>
  11. <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  12. <a class="toc2" target="content" href="#useresource">Using Resources on a Page</a><br>
  13. <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  14. <a class="toc2" target="content" href="#satellite">Using Satellite Assemblies for Controls</a><br>
  15. </div>
  16. <p>
  17. <hr>
  18.  
  19.  
  20.  
  21. <!--BEGIN SECTION-->
  22. <a name="create">
  23. <span class="subhead">Creating Resources</span>
  24. <p>
  25.  
  26. Resource management is a feature of the base class libraries, which can be used to extract
  27. localizable elements from source code and to store them with a string key as
  28. resources. At runtime an instance of the <b>ResourceManager</b> class can be used to resolve
  29. the key to the original resource or a localized version. Resources can be stores as
  30. independent ("loose") files or as a part of an assembly.
  31. <p>
  32. ASP+ pages can utilize resource files, whereas compiled code-behind controls also can use
  33. resources embedded or linked into their assembly.
  34. <p>
  35.  
  36. Resources can be created via the <b>ResourceWriter</b> class programmatically or by the tool 
  37. resgen.exe . The resgen tool uses a simple key=value format as input (note, that resgen
  38. will be updated to support more formats):
  39. <div class="code"><xmp>
  40. ;
  41. ; Lines beginning with a semicolon can be used for comments. 
  42. ;
  43.  
  44. [strings]
  45. greeting=Welcome !
  46. more=Read more ...
  47. ...
  48. </xmp></div>
  49.  
  50. <b>ResourceWriter</b> and <b>ResGen</b> create a .resources file, which can be used as is or as part
  51. of an assembly. To include a .resources file in an assembly use the related compiler
  52. switch or the al.exe tool. Assemblies containing only localized resources and no code 
  53. are called satellite assemblies.
  54.  
  55.  
  56.  
  57. <!--BEGIN SECTION-->
  58. <br>
  59. <a name="useresource">
  60. <br>
  61. <span class="subhead">Using Resources on a Page</span>
  62. <p>
  63.  
  64. The following sample implements only one .aspx page, which is localized 
  65. for each request. The supported languages are English, German and Japanese.
  66. The language is determined by examining the <b>Content-Language</b> field  
  67. of the HTTP header in the global.asax file. The contents of the
  68. field are accessible through the <b>UserLanguages</b> collection:
  69. <p>
  70.  
  71. <div class="code"><xmp>
  72. Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[0]);
  73. </xmp></div>
  74.  
  75. To change the initial language setting you can use differently localized clients or
  76. change the language setting on your browser. For Internet Explorer 5.x for
  77. example select Tools -> Internet Options from the menu and click the "Languages..."
  78. button at the bottom. In the following dialog you can add additional languages and
  79. define their priority. For simplicity the sample always chooses the first entry.
  80. <p>
  81. After the page is loaded the first time, the user can select another culture in the 
  82. drop-down list control MyUICulture. If a valid culture is selected, this value
  83. will override the setting aquired from <b>UserLanguages</b>:
  84.  
  85. <div class="code"><xmp>
  86. String SelectedCulture = MyUICulture.SelectedItem.Text;
  87. if(! SelectedCulture.StartsWith("Choose")) 
  88. {
  89.     // If another culture was selected, use that instead.
  90.     Thread.CurrentThread.CurrentCulture = new CultureInfo(SelectedCulture);
  91.     Thread.CurrentThread.CurrentUICulture = new CultureInfo(SelectedCulture);
  92. }
  93. </xmp></div>
  94.  
  95.  
  96. <p>
  97. Also in the global.asax file a <b>ResourceManager</b> instance with application scope
  98. is initialized. This way resources are only loaded once per application. As
  99. resources are readonly, no lock contention should occur.
  100. <div class="code"><xmp>
  101. public void Application_OnStart() 
  102. {
  103.     Application["RM"] = new ResourceManager("articles", 
  104.                 Server.MapPath("resources") + Environment.DirectorySeparatorChar, 
  105.                 null);
  106. }
  107. </xmp></div>
  108. <p>
  109.  
  110. The resource manager then can easily be used on the page. The greeting string is
  111. simple localized by:
  112. <div class="code"><pre>
  113. <%=rm.GetString("greeting")%>
  114. </pre></div>
  115.  
  116. <table><tr>
  117. <td>
  118. <Acme:SourceRef 
  119.   ViewSource="/quickstart/aspplus/samples/localize/resources/global_asax.src"
  120.   Icon="/quickstart/images/genicon.gif"
  121.   Caption="global.asax"
  122.   runat="server" />
  123. </td>
  124. <td>
  125. <Acme:SourceRef 
  126.   RunSample="/quickstart/aspplus/samples/localize/resources/news.aspx" 
  127.   ViewSource="/quickstart/aspplus/samples/localize/resources/news.src"
  128.   Icon="/quickstart/aspplus/images/resources1.gif"
  129.   Caption="news.aspx"
  130.   runat="server" />
  131. </td>
  132. </tr></table>
  133.  
  134. <!--BEGIN SECTION-->
  135. <br>
  136. <a name="satellite">
  137. <br>
  138. <span class="subhead">Using Satellite Assemblies for Controls</span>
  139. <p>
  140.  
  141. Compiled code-behind controls can also use satellite assemblies to supply
  142. localized content. From a deployment perspective this is an especially good
  143. thing, as satellite assemblies can be version independent from the code,
  144. so that support for additional languages can be provided just by copying
  145. the module of the satellite to the server. No code has to be changed for
  146. this.<p>
  147.  
  148. The following sample contains the "LocalizedButton" control in the 
  149. assembly "LocalizedControls" (module LocalizedControls.dll). On the page
  150. showcontrols.aspx the compiled control is registered and used later on:
  151.  
  152. <div class="code"><pre>
  153. <%@Register TagPrefix="Loc" namespace="LocalizedControls" %>
  154. ...
  155. <Loc:LocalizedButton runat="server" Text="ok" />
  156. </pre></div>
  157.  
  158. <p>
  159. The LocalizedButton control stores a <b>ResourceManager</b> instance, which is
  160. shared by all instances of LocalizedButton. Whenever a control is 
  161. rendered, the value of the Text property is replaced with the localized
  162. version:
  163.  
  164. <div class="code"><xmp>
  165. _rm = new ResourceManager("LocalizedStrings",
  166.                           Assembly.GetExecutingAssembly(),
  167.                           null,
  168.                           true );
  169. ...
  170. override protected void Render (HtmlTextWriter writer) 
  171. {
  172.     Text = ResourceFactory.RManager.GetString(Text);
  173.     base.Render(writer);
  174. }
  175. </xmp></div>
  176.  
  177. The <b>ResourceManager</b> instance is responsible to resolve the key to a
  178. localized resource. If a satellite assembly with the correct culture
  179. is not available and no related culture is found, the neutral resource 
  180. of the main assembly is used ("en-us" <nobr>-></nobr> "en" <nobr>-></nobr>
  181. neutral). Support for another language is simply granted by copying the
  182. module file for the new satellite assembly in place.
  183. <p>
  184.  
  185. <table><tr>
  186. <td>
  187. <Acme:SourceRef 
  188.   ViewSource="/quickstart/aspplus/samples/localize/resources/controls.src"
  189.   Icon="/quickstart/images/genicon.gif"
  190.   Caption="Localized Controls"
  191.   runat="server" />
  192. </td>
  193. <td>
  194. <Acme:SourceRef 
  195.   RunSample="/quickstart/aspplus/samples/localize/resources/showcontrols.aspx" 
  196.   ViewSource="/quickstart/aspplus/samples/localize/resources/showcontrols.src"
  197.   Icon="/quickstart/aspplus/images/resources2.gif"
  198.   Caption="Using Localized Controls"
  199.   runat="server" />
  200. </td>
  201. </tr></table>
  202.  
  203. <p>
  204.  
  205.  
  206. <h4>Section Summary</h4>
  207. <ol>
  208. <li>ASP+ pages can utilize the resource classes to isolate localizable content
  209. in resources, which are selected at runtime.
  210. <li>Compiled controls can contain resources of their own and will select the
  211. right localized content depending on the UICulture of the hosting page.
  212. </ol>
  213.  
  214.  
  215. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->
  216.