{"id":3483,"date":"2004-10-05T01:14:00","date_gmt":"2004-10-05T01:14:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/heaths\/2004\/10\/05\/improvements-to-localization-in-visual-studio-2005\/"},"modified":"2024-03-15T15:17:34","modified_gmt":"2024-03-15T23:17:34","slug":"improvements-to-localization-in-visual-studio-2005","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/setup\/improvements-to-localization-in-visual-studio-2005\/","title":{"rendered":"Improvements to Localization in Visual Studio 2005"},"content":{"rendered":"<p>Designing and developing applications for the global economy can often be difficult, but the .NET Framework made <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpguide\/html\/cpconglobalization.asp\">globalization<\/a>\u00a0and <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpguide\/html\/cpconlocalization.asp\">localization<\/a>\u00a0much more simple. In <a href=\"http:\/\/lab.msdn.microsoft.com\/vs2005\/\">Visual Studio 2005<\/a>\u00a0resources are handled a little bit differently, providing you &#8211; the application developer &#8211; a class you can easily use throughout your application to access resources as their native Type.<\/p>\n<p>One major advancement I&#8217;ve had a chance to use is the <code>ComponentResourceManager<\/code>. This class helps overcome many of the performance problems of the <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpref\/html\/frlrfsystemresourcesresourcemanagerclasstopic.asp\">ResourceManager<\/a>. Before in Visual Studio 7.0 (2002) and newer when you set the <code>Localizable<\/code> property to true for a <code>Form<\/code>, every property attributed with <code>LocalizableAttribute(true)<\/code> was added to the <code>Form<\/code>&#8216;s ResX file and a call to <code>ResourceManager.GetObject<\/code> &#8211; along with a cast to the appropriate Type &#8211; was added to <code>InitializeComponent<\/code>.<\/p>\n<p>The major drawback was that properties that you may or may not localize were using a lot of space in your primary assembly &#8211; the assembly that contains your embedded IL modules. The extra calls &#8211; while necessary to fully support <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpguide\/html\/cpconlocalizability.asp\">localizability<\/a> &#8211; cost a lot in terms of performance since the embedded .resources file must be parsed, cached, and objects must be cast, often times to value types (which requires <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/csref\/html\/vclrfunboxingconversionpg.asp\">unboxing<\/a>, an expensive operation).<\/p>\n<p>In Visual Studio 2005 performance and cost in terms of file size is improved thanks to the <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpref\/html\/frlrfsystemcomponentmodelcomponentresourcemanagerclasstopic.asp\">ComponentResourceManager<\/a>, a derivative of the <code>ResourceManager<\/code>. The designer needs only to instantiate this class once using the type of the container you&#8217;re localizing &#8211; like a <code>Form<\/code> &#8211; and makes only a single call to <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpref\/html\/frlrfSystemComponentModelComponentResourceManagerClassApplyResourcesTopic.asp\">ApplyResources<\/a> for each component that requires localization. This method differs from many calls to <code>GetObject<\/code> because the ResX file needs to only store the resources to be localized, since <code>ApplyResources<\/code> enumerates all the resources for a particular control, and caches them for a particular culture.<\/p>\n<p>The <code>ComponentResourceManager<\/code> class, however, is not new to the BCL. In fact it has been around since the .NET Framework 1.0 RTM. Visual Studio 2005 is the first to use it from the form designer.<\/p>\n<p>To use the <code>ComponentResourceManager<\/code> in your class, you can use an existing <code>Form<\/code> of create a new <code>Form<\/code>. For brevity, I&#8217;ll assume you&#8217;re upgrading an existing <code>Form<\/code>. First change declaration of the <code>ResourceManager<\/code> to <code>ComponentResourceManager<\/code> and instantiate a new instance <code>ComponentResourceManager<\/code> in <code>InitializeComponent<\/code>.<\/p>\n<pre><span class=\"Type\">private<\/span> System.ComponentModel.ComponentResourceManager resources;\r\n<span class=\"Type\">private<\/span> <span class=\"Type\">void<\/span> InitializeComponent()\r\n{\r\n <span class=\"Statement\">this<\/span>.resources = <span class=\"Statement\">new<\/span> System.ComponentModel.ComponentResourceManager(<span class=\"Statement\">this<\/span>.GetType());\r\n <span class=\"Comment\">\/\/ ...\r\n<\/span>}<\/pre>\n<p>Now for each component you want to localize add a call to <code>ApplyResources<\/code> passing the name of the component as well as the instance of the component itself.<\/p>\n<pre><span class=\"Statement\">this<\/span>.resources.ApplyResources(<span class=\"Constant\">\"textBox1\"<\/span>, <span class=\"Statement\">this<\/span>.textBox1);\r\n<span class=\"Statement\">this<\/span>.textBox1.Name = <span class=\"Constant\">\"textBox1\"<\/span>;\r\n<span class=\"Statement\">this<\/span>.resources.ApplyResources(<span class=\"Constant\">\"button1\"<\/span>, <span class=\"Statement\">this<\/span>.button1);\r\n<span class=\"Statement\">this<\/span>.button1.Name = <span class=\"Constant\">\"button1\"<\/span>;<\/pre>\n<p>When <code>ApplyResources<\/code> is called the .resources embedded resource matching the fully-qualified class name is loaded and parsed, enumerating all the property values. Each key in the .resources file that beings with <i>objectName<\/i> is matched against a property of the named object and assigned for you. Numerous calls to <code>GetObject<\/code> are no longer required and your application is still fully localizable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Designing and developing applications for the global economy can often be difficult, but the .NET Framework made globalization\u00a0and localization\u00a0much more simple. In Visual Studio 2005\u00a0resources are handled a little bit differently, providing you &#8211; the application developer &#8211; a class you can easily use throughout your application to access resources as their native Type. One [&hellip;]<\/p>\n","protected":false},"author":389,"featured_media":3843,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[14,22],"class_list":["post-3483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-development","tag-localization"],"acf":[],"blog_post_summary":"<p>Designing and developing applications for the global economy can often be difficult, but the .NET Framework made globalization\u00a0and localization\u00a0much more simple. In Visual Studio 2005\u00a0resources are handled a little bit differently, providing you &#8211; the application developer &#8211; a class you can easily use throughout your application to access resources as their native Type. One [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts\/3483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/users\/389"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/comments?post=3483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts\/3483\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/media\/3843"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/media?parent=3483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/categories?post=3483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/tags?post=3483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}