{"id":389,"date":"2021-03-30T11:27:25","date_gmt":"2021-03-30T19:27:25","guid":{"rendered":"http:\/\/www.antunkarlovac.com\/blog\/?p=389"},"modified":"2023-10-23T15:58:02","modified_gmt":"2023-10-23T23:58:02","slug":"using-template-resource-files-for-labels-in-commerce-cloud","status":"publish","type":"post","link":"https:\/\/www.antunkarlovac.com\/blog\/2021\/03\/30\/using-template-resource-files-for-labels-in-commerce-cloud\/","title":{"rendered":"Using Template Resource Files For Labels in Commerce Cloud"},"content":{"rendered":"<p>It&#8217;s good practice to parameterize labels, rather than hard-coding them, so that they can localized easily. In SFCC, you define your labels in .properties files, and you can create separate files for each locale.<\/p>\n<p>In this post, assume that your cartridge is called int_mycartridge. You would create your .properties file(s) in int_mycartridge\/templates\/resources:<\/p>\n<pre>\/repo_root\n+--int_mycartridge\n   +--templates\n      +--my_template_files.isml\n      +--resources\n         +--my_resource_file.properties\n<\/pre>\n<p>You can have more than one .properties file. The <a href=\"https:\/\/documentation.b2c.commercecloud.salesforce.com\/DOC1\/topic\/com.demandware.dochelp\/content\/b2c_commerce\/topics\/localization\/b2c_localizing_template_resource_bundles.html?resultof=%22%72%65%73%6f%75%72%63%65%22%20%22%72%65%73%6f%75%72%63%22%20\">file naming convention for different locales<\/a> is as follows:<\/p>\n<ul>\n<li>my_resource_file.properties: Default locale file<\/li>\n<li>my_resource_file_es.properties: Localization file for all Spanish (ES) locales<\/li>\n<li>my_resource_file_es_ES.properties: Localization file for the es_ES (Spanish-Spain) locale<\/li>\n<\/ul>\n<p>The file will look something like this, with one entry per line:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfield.shipping.address.rut=Rol Unico Tributario\n<\/pre>\n<p>You can use your judgment as to the dot syntax &#8211; in the example above, which is for a checkout form, I followed the same pattern as other checkout fields.<\/p>\n<p>Then, in either your ISML templates, you can reference the labels as follows:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&amp;amp;lt;label&amp;amp;gt;\n${Resource.msg(&#039;field.shipping.address.rut&#039;, &#039;my_resource_file&#039;, &#039;Rol Unico Tributario&#039;)}:\n&amp;amp;lt;\/label&amp;amp;gt;\n<\/pre>\n<p>Note that uses the <a href=\"https:\/\/documentation.b2c.commercecloud.salesforce.com\/DOC1\/topic\/com.demandware.dochelp\/DWAPI\/scriptapi\/html\/api\/class_dw_web_Resource.html?resultof=%22%72%65%73%6f%75%72%63%65%22%20%22%72%65%73%6f%75%72%63%22%20\">Resource class<\/a>. The second argument is the name of your .properties file (without the extension), and the third is the default text to show if the label couldn&#8217;t be found.<\/p>\n<p><strong>Forms<\/strong><br \/>\nIf you&#8217;re using SFRA&#8217;s declarative XML to define a form, complete with validation, then you will want to reference error messages in the definition XML, e.g.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&amp;amp;lt;?xml version=&quot;1.0&quot;?&amp;amp;gt;\n&amp;amp;lt;form xmlns=&quot;http:\/\/www.demandware.com\/xml\/form\/2008-04-19&quot;&amp;amp;gt;\n&amp;amp;lt;field formid=&quot;regionCode&quot; type=&quot;string&quot; mandatory=&quot;true&quot;\nlabel=&quot;form.mycartridge.label.region&quot;\nmissing-error=&quot;form.mycartridge.region.missing&quot;\nvalue-error=&quot;form.mycartridge.region.invalid&quot;\nparse-error=&quot;error.message.required&quot;\nregexp=&quot;(^[a-zA-Z0-9]+$)&quot;&amp;amp;gt;\n&amp;amp;lt;\/field&amp;amp;gt;\n\n&amp;amp;lt;\/form&amp;amp;gt;\n<\/pre>\n<p>Note how this XML file references labels such as <strong>form.mycartridge.label.region<\/strong>. In this case, you simply need to define a resource file named <strong>forms.properties<\/strong> for the framework to locate your labels.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>It&#8217;s good practice to parameterize labels, rather than hard-coding them, so that they can localized easily. In SFCC, you define your labels in .properties files, and you can create separate files for each locale. In this post, assume that your cartridge is called int_mycartridge. You would create your .properties file(s) in int_mycartridge\/templates\/resources: \/repo_root +&#8211;int_mycartridge +&#8211;templates &hellip; <a href=\"https:\/\/www.antunkarlovac.com\/blog\/2021\/03\/30\/using-template-resource-files-for-labels-in-commerce-cloud\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Template Resource Files For Labels in Commerce Cloud<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/389"}],"collection":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":16,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":458,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions\/458"}],"wp:attachment":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}