Properties files and localized strings with the Resource class
Key Takeaway
Store storefront text in properties files and read it with Resource.msg(key, bundleName, defaultMessage). A call with only a key, or with a key and a default message, reads only the default bundle message (message.properties).
Properties files, called template resource bundles, hold the text a storefront shows as key=value lines. Each language or locale gets its own translated copy. Templates, form definitions and controllers refer to that text by key. The dw.web.Resource class then returns the version that matches the current request's locale. One set of templates can therefore serve every language. Translators work on simple text files instead of template code.
Two ways to localize templates
B2C Commerce can localize templates in two ways. You can keep one template set and move its text into properties files. You can also create a separate template set for each locale.
Salesforce recommends the single template set as a best practice. SFRA uses the single template set too. With this approach, you translate the template resource bundles, not the templates.
A single template set works like this:
- Display logic stays in one place.
- Templates don't change when you add a language.
- The strings sit in a simple file format that you can send to a translation provider.
- You add a translation back by copying the translated file into the cartridge, without risk of breaking template logic.