首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >babel-plugin-react intl:将字符串提取到单个文件

babel-plugin-react intl:将字符串提取到单个文件
EN

Stack Overflow用户
提问于 2016-05-17 13:12:38
回答 2查看 5.5K关注 0票数 5

目前,在使用babel-plugin-react-intl时,每个组件都使用“id”、“description”和“defaultMessage”创建单独的json。我需要的是只创建一个json,它包含一个对象,所有'id‘作为'key’,'defaultMessage‘作为’值‘。

现状:

ComponentA.json

代码语言:javascript
复制
[
  {
    "id": "addEmoticonA",
    "description": "Add emoticon",
    "defaultMessage": "Insert Emoticon"
  },
  {
    "id": "addPhotoA",
    "description": "Add photo",
    "defaultMessage": "Insert photo"
  }
]

ComponentB.json

代码语言:javascript
复制
[
  {
    "id": "addEmoticonB",
    "description": "Add emoji",
    "defaultMessage": "Insert Emoji"
  },
  {
    "id": "addPhotoB",
    "description": "Add picture",
    "defaultMessage": "Insert picture"
  }
]

我需要翻译的东西。

final.json

代码语言:javascript
复制
{
  "addEmoticonA": "Insert Emoticon",
  "addPhotoA": "Insert photo",
  "addEmoticonB": "Insert Emoji",
  "addPhotoB": "Insert picture"
}

有什么办法完成这个任务吗?可能是通过使用python脚本之类的方式实现的。即从不同的json文件中生成一个json文件。或者直接使用babel-plugin-react intl创建一个json文件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-19 20:37:56

有一个翻译经理可以做到这一点。

或有关自定义选项,请参见下面

下面的脚本是基于这个脚本的,它遍历由babel-plugin-react-intl创建的翻译消息,并创建js文件,其中包含来自json格式的所有组件的所有消息。

代码语言:javascript
复制
import fs from 'fs'
import {
  sync as globSync
}
from 'glob'
import {
  sync as mkdirpSync
}
from 'mkdirp'
import * as i18n from '../lib/i18n'

const MESSAGES_PATTERN = './_translations/**/*.json'
const LANG_DIR         = './_translations/lang/'
  // Ensure output folder exists
mkdirpSync(LANG_DIR)

// Aggregates the default messages that were extracted from the example app's
// React components via the React Intl Babel plugin. An error will be thrown if
// there are messages in different components that use the same `id`. The result
// is a flat collection of `id: message` pairs for the app's default locale.
let defaultMessages = globSync(MESSAGES_PATTERN)
  .map(filename => fs.readFileSync(filename, 'utf8'))
  .map(file => JSON.parse(file))
  .reduce((collection, descriptors) => {
    descriptors.forEach(({
      id, defaultMessage, description
    }) => {
      if (collection.hasOwnProperty(id))
        throw new Error(`Duplicate message id: ${id}`)

      collection[id] = {
        defaultMessage, description
      }
    })
    return collection
  }, {})

// Sort keys by name
const messageKeys = Object.keys(defaultMessages)
messageKeys.sort()
defaultMessages = messageKeys.reduce((acc, key) => {
  acc[key] = defaultMessages[key]
  return acc
}, {})

// Build the JSON document for the available languages
i18n.en = messageKeys.reduce((acc, key) => {
  acc[key] = defaultMessages[key].defaultMessage
  return acc
}, {})
Object.keys(i18n).forEach(lang => {
  const langDoc = i18n[lang]
  const units = Object.keys(defaultMessages).map((id) => [id, defaultMessages[id]]).reduce((collection, [id]) => {
    collection[id] = langDoc[id] || '';
    return collection;
  }, {});
  fs.writeFileSync(`${LANG_DIR}${lang}.json`, JSON.stringify(units, null, 2))
})

票数 8
EN

Stack Overflow用户

发布于 2019-02-19 05:40:37

您可以使用巴贝尔-插件-反应式萃取器将翻译聚合到单个文件中。此外,它还提供了对消息的每次更改的自动重新编译翻译文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37277206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档