Shopify / theme-tools

Everything developer experience for Shopify themes
https://shopify.dev/docs/themes
MIT License
73 stars 21 forks source link

Feature Request: Improved translation pluralisation support #538

Open david-w-shopify opened 4 days ago

david-w-shopify commented 4 days ago

Is your feature request related to a problem? Please describe. The existing translation completions support pluralised translations, which is excellent, but there are a couple of edge cases that haven't yet been addressed.

  1. The count variable is not always rendered inside a translation, but still needed for the pluralisation logic to work. If a pluralised variable is injected, count should be included with the variables regardless of if it is used inside the rendered string.
  2. It’s possible that pluralised translations use different variables, so all should be accounted for. Currently it looks like only those in the first rule are used for autocompletions.

Describe the solution you'd like

Use case 1:

{
  "product": {
    "add_product_to_cart": {
      "one": "Add product to cart",
      "other": "Add products to cart"
    }
  }
}

In this example the completion should be:

{{ 'product.add_product_to_cart' | t: count: count }}

Despite count not being used in the actual rendered string, it is required for the pluralisation rules to work.

Use case 2:

{
  "product": {
    "add_count_product_to_cart": {
      "one": "Add {{ product }} to cart, {{ test }}",
      "other": "Add {{ product }}s to cart, {{ test_2 }}"
    }
  }
}

In this example the completion should be:

{{ 'product.add_product_to_cart' | t: count: count, product: product, test: test, test_2: test_2 }}

Variables discovered in all options should be completed to ensure the string can always render correctly.

charlespwd commented 3 days ago

The count variable is not always rendered inside a translation, but still needed for the pluralisation logic to work. If a pluralised variable is injected, count should be included with the variables regardless of if it is used inside the rendered string.

I didn't even know that. 🤦

david-w-shopify commented 2 days ago

You made me second guess, so I had to double check on pluralisation this morning. If you don't pass a count variable the object is rendered rather than any of the individual values.

Image

Image

Image

Fwiw I think it is a lesser-known feature, but here are the docs: https://shopify.dev/docs/storefronts/themes/architecture/locales/storefront-locale-files#pluralize-translations