我正在尝试使用 Javascript 从我在 AEM 翻译器 (/libs/cq/i18n/translator.html) 中创建的字典中动态检索翻译的消息。
我们的字典值设置如下:
String EN FR
========================================================
TEST-001 This is a Test1 FR:This is a Test1
TEST-002 This is a Test2 FR:This is a Test2
TEST-003 This is a Test3 FR:This is a Test3
我已经浏览了国际化 UI 字符串文章 ( https://docs.adobe.com/docs/en/aem/6-2/develop/components/i18n/i18n-dev.html ),但是当我尝试执行以下操作时:
Granite.I18n.setLocale("en");
Granite.I18n.get("TEST-001");
或
Granite.I18n.setLocale("fr");
Granite.I18n.get("TEST-001");
我只返回我传入的字符串(即“TEST-001”)。
有人可以帮助我了解如何取回翻译后的值吗?
谢谢!
最佳答案
取决于您的翻译存储位置,您可以尝试类似 -
<script>
Granite.I18n.init({
locale: "<%= LocaleUtil.toRFC4646(request.getLocale()).toLowerCase() %>",
urlPrefix: "<%= request.getContextPath() %>/libs/cq/i18n/dict."
});
</script>
这是从这里获取的示例 -
/libs/cq/gui/components/projects/admin/page/head.jsp
另一种方式是
<html
lang="<%= LocaleUtil.toRFC4646(request.getLocale()).toLowerCase() %>"
data-i18n-dictionary-src="<%= request.getContextPath() %>/libs/cq/i18n/dict.{+locale}.json">
取自 - /libs/fd/fm/gui/components/admin/creationwizard/page/page.jsp
关于javascript - AEM 6.x : How to access i18n translations via Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344768/