gpt4 book ai didi

react-intl - babel-plugin-react-intl : Extract strings to a single file

转载 作者:行者123 更新时间:2023-12-04 03:44:57 30 4
gpt4 key购买 nike

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

现状:
ComponentA.json

[
{
"id": "addEmoticonA",
"description": "Add emoticon",
"defaultMessage": "Insert Emoticon"
},
{
"id": "addPhotoA",
"description": "Add photo",
"defaultMessage": "Insert photo"
}
]
ComponentB.json
[
{
"id": "addEmoticonB",
"description": "Add emoji",
"defaultMessage": "Insert Emoji"
},
{
"id": "addPhotoB",
"description": "Add picture",
"defaultMessage": "Insert picture"
}
]

我需要什么翻译。
final.json
{
"addEmoticonA": "Insert Emoticon",
"addPhotoA": "Insert photo",
"addEmoticonB": "Insert Emoji",
"addPhotoB": "Insert picture"
}

有没有办法完成这个任务?可能是通过使用 python 脚本或任何东西。即从不同的 json 文件制作单个 json 文件。或者直接使用 babel-plugin-react-intl 制作单个 json 文件

最佳答案

有一个translations manager 这将做到这一点。

或自定义选项见下文

以下脚本基于此 script浏览由创建的翻译消息babel-plugin-react-intl并以 json 格式创建包含来自所有组件的所有消息的 js 文件。

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))
})

关于react-intl - babel-plugin-react-intl : Extract strings to a single file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277206/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com