简体   繁体   English

如何使用 FormatJS CLI 保留翻译顺序?

[英]How to retain order of translations with FormatJS CLI?

When the messages are extracted they are ordered in the extraction file alphabetically according to their randomly generated ids.提取消息时,它们在提取文件中根据其随机生成的 id 按字母顺序排序。 Is there any way to prevent that and have them ordered in the exact same order in which they appear in the code instead?有什么办法可以防止这种情况发生,并让它们以与它们在代码中出现的顺序完全相同的顺序排列?

Eg now:例如现在:

"AYGNrK": translation2,
"BIOgfm": translation1,
"Cqv/CV": translation3,

Wanted:通缉:

"BIOgfm": translation1,
"AYGNrK": translation2,
"Cqv/CV": translation3,

If you want to have full control over the exported message, you can use your own formating function.如果您想完全控制导出的消息,您可以使用自己的格式 function。

formatjs extract "src/**/*.{ts,tsx,vue}" --format [path]

In this function, you can sort it ie by value.在这个 function 中,您可以按值对其进行排序。 See this example for the general structure of a custom formatter:有关自定义格式化程序的一般结构,请参见此示例:

https://github.com/formatjs/formatjs/blob/main/packages/cli/src/formatters/default.ts https://github.com/formatjs/formatjs/blob/main/packages/cli/src/formatters/default.ts

An altnerative way is to set the custom id with a prefix instead of generating ids.另一种方法是使用前缀设置自定义 ID,而不是生成 ID。 But you need to be aware of naming collisions.但是您需要注意命名冲突。

<FormattedMessage
  id="component.title"
  value="My title"
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM