إضافة سلاسل نصية قابلة للترجمة إلى الثيمات ومكونات الثيم

For those looking to add custom languages and translations to a Discourse theme or theme component, they can now include localised strings, which are made available for use in UI components. Translations are stored in the same format as core/plugin translations, and can be used in almost the same way.

Themes can supply translation files in a format like /locales/{locale}.yml. These files should be valid YAML, with a single top level key equal to the locale being defined. These can be defined using the discourse_theme CLI, importing a .tar.gz, installing from a GIT repository, or via the editor on theme-creator.discourse.org.

An example locale file might look like

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`"
  sidebar:
    welcome: "Welcome"
    back: "back,"
    welcome_subhead: "We're glad you're here!"
    likes_header: "Share the Love"
    badges_header: "Your Top Badges"
    full_profile: "View your full profile"

Administrators can override individual keys on a per-theme basis in the /admin/customize/themes user interface. Fallback is handled in the same way as core, so it is ok to have incomplete translations for non-english languages will make use of the english keys.

In the background, these translations are stored alongside the core translations, under a theme-specific namespace. For example:

theme_translation.{theme_id}.sidebar.welcome

You should never hardcode the theme_id in your theme code, so there are a few ways to help you access the translations.

In .hbs files, you can use the dedicated helper

{{theme-i18n "my_translation_key"}}

Or, if you need to pass the translation key into another component, you can use the theme-prefix helper:

<DButton @label={{theme-prefix "my_translation_key"}} />

In Javascript, or in .gjs files, you can use the themePrefix function. This is automatically injected, and does not need to be imported:

const result = I18n.t(themePrefix("my_translation_key"));

<template>{{i18n (themePrefix "blah")}}</template>

For a complete example of using translations in a theme, check out @awesomerobot’s Fakebook theme: GitHub - discourse/Fakebook


This document is version controlled - suggest changes on github.

31 إعجابًا

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: Developer’s guide to Discourse Themes

إعجاب واحد (1)

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)

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.

مرحباً، أعتذر مقدماً عن سؤال المبتدئين هذا.
لقد أنشأت ترويسة باستخدام HTML و CSS فقط

        <div class="top-navbar">
         <span class="j_menu_item" ><a>Download</a></span>     
        </div>

ثم أريد ترجمة كلمة “Download”
لقد أنشأت ملف الترجمة الإنجليزية

en: 
  top-navbar: 
    download: "Yeah"

ثم قمت بتغيير كود HTML كما في مثال فيسبوك


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

هذا يترجم ويطبع “Yeah” ولكنه يكسر تخطيطي، أعتقد أن السبب هو أنني أستخدم “/connectors/discovery-below/sidebar”. أريد فقط تطبيق ترجمتي دون العبث بأي قالب ولكنني لا أفهم كيف أطبق الترجمة مباشرة.

هل يمكنك تقديم مثال بسيط حول كيفية استخدام الترجمة في HTML المخصص لموضوع ما؟

شكرا!

مرحباً،
المشكلة هي أن data-template-name يجب أن يكون اسمًا فريدًا. Developing Discourse Themes & Theme Components. مثل هذا: data-template-name=\"/connectors/PLUGIN-OUTLET-NAME/UNIQUE-NAME\" الآن أنت تستخدم اسم sidebar في مثالك والذي أعتقد أنه يتجاوز قالب الشريط الجانبي لـ Fakebook.

على سبيل المثال، يجب أن يعمل هذا :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)

شكراً @Don

نسيت أن أذكر أن المكون الإضافي غير مثبت وأنني حاولت بالفعل تغيير data-template-name إلى اسم فريد عشوائي بنفس النتيجة أو عدم ظهور اللافتة على الإطلاق إذا قمت بإنشاء PLUGIN-OUTLET-NAME.
ربما يمكنك أن تخبرني الآن، أنا لست على دراية على الإطلاق بـ handlebars/ember :slight_smile:
فهمي هو أنني أقوم بتخصيص قالب له مكانه المحدد مسبقًا في html وله نتيجة، لم يعد html المخصص فوق /html/body/section main ولكن في العمق مما يؤدي إلى وراثة CSS لم تكن لدي من قبل.
ما أواجه صعوبة في فهمه هو لماذا أحتاج إلى تخصيص أي قالب لاستخدام ترجمة…
تمكنت من تحديد القالب لتخصيصه باستخدام Ember inspector كما هو موصى به هنا
وبفضل إجابتك ورابطك حول plugin-outlet وجدت data-template-name الصحيح = “/connectors/above-site-header/my-navbar”
شكراً مرة أخرى على مساعدتك.

إعجاب واحد (1)

أوه، فهمت… اعتقدت أنك تستخدم سمة Fakebook وتريد وضع الكود الخاص بك أسفل قسم الشريط الجانبي. :slightly_smiling_face:

طريقة جيدة لرؤية منافذ المكونات إضافية بصريًا يمكنك استخدام مكون السمة Plugin Outlet Locations.

إعجابَين (2)

مرحباً @Don،

السبب في استخدامي لمثال فيسبوك هذا هو:

شكراً مرة أخرى!

ماذا عن المفرد والجمع، كيف تترجم النص باستخدام {{theme-i18n}} إذا كان لديه ترجمة واحدة وكثيرة؟ على سبيل المثال “Result” و “Results”

إعجابَين (2)

هناك بعض الأمثلة على كيفية قيامنا بذلك في مصدر Discourse، وعادةً ما يكون لدينا سلسلتان ونقوم بالتبديل بناءً على عدد صحيح:

Screenshot 2022-12-15 at 5.42.58 PM

يجب أن يعمل هذا في سمة أيضًا، وبشكل عام ستبدو جافا سكريبت شيئًا كهذا:

I18n.t(themePrefix("confirm_remove_tags"), {
  count: exampleCountValue,
});
إعجاب واحد (1)

وماذا عن hbs، هل يمكن القيام بذلك في قوالب hbs؟

إعجاب واحد (1)

نعم يمكن ذلك:

{{theme-i18n "confirm_remove_tags" count=this.exampleCountValue}}
4 إعجابات

عندما أفعل ذلك في قالب مكون .gjs داخل سمة، أحصل على:
Error: Attempted to resolve a helper in a strict mode template, but that value was not in scope: theme-prefix

لذا أحاول استيراده:
import themePrefix from "discourse/helpers/theme-prefix";

لكنه يتذمر:
Identifier 'themePrefix' has already been declared

(لقد بحثت عن مثال في "hub ولكن لا يبدو أن هناك واحدًا)

إعجاب واحد (1)

تحديث: تحتاج إلى استخدام {{i18n (themePrefix "

4 إعجابات