Using Template Resource Files For Labels in Commerce Cloud

It’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
+--int_mycartridge
   +--templates
      +--my_template_files.isml
      +--resources
         +--my_resource_file.properties

You can have more than one .properties file. The file naming convention for different locales is as follows:

  • my_resource_file.properties: Default locale file
  • my_resource_file_es.properties: Localization file for all Spanish (ES) locales
  • my_resource_file_es_ES.properties: Localization file for the es_ES (Spanish-Spain) locale

The file will look something like this, with one entry per line:

field.shipping.address.rut=Rol Unico Tributario

You can use your judgment as to the dot syntax – in the example above, which is for a checkout form, I followed the same pattern as other checkout fields.

Then, in either your ISML templates, you can reference the labels as follows:

<label>
${Resource.msg('field.shipping.address.rut', 'my_resource_file', 'Rol Unico Tributario')}:
</label>

Note that uses the Resource class. 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’t be found.

Forms
If you’re using SFRA’s declarative XML to define a form, complete with validation, then you will want to reference error messages in the definition XML, e.g.

<?xml version="1.0"?>
<form xmlns="http://www.demandware.com/xml/form/2008-04-19">
<field formid="regionCode" type="string" mandatory="true"
label="form.mycartridge.label.region"
missing-error="form.mycartridge.region.missing"
value-error="form.mycartridge.region.invalid"
parse-error="error.message.required"
regexp="(^[a-zA-Z0-9]+$)">
</field>

</form>

Note how this XML file references labels such as form.mycartridge.label.region. In this case, you simply need to define a resource file named forms.properties for the framework to locate your labels.

Leave a Reply

Your email address will not be published. Required fields are marked *