常用 FBT 字符串
fbt
框架提供了一种在共享位置定义常见简单字符串的方法。 预期的格式是文本到描述的映射。
例如
// OurCommonStrings.js
module.exports = {
"Photo": "Still image ...",
"Video": "Moving pictures ...",
...
};
FBT 转换
它通过主 Babel 转换 babel-plugin-fbt
以插件选项的形式接受这些常用字符串。
Babel 设置示例
{
plugins: [
['babel-plugin-fbt', {
fbtCommonPath: "/path/to/OurCommonStrings.js",
// OR inlined...
fbtCommon: {
"Photo": "...",
...
},
...
}],
...
]
}
运行时 API
要在运行时使用字符串,可以使用 fbt.c(...)
函数调用或 <fbt common=true>...</fbt>
JSX API。
注意:如果转换遇到 没有 在提供的映射中的常用字符串,则会抛出错误。
例如。
<button>
{fbt.c('Photo')}
</button>
或
<button>
<fbt common>Photo</fbt>
</button>
以上两个示例的功能与工程师也包含文本的描述一样。
<fbt desc="Still image ...">Photo</fbt>
所有这些实例都会在收集时生成相同的识别哈希,因此会合并为相同的翻译。