如何使用 i18n 本地化 Stimulus JS 值?

发布于 2025-01-12 23:52:51 字数 558 浏览 3 评论 0原文

我正在开发 Rails 6 应用程序,我试图将所有显示的字符串存储在语言环境文件中。我有一个名为 countdown_controller.js 的 Stimulus 控制器:

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {

  static values = {
    message: { type: String, default: "Deal closed!" }
  }
}

en.yml:

en:
 deals:
   status:
     closed: "Deal closed!"

我想使用 I18n.t("deals.status.close") 作为 countdown_controller.jsmessageValue 的值,而不是直接写入字符串。我不知道该怎么做。

I am working on rails 6 application where I'm trying to store all displayed strings in locales files. I have a Stimulus controller named countdown_controller.js:

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {

  static values = {
    message: { type: String, default: "Deal closed!" }
  }
}

en.yml:

en:
 deals:
   status:
     closed: "Deal closed!"

I want to use I18n.t("deals.status.closed") as a value for messageValue in countdown_controller.js instead of directly writing the string. I am not sure how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浴红衣 2025-01-19 23:52:52

我还发现很难使用 Stimulus 控制器实现本地化,直到我找到一个线程 - 对我的情况非常有帮助

# your config/locales/en.yml:

 en:
  deals:
   status:
    closed: "Deal closed!"


# app/views/layouts/application.html.erb (or any layout you're using)
...
<%= javascript_tag do %>
  window.I18n = <%= t("deals").to_json.html_safe %>
<% end %>
...


# in your Stimulus controller, you can simply call the translation for ex:


import { Controller } from "stimulus"

export default class extends Controller {    
  connect() {
    console.log(I18n.status.closed)
  }
}

I also find it hard to implement localization with Stimulus controller until I found a thread - very helpful in my case

# your config/locales/en.yml:

 en:
  deals:
   status:
    closed: "Deal closed!"


# app/views/layouts/application.html.erb (or any layout you're using)
...
<%= javascript_tag do %>
  window.I18n = <%= t("deals").to_json.html_safe %>
<% end %>
...


# in your Stimulus controller, you can simply call the translation for ex:


import { Controller } from "stimulus"

export default class extends Controller {    
  connect() {
    console.log(I18n.status.closed)
  }
}
眼眸 2025-01-19 23:52:51

只需将 countdown_controller.js 转换为 countdown_controller.js.erb 对我来说很有效。然后我使用插值来注入 I18n.t("deals.status.close") 如下:

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {

  static values = {
    message: { type: String, default: "<%= I18n.t('deals.status.closed') %>" }
  }
}

PS 当我将注释更改为使用以 # 而不是以 // 开头的 JS 语法。

Simply converting countdown_controller.js to countdown_controller.js.erb worked for me. Then I used interpolation to inject the I18n.t("deals.status.closed") as follows:

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {

  static values = {
    message: { type: String, default: "<%= I18n.t('deals.status.closed') %>" }
  }
}

P.S. I was getting an error initially as I changed the comment to use ruby syntax which begins with # instead of JS syntax which begins with //.

古镇旧梦 2025-01-19 23:52:51

我所做的是将翻译作为 Stimulus 控制器元素数据集的一部分发送,因此可以在 connect 上使用。就我而言,有多个,因此我从视图或助手发送一个哈希,如下所示:

localizations = {
  processing_images: :suggestions_processing_images.t,
  processing_image: :suggestions_processing_image.t,
  processing_results: :suggestions_processing_results.t,
  error: :suggestions_error.t
}.to_json

button_tag(:suggest_names.l,
           type: :button, 
           data: { localization: localizations,
                   controller: "suggestions",
                   action: "suggestions#suggestNames" })

// javascript/controllers/suggestions_controller.js

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="suggestions"
export default class extends Controller {
  initialize() {
    this.localized_text = {}
  }
  connect() {
    Object.assign(this.localized_text,
      JSON.parse(this.element.dataset.localization));
  }
}

What I do is to send the translations as part of the Stimulus controller element dataset, so it's available on connect. In my case there is more than one, so I send a hash from the view or the helper, like this:

localizations = {
  processing_images: :suggestions_processing_images.t,
  processing_image: :suggestions_processing_image.t,
  processing_results: :suggestions_processing_results.t,
  error: :suggestions_error.t
}.to_json

button_tag(:suggest_names.l,
           type: :button, 
           data: { localization: localizations,
                   controller: "suggestions",
                   action: "suggestions#suggestNames" })

// javascript/controllers/suggestions_controller.js

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="suggestions"
export default class extends Controller {
  initialize() {
    this.localized_text = {}
  }
  connect() {
    Object.assign(this.localized_text,
      JSON.parse(this.element.dataset.localization));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文