Add localizable strings to themes and theme components

A theme’s custom text can come from two places:

  • Locale files for text that ships with the theme, such as headings, button labels and messages.
  • Object settings for text that admins enter in the object editor, such as card titles or link labels. See translatable settings.

Both end up in the same theme translations. Admins translate and customize them in the admin UI, and your code reads them with themePrefix and i18n.

Locale files

Add translation files to the theme as locales/{locale}.yml, for example locales/en.yml or locales/fr.yml. Each file is valid YAML with a single top-level key equal to its locale:

en:
  theme_metadata:
    description: "This is a description for my theme"
    settings:
      theme_setting_name: "This is a description for the setting `theme_setting_name`"
      another_theme_setting_name:
        description: "This is a description for the setting `another_theme_setting_name`"
  banner:
    welcome: "Welcome to our community"
    subhead: "We're glad you're here!"
    button: "Start a new topic"
    dismiss: "Hide this banner"

The theme_metadata block holds the theme’s description and the descriptions of its settings. See theme metadata and theme settings. All other keys are yours to use in code.

Fallback works as in core. A locale file can be incomplete: any key it doesn’t define falls back to English.

Using translations in code

Translations are stored under a namespace for each theme. Site texts shows them as js.theme_translations.<theme_id>.<key>, for example js.theme_translations.34.banner.welcome.

Never hardcode the theme ID. Build the key with the themePrefix helper:

import { i18n } from "discourse-i18n";
import { themePrefix } from "virtual:theme";

// In JS code:
const result = i18n(themePrefix("banner.welcome"));

// In a template tag:
<template>{{i18n (themePrefix "banner.welcome")}}</template>

i18n() treats %{name} as an interpolation placeholder. If you don’t pass an argument for it, the output shows [missing %{name} value].

Managing translations in the admin UI

Admins can translate and customize a theme’s strings on the theme’s page or in Site texts. Both save the same overrides, so a change made in one shows up in the other. Each installation of a component has its own translations.

In Theme translations

Open the theme or component in Appearance → Themes & components. The Theme translations section lists the theme’s strings. Choose a language, edit a string, and select reset to go back to the shipped text.

When the Discourse AI plugin is enabled, Translate next to the language picker generates translations from the selected language into the site’s content localization languages.

In Site texts

In Appearance → Site texts, select the theme or component under Theme or component to show its strings. The filter can be linked to directly: /admin/customize/site_texts?theme_id=<theme_id>.

When you edit an entry, Site texts shows its default text, labelled with the language of that text, next to your translation. For locale files, the default text is the English text if English defines the key; otherwise the selected language’s shipped text.

Outdated translations

When the default text changes, translations based on the old text are marked Outdated, and Site texts shows the old and new default side by side. Only show outdated/invalid lists them. Translations saved before Discourse tracked default text show Unknown baseline instead; Acknowledge current default starts tracking them.

Interpolation placeholders

Site texts doesn’t check placeholders in locale-file translations, because your code can pass arguments that the shipped text doesn’t use.

Removed keys

Removing a key from a locale file keeps its translations, hidden from the admin UI, so they’re used again if the key comes back. Translations are only deleted by reset on the theme’s page, Revert to default in Site texts, or by deleting the theme or component.

Updating existing components

To turn a plain string setting into a locale-file translation, use helpers.migrateSettingToTranslation in a settings migration. See migrate settings.

Example

For a complete component that uses both locale files and translatable settings, see discourse-theme-component-i18n-test.


This document is version controlled - suggest changes on github.

32 Likes

Howto use theme translations in CSS ? I know we can use theme parameters but I need theme translations.

You can’t, they’re only available in templates and javascript. That’s the same as core/plugin translations.

Ideally, refactor things so that the string is set in a template. But if you really need a customisable string in a css file, you could use theme settings: Developing Discourse Themes & Theme Components

1 Like

And if I use theme setting, is it possible to translate it ?

No, not in to multiple languages, but it would allow admins to customize it for their site.

2 Likes

Hello everyone.
I’m redefining the template upload-selector.hbs
I want to add a translatable phrase.
I created a new variable upload_selector.upload_images
For example

Please tell me what to do next?

Hello,

when i try this technic in the discourse theme creator (see here) everything works fine and as explained here in the post.

However if I import the very same theme on my own server only " [en.theme_translations.12.blog]" appears. Also in the theme settings page there is no “Theme Translations” section like on the theme creator.

I really don’t know where to search for this error anymore. Has anybody a hint for me?

[Edit]
I’m using discourse 2.6.7 ( [f73cdbbd2f ] ) in an docker environment.

can you update discourse ?

you are using an old version

Yeah i’m trying that, that is another thing i need help with but, I don’t want to post it here, that’s another topic.

Anyway I thought that shouldn’t be problem as the translation feature was included in version 2.2.0.beta9, see commit.

Hello, sorry in advance for a newbs question here.
I created a header with just html and css

        <div class="top-navbar">
         <span class="j_menu_item" ><a href="https://www.example.com/download">Download</a></span>     
        </div>

And then I want to translate the word “Download”
I created the english translation file

en: 
  top-navbar: 
    download: "Yeah"

Then I changed the html code as for the facebook example


    <script type="text/x-handlebars" data-template-name="/connectors/discovery-below/sidebar">
        <div class="top-navbar">
         <span class="j_menu_item" ><a href="https://www.example.com/download">{{i18n (theme-prefix "top-navbar.download")}}</a></span>     
        </div>
    </script>

This translates and prints “Yeah” but breaks my layout, I guess it is because I’m using the “/connectors/discovery-below/sidebar”. I just want to apply my translation without messing with any template but I don’t understand how to just apply the translation inline.

Could you please provide a simple example on how to just use a translation in the custom html of a theme ?

Thanks !

Hello,

The problem is, the data-template-name should be unique name. Developing Discourse Themes & Theme Components. Like this: data-template-name="/connectors/PLUGIN-OUTLET-NAME/UNIQUE-NAME" Now you use the sidebar name in your example which is I think override Fakebook sidebar template.

For example this should work :slightly_smiling_face:

<script type="text/x-handlebars" data-template-name="/connectors/discovery-below/downloadlink">
  <div class="top-navbar">
    <span class="j_menu_item" ><a href="https://www.example.com/download">{{i18n (theme-prefix "top-navbar.download")}}</a></span>     
  </div>
</script>
2 Likes

Thanks @Don

I forgot to mention that plugin is not installed and that I tried already to change the data-template-name to something a random UNIQUE-NAME with the same result or not showing my banner at all if I made up the PLUGIN-OUTLET-NAME.

You can probably tell by now , I’m not familiar at all with handlebars/ember :slight_smile:

My understanding is that I’m customizing a template that has it’s predefined place in the html and has a result the custom html is not anymore over /html/body/section main but deep inside resulting in css inheritance I didn’t have before.

What I struggle to understand is Why do I need to customize any template to use a translation…
I managed to identify the template to customize using Ember inspector as advised here

And thanks to your answer and link about the plugin-outlet I found the right data-template-name=“/connectors/above-site-header/my-navbar”

Thanks again for your help

1 Like

Oh I see… I thought you using the Fakebook theme and want to place your code below the sidebar section. :slightly_smiling_face:

A good way to see the plugin outlets visually you can use the Plugin Outlet Locations theme component.

2 Likes

Hi @Don ,

The reason I used that facebook example is because of:

Thanks again !

What about singular and plural, how to translate text using {{theme-i18n}} if it has one and many translations. For instance “Result” and “Results”

2 Likes

There are some examples of how we do this in Discourse’s source, typically we have two strings and switch based on an integer:

Screenshot 2022-12-15 at 5.42.58 PM

This should work in a theme too, generally the JS would look something like:

I18n.t(themePrefix("confirm_remove_tags"), {
  count: exampleCountValue,
});
1 Like

and what about hbs, can it be done in hbs templates?

1 Like

Yes it can:

{{theme-i18n "confirm_remove_tags" count=this.exampleCountValue}}
4 Likes

when I do that in a .gjs Component template within a Theme I get:

Error: Attempted to resolve a helper in a strict mode template, but that value was not in scope: theme-prefix

So I attempt to import it:

import themePrefix from "discourse/helpers/theme-prefix";

But it grumbles:

Identifier 'themePrefix' has already been declared

(I looked for an example in on the 'hub but there doesn’t seem to be one)

1 Like

Update: you need to use {{i18n (themePrefix "

4 Likes