How to modify the translation filter to a specific language

I have a muliti-language theme. But in a specific section, I only want two languages. If user select french , the section’s language is french, if user select other languages, the section’s language is english.

The github copilot tell’s me use this code :

{% if request.query.locale == 'fr' %}
  {%assign locale = 'fr' %}
{% else %}
  {%assign locale = 'en' %}
{% endif %} 
<H2>{{ 'general.accessibility.skip_to_content' | t: locale }} </H2>

It can run ,but it doesn’t work, how should I do?

Hi @qinhuanle0805 ,

Ensure your locale files are set up correctly:

locales/en.default.json:

{
  "general": {
    "accessibility": {
      "skip_to_content": "Skip to content"
    }
  }
}

locales/fr.default.json:

{
  "general": {
    "accessibility": {
      "skip_to_content": "Aller au contenu"
    }
  }
}

And Here’s an updated code , please try it.

{% if request.query.locale == 'fr' %}
  {% assign locale = 'fr' %}
{% else %}
  {% assign locale = 'en' %}
{% endif %}

## 
  {% case locale %}
    {% when 'fr' %}
      {{ 'general.accessibility.skip_to_content' | t }}
    {% else %}
      {{ 'general.accessibility.skip_to_content' | t: locale }}
  {% endcase %}

If our suggestion is useful, please don’t forget like and mark as a solution . Thank you very much !

Thanks for reply,but the code still not works well .

I checked the locale files , they are ok.

I tested this code for force change all languages to english, it’s also failed.

## {{ 'general.accessibility.skip_to_content' | t: 'en' }}

Does the translation filter has this usage? It is not mentioned in the document Storefront locale files (shopify.dev) , or something else affected it ?

Looking forward to your reply.

Hi @qinhuanle0805 ,

Can you show me the configuration of the locales/en.default.json and locales/fr.default.json files? I am using the Dawn theme and my configuration for general and accessibility will be on the same level.