How to show site_texts strings to themes and theme components?

סיכום מבוסס בינה מלאכותית

The discussion revolves around displaying localized strings in Discourse themes and components. Bcat seeks help on correctly using the default js.share.facebook string in their theme, referencing a guide on localizable strings. Johani advises importing I18n with const I18n = require("I18n").default; to access translation keys like I18n.t("share.facebook"). wyudong notes that the I18n module is no longer global in recent Discourse versions, requiring explicit import. Bcat confirms that using I18n.t("share.facebook") works without the import, possibly due to version differences. Johani clarifies that the bulk update of files was done via a script, not manually, addressing Bcat’s concern about the effort involved.

I have created Tocic Share but want to use the default js.share.facebook example, but according to this article, I wrote it wrong so it doesn’t show.
Can show me how to display properly instead of adding new words to the topic

I try: I18n.t(themePrefix("my_translation_key"))

Add this at the top of your component

const I18n = require("I18n").default;

You can then use something like this when you need to use the default Discourse text

I18n.t("TRANSLATION_KEY")

For example this

h('span', 'Chia sẻ qua Facebook')

becomes

h("span", I18n.t("share.facebook"))

thank you very much. :smile:

Without this line, the i18n works fine. Otherwise, error occurs: “Could not find module I18n”.

That depends on your Discourse version. I18n used to be globally defined so you didn’t need to import it on older Discourse versions.

If your site is on a recent commit like the OP, then you need to import / require it.

https://github.com/discourse/discourse/commit/eab560fe2aa9053f6d227d29ec6c1ad0939ea940

Is it automatic or did you manually change 352 changed files? :roll_eyes:

@wyudong I used h("span", I18n.t("share.facebook")) No need to add const I18n = require ("I18n"). default; `

I’m not sure I understand. Can you please clarify what you mean here?

I am wondering you have to change each file then commit. Or have it automatically (change multiple files at once)
This question is not important. If with personal knowledge maybe I will change each file and it takes all day :frowning:
GIT, I don’t have much experience.

Thanks for clarifying :+1:

Yeah updating 300+ files manually would take a lot of time. The files were updated with a script that Mark created.