Using singular and plural forms in theme locale files

Reading time: 2 minutes

That themes can be translated into any language is probably a well-known fact. What's less well-known is that you can set up your translations so that singular and plural word forms are automatically used appropriately.

For example, say you have a block on your product detail page where it says how many items are left in stock. For multiple items, you'd want the text to read something like "7 items left in stock", but for the last item you'd want it to read "1 item left in stock".

Now, you could just use two different locale strings and use some Liquid code to decide which one to use depending on the items left in stock. But there's a more elegant way of achieving this.

For any given locale string, you can set up multiple translations, with each translation being applied depending on a count parameter you pass when rendering the translation.

Sticking with our example from above, this is what the translation string would look like for item:

To render this string in the theme you would use this code:

This code assumes the number of items left in stock is stored in the Liquid variable items_in_stock. If that variable contains the value 1, the string for "one" will be rendered, in any other case the "other" string will be rendered.

This lets you use a single Liquid statement to render a string depending on the variable you're passing to the translate filter.

Also note, that you can use this not just for differentiating between singular and plural word forms. You could use a completely different text for when only one items is left in stock. So if you wanted to convey a sense of urgency, you could use a string like "Only 1 item left in stock, hurry before it's gone!" or something along those lines.

There are also more cases supported than just "one" or "other". Check out Shopify's help page on pluralizing translations for details.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.