在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):ttag-org/babel-plugin-ttag开源软件地址(OpenSource Url):https://github.com/ttag-org/babel-plugin-ttag开源编程语言(OpenSource Language):JavaScript 96.6%开源软件介绍(OpenSource Introduction):babel-plugin-ttag
project descriptionSolution for providing gettext like translations into your project. Uses es6 native template syntax. c-3po.js.orgdocumentation -Plugin functions:
Key features: The core features of this tool are:
TutorialsInstallation
gettext exampleHere is how you code will look like while using this plugin: import { t } from 'ttag';
const name = 'Mike';
console.log(t`Hello ${name}`); So you can see that you can use native es6 template formatting. To make your string translatable, all you need to do is to place 't' tag. Translator will see this inside .po files: #: src/page.js:8
msgid "Hello ${ name }"
msgstr "" Plural exampleHere is how you can handle plural forms:
import { ngettext, msgid } from 'ttag';
const name = 'Mike';
const n = 5;
console.log(ngettext(msgid`Mike has ${n} banana`, `Mike has ${n} bananas`, n)); Output in .po files: #: src/PluralDemo.js:18
msgid "Mike has ${ n } banana"
msgid_plural "Mike has ${ n } bananas"
msgstr[0] ""
msgstr[1] "" Use case with jsx (react):There are no additional setup for making this plugin work inside jsx. (just add babel-plugin-react plugin to your .babelrc) import React from 'react';
import { t, ngettext, msgid } from 'ttag';
class PluralDemo extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
this.countInc = this.countInc.bind(this);
}
countInc() {
this.setState({ count: this.state.count + 1 });
}
render() {
const n = this.state.count;
return (
<div>
<h3>{ t`Deadly boring counter demo (but with plurals)` }</h3>
<div>{ ngettext(msgid`You have clicked ${n} time`, `You have clicked ${n} times`, n) }</div>
<button onClick={this.countInc}>{ t`Click me` }</button>
</div>
)
}
}
export default PluralDemo; Disabling some code partsIf for some reason you need to disable ttag plugin transformation for some code block you can use special comment globally to disable the whole file or inside some code block (function): /* disable ttag */
// or
function test() {
/* disable ttag */
} ContributionFeel free to contribute, make sure to cover your contributions with tests. Test command:
License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论