问题标签 [messageformat.js]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
1262 浏览

javascript - 使用 angular-translate 更改语言会导致消息格式和绑定出错

我正在尝试使用消息格式学习角度翻译。当我单击按钮更改语言时,我收到一个错误“未定义不是函数” - 但它不是 100% 可靠的。它很容易发生,但我不确定哪个更改会触发错误。

转载者:在 Chrome 41 Firefox 开发者版 39 和 IE11 中也进行了测试。不工作。

以下是我详细复制它的方法: 在新加载时:将变量设置为另一个数字(例如 3),同时仍然是英语。改成挪威语。将数字更改为其他内容。改回英文。当我现在尝试更改号码时,它不再起作用我在 plunker 中重新创建了我的代码:http ://plnkr.co/edit/K4xHNf93Vhkrfc5LmliV?p=preview

代码如下:

HTML:

Javascript:

问题截图

0 投票
1 回答
176 浏览

angularjs - 带有复数和 HTML 的 ngTranslate

我有一个带有 ngTranslate 复数形式的模板:

但我想把数字放在span标签之间:

这样,如果我使用translate属性指令:

HTML 被加密,我可以在最终结果中看到 HTML 标记。所以我使用ng-bind-html指令:

结果相同。HTML 显示给用户。

有没有办法解密这个 HTML 标签?

编辑:

该问题并非特定于 ngMessageFormat 使用。如果我有类似的翻译"<b>{{count}}</b> accepted",则标签会被转义。我尝试添加translate-compile指令,但似乎没有任何效果。

0 投票
1 回答
858 浏览

localization - 将 {select} 格式 (format.js) 与聚合物 app-localize-behavior 一起使用

我正在使用 Polymers app-localize-behavior 进行一些试验,我很难找到一些关于如何使用 format.js 的 {select} 格式的清晰文档

app-localization-behavior文档说:
“Polymer.AppLocalizeBehavior 完全支持与 format.js 相同的消息语法;”
但是例如在聚合物文档中,他们将参数作为字符串传递:

在 format.js 文档中不是:

现在我面临的问题是如何使用 {select} 格式。

format.js文档说要像这样使用它:

所以我在我的模板中这样做了:

locales.json :

语言默认设置为 french "fr",所以我希望得到“nous”作为输出,而不是完整的{{localize(etc..)}}显示在屏幕上。

我最后一次尝试将所有内容作为字符串或只是一些参数(所有组合)传递,但这一切都无济于事。

有没有人遇到过同样的问题,或者有人可以解释一下我在这里做错了什么吗?

帮助将不胜感激。

0 投票
2 回答
241 浏览

angularjs - 如何使用来自 npm 的消息格式

我从 npm 安装了messageformat以在我的 angular-boostrap 项目中与 angular-translate 一起使用。

如果我只是通过 CDN 添加 messageformat v0.3.1,一切都会按预期工作。但我想使用从 npm 安装的 messageformat。问题是,我不知道怎么做。messageformat npm 包中的脚本看起来都不像 CDN 上的脚本。我想知道我是否需要构建它或其他什么?但是话又说回来,npm for messageformat 的文档除了“npm install ...”之外没有提及任何内容。

我错过了什么?

[编辑]
项目中有 bin 和 lib 文件夹。两者都包含 messageformat.js,但它们都不起作用,而且它们看起来与 CDN 中的完全不同。如果我包含 node_modules/messageformat/lib/messageformat.js 我会收到“模块未定义”错误。如果我包含 node_modules/messageformat/bin/messageformat.js 我得到“无效或意外的令牌”......因此我怀疑缺少一个步骤来获取类似于 CDN 的脚本

0 投票
1 回答
329 浏览

webpack - 在 Webpack 中加载多个资源文件以在 messageformat.js 中使用

我有一个名为的webpack 加载器messageformat-loader,它接收 JSON,通过messageformat.js传递它,并输出格式函数的结果映射。它适用于单个文件。

./simple-messages.json

./example.js

跑步

但是, messageformat.js 的CLI接受一组文件,然后在将文件传递给编译器之前,它使用文件名作为其键将其聚合到一个 JSON 对象中。

webpack由于不支持 glob,我将如何使用它来完成此操作?

./multi-example.js

跑步


我的尝试:

  • 应用 globwebpack.config.js本身(如此推荐)

    • 如何汇总结果并将它们传递给messageformat-loader? 如果我不这样做,我将为messageformat.js每个 JSON 文件包含一次运行时。
      • 看起来这实际上可能是更好的方法,但我必须修改 messageformat.js 的输出以将运行时与字符串分开。
  • 使用require.context这里推荐)

    • 看上面
  • multi-json-loader

    • 不幸的是,这很尴尬,因为 webpack 需要一个资源文件,即使它没有被使用

      /li>
    • 我可以将 glob 移动到资源文件中,例如glob-loader,但是从源文件中提取加载程序的参数对我来说似乎更尴尬。

      • 这也是fontgen-loader所做的,所以这看起来是一种常见的方法。
  • 我应该做一个messageformat-plugin而不是装载机吗?然后我就可以完全控制这些块了。不过,这感觉像是错误的抽象。

0 投票
1 回答
4375 浏览

internationalization - react-intl 或 intl-messageformat 中的小数复数形式(javascript i18n 包)

我正在使用react-intl包进行国际化。反过来,该软件包依赖于intl-messageformat. 我一直在搜索这两个包中的文档,但仍然对是否可以将分数变量的消息国际化感到困惑。例如,如此所述:

一些语言使用“1. 2茶匙”但“2 . 1茶匙”</p>

react-intl使用/时有没有办法计算分数intl-messageformat?到目前为止,我只在文档中看到了带有整数的示例。

0 投票
0 回答
105 浏览

intl - react-intl 复合消息

我正在努力找出构建消息的最佳方法,其中一个动态值也需要翻译。

例如,如果我有消息“输入西班牙语...”,我想在我的应用程序支持的所有语言的每个不同组合中使用此消息。

假设只支持 en/es 那么我需要一个可以输出以下组合的函数:

使用 3 种支持的语言,将有 9 种可能的组合等...

我能想到的唯一方法是按如下方式编写消息:

我有一种感觉,这不是最佳方式。或许在某些语言中,“English”这个词的独立形式在“Type in English...”这个短语的上下文中使用时是不一样的。

我想这是一个假设的问题,我对语言的了解还不够,无法知道这是否是一个问题。

上述编写消息的方法是否有效?

0 投票
1 回答
1200 浏览

ionic-framework - Getting ngx-translate-messageformat-compiler to work with ionic

I'm trying to get an Ionic app to work with ICU messageformat translations. I'm not actually a "web" guy, I'm just trying to get a test frontend working as a proof-of-concept for my C# backend that delivers those ICU strings, so bear with me if I missed something obvious.

To start off I chose the template that already does "normal" translations: super.

I then followed the steps here to get ngx-translate-messageformat-compiler to work:

Made sure that @ngx-translate/core is already installed:

Installed ngx-translate-messageformat-compiler and messageformat

Opened this folder in my favorite editor.

Added the example to the json files ./src/assets/i18n/de.json (this is my local language used). Added the example to the json files ./src/assets/i18n/en.json (this is the fallback language... just in case...).

Added the suggested testing view template to ./src/pages/welcome/welcome.html

Starting it with ionic:serve works as one would imagine: the app starts, I click "Skip" to go to the welcome page and my text appears... translated, but not yet "messageformat"ed.

enter image description here

Now onto adding the actual messageformat:

In ./src/app/app.module.ts, I added the imports for TranslateCompiler and TranslateMessageFormatCompiler and added the configuration block for the compiler, so the whole configuration for the TranslateModule now reads:

Upon starting the app, I immediately (before even clicking "Skip" to go to the welcome page) get the following error:

I checked that make-plural is installed:

And now I'm kinda out of my depth. I did not get any errors or warnings during the whole process. What is wrong, how can I fix it? I'm not opposed to people telling me I do it wrong and I have to use a different package. I just need a way to translate and messageformat.


I removed the plural messageformat from the example texts and it works for non-plural texts:

enter image description here

But I still would like to have plural support... I mean it's their own example texts, that should work, right?

0 投票
1 回答
109 浏览

angular - 获取JS日期格式字符串

对于我们的 Angular 4 应用程序,我使用带有 messageformat.js 的 ngx-translate 来处理国际化和本地化。是否可以获取日期对象的短日期格式字符串,即。'YYYY-MM-dd'?

即:new Date().formatString('en-US')返回:'YYYY-MM-dd'

0 投票
1 回答
765 浏览

icu - ICU 表达式 *select* 未检测到空值

我正在使用 i18n angular 7 翻译我的应用程序,但我遇到了 ICU 表达式select的问题。问题是我希望条件能够识别并清空这样的值:animal: string

我试过使用

但它没有用

当动物是:

(空字符串、null 或未定义) -> 翻译 Cat

-> 翻译狗

其他-> 翻译猪