{"id":1315,"date":"2015-01-20T14:36:37","date_gmt":"2015-01-20T14:36:37","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=1315"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"localization-using-resx-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/localization-multi-language-support\/localization-using-resx-files","title":{"rendered":"Localization using *.resx files"},"content":{"rendered":"<p>\nYou can create a separate resources file for each language&nbsp;in managed C++ which&nbsp;contain&nbsp;all the strings used by the application.\n<\/p>\n<p>\nIn the solution explorer, right-click the Resource Files folder for&nbsp;the&nbsp;project&nbsp;&gt; Add &gt;&nbsp;New Item &gt; Visual C++ &gt; Resource &gt;&nbsp;Assembly Resource File (.resx).\n<\/p>\n<p style=\"margin-left: 40px;\">\nGive it the name AppLocalization.resx for example.\n<\/p>\n<p>\nAdd your strings to the file.\n<\/p>\n<h4>\nImportant<br \/>\n<\/h4>\n<p>\nYour&nbsp;project properties &quot;Common Language Runtime Support&quot; setting must not be set to &quot;\/clr:safe&quot;. If it is the non default resource files&nbsp;won&#39;t get used. &nbsp;Setting it to&nbsp;&quot;\/clr:pure works&nbsp;fine.\n<\/p>\n<h4>\nAdding files for the other languages<br \/>\n<\/h4>\n<p>\nCreate the files with the same name and the localization&nbsp;characters added:\n<\/p>\n<p style=\"margin-left: 40px;\">\nFrench: AppLocalization.fr.resx<br \/>\nFrench-France: AppLocalization.fr-FR.resx\n<\/p>\n<p>\n<a href=\"https:\/\/msdn.microsoft.com\/en-gb\/library\/ee825488(v=cs.20).aspx\">Table of Language Culture Names, Codes, and ISO Values<\/a>\n<\/p>\n<h4>\nUsing On&nbsp;Forms<br \/>\n<\/h4>\n<p>\nDon&#39;t set the form to &#39;Localizable&#39; in its properties as that will create a resx file specifically for the form and let visual studio handle the multi languages (another way of doing all this, but not as simple as a single languages file when you want to be able to&nbsp;send it&nbsp;off and have others provide translations as this is more aimed at the localizations being carried out within visual studio).\n<\/p>\n<h5>\nSetting Strings Of Form Controls<br \/>\n<\/h5>\n<p>\nIt would be nice to be able to use&nbsp;the rm-&gt;GetString() in the auto generated form designer code but if you do you&#39;ll break the design time ability to design your form as the strings can only be found at run time. &nbsp;So you need to set strings in say the form Load event&nbsp;instead.\n<\/p>\n<h4>\nUsing The Strings<br \/>\n<\/h4>\n<h5>\nLocally accessing the resource file<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tResources::ResourceManager^ rm1 = gcnew Resources::ResourceManager(L&quot;MyProjectsNamespace.AppLocalization&quot;, this-&gt;GetType()-&gt;Assembly);\r\n\tthis-&gt;Text = rm1-&gt;GetString(L&quot;My String Name&quot;);\r\n<\/code>\r\n<\/pre>\n<p>\n&quot;MyProjectsNamespace&quot; is the project name \/ namespace\n<\/p>\n<p>\n&quot;AppLocalization&quot; is the name of your .resx files before the extension.\n<\/p>\n<h5>\nCreating a global object<br \/>\n<\/h5>\n<p>\nIn stdafx.h\n<\/p>\n<pre>\r\n<code>\r\nref class GlobalObjects\r\n{\r\n\tpublic: static System::Resources::ResourceManager ^rm1;\r\n};\r\n<\/code><\/pre>\n<p>\nIn the constructor of your main form\n<\/p>\n<pre>\r\n<code>\r\n\tGlobalObjects::rm1 = gcnew System::Resources::ResourceManager(L&quot;MyProjectsNamespace.AppLocalization&quot;, this-&gt;GetType()-&gt;Assembly);\r\n<\/code><\/pre>\n<p>\nThen to use it\n<\/p>\n<pre>\r\n<code>\r\n\tthis-&gt;Text = GlobalObjects::rm1-&gt;GetString(L&quot;My String Name&quot;);\r\n<\/code><\/pre>\n<h4>\nLanguage Identifiers<br \/>\n<\/h4>\n<p>\n&quot;fr&quot; means french. &nbsp;&quot;fr-FR&quot; means French&nbsp;in France, whereas &quot;fr-BE&quot; means&nbsp;French in&nbsp;Belgium. You can use both &quot;fr&quot; and &quot;fr-##&quot; files&nbsp;with territory specific strings in the latter and more generic strings in the former. &nbsp;If it can&#39;t find a&nbsp;string in the specific teritory file it will drop down to the language file instead and look for it there.\n<\/p>\n<p>\nSo for general French translations use a file .fr.resx and if you need to add specific translations for a particular territory add the file for that territory also.\n<\/p>\n<h4>\nTesting Translations<br \/>\n<\/h4>\n<h5>\nTesting Individually<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tthis-&gt;Text = GlobalObjects::rm1-&gt;GetString(L&quot;My String Name&quot;, gcnew System::Globalization::CultureInfo(&quot;fr-FR&quot;));\r\n<\/code><\/pre>\n<h5>\nTesting Globally<br \/>\n<\/h5>\n<p>\nJust add these 2 CultureInfo&nbsp;settings before creating the&nbsp;ResourceManager:\n<\/p>\n<pre>\r\n<code>\r\n\tSystem::Threading::Thread::CurrentThread-&gt;CurrentCulture = gcnew System::Globalization::CultureInfo(&quot;fr&quot;);\r\n\tSystem::Threading::Thread::CurrentThread-&gt;CurrentUICulture = gcnew System::Globalization::CultureInfo(&quot;fr&quot;);\r\n\t\t\t\t\t\t\r\n\tGlobalObjects::rm1 = gcnew System::Resources::ResourceManager(L&quot;MyProjectsNamespace.AppLocalization&quot;, Assembly::GetExecutingAssembly());\r\n<\/code><\/pre>\n<p>\nThis will also cause region specific number formats to be used too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can create a separate resources file for each language&nbsp;in managed C++ which&nbsp;contain&nbsp;all the strings used by the application. In the solution explorer, right-click the Resource Files folder for&nbsp;the&nbsp;project&nbsp;&gt; Add &gt;&nbsp;New Item &gt; Visual C++ &gt; Resource &gt;&nbsp;Assembly Resource File (.resx). Give it the name AppLocalization.resx for example. Add your strings to the file. Important [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-localization-multi-language-support"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":14,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1502,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1315\/revisions\/1502"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}