Labeling and localizing drop-in components
In this topic, you will learn how to update drop-in component labels by using the resources available in the commerce boilerplate.
Key concepts
Placeholders files
Multiple JSON files to manage storefront labels, including localization. Each drop-in component has its own placeholders file (e.g., cart.json
, checkout.json
, pdp.json
). See the Placeholders files for more information.
Language objects
A JavaScript object (named langDefinitions
by convention) that contains key-value pairs for UI text labels in a specific language. The object is used to override the default dictionary with the placeholder file’s keys and values.
Labeling
To add or change UI text labels within Commerce drop-in components. Merchants use placeholer files to quickly change the default UI labels for drop-in components.
Localizing
To adapt UI text labels and formatting for a specific language. This process includes translating text labels and changing the text direction, date and time formats, and currency symbols to match the target language.
Big picture
Labeling drop-in components in the storefront involves two files:
- The placeholders files that provide the default drop-in component UI labels that merchants can quickly update as needed.
- The drop-in block (examples,
product-details.js
,cart.js
) where you add code to fetch, map, and override the drop-in component dictionary at runtime.
The following diagram shows the process for adding and overriding labels and text for drop-in components within the boilerplate template.
- Placeholder files. Merchants can change the storefront labels by changing the values in the placeholder JSON files, which are organized by drop-in components—
cart.json
,checkout.json
,pdp.json
, and so on. - Import function. You need to import the
fetchPlaceholders
function from the boilerplate’scommerce.js
file. - Fetch placeholders. Use the
fetchPlaceholders
function to retrieve theplaceholders
key-value pairs from the content folder. - Override default dictionary. Override the
default
property from thelangDefinitions
object with the keys and values from theplaceholder
object. - Initialize dictionary. Use the
register
function to update the dictionary at runtime.
Step-by-step
In the boilerplate code, the UI text labels in drop-in components come from the placeholder files. By using these files as the source for all storefront UI labels, merchants can easily change labels without involving developers.
There are two things to be aware of when using the fetchPlaceholders()
function:
During initialization:
You must provide the path to the drop-in’s placeholders file. This file will be fetched and merged into the existing placeholders object. Subsequent calls tofetchPlaceholders()
without a path will return the merged object containing all fetched labels.After initialization:
You can callfetchPlaceholders()
without a path to retrieve all initialized placeholders as a single object. This object can be accessed from a Block or anywhere else in the project.
Import fetchPlaceholders
function
In the drop-in block (for example, product-details.js
, cart.js
), import the fetchPlaceholders
function from the boilerplate’s commerce.js
file.
import { fetchPlaceholders } from '../../scripts/commerce.js';
Initialize placeholders with path
During initialization, you must use the fetchPlaceholders()
function using an argument to the path to your drop-in’s placeholders file. This fetches and merges the placeholders into the global object.
// Initialize placeholders for this drop-inconst placeholders = await fetchPlaceholders('placeholders/cart.json');
const langDefinitions = { default: { ...placeholders, },};
// Register Initializersinitializers.mountImmediately(initialize, { langDefinitions, //...});
Fetch placeholders after initialization
After initialization, you can use the fetchPlaceholders
function without a path to retrieve all merged placeholders. The following diagram and code snippet shows how to fetch the placeholders.
// Retrieve the placeholders language objectconst labels = await fetchPlaceholders();
export default async function decorate(block) { const $elem = document.createElement('div'); $elem.innerText = labels.Cart.PriceSummary.shipping.label;}
Test the changes
After you’ve updated the drop-in component dictionary with the new langDefinitions
object, test the changes in the storefront to ensure the new labels are displayed correctly. If the labels are not displaying as expected, review the mapping between the placeholder keys and the drop-in component dictionary keys. Make sure the keys match exactly. If the keys don’t match, the drop-in component will use the default dictionary values.