Skip to content

Instantly share code, notes, and snippets.

@arcturus011
Created April 23, 2018 02:06
Show Gist options
  • Save arcturus011/930d6bb08fb83bc8e2a0898696df78e7 to your computer and use it in GitHub Desktop.
Save arcturus011/930d6bb08fb83bc8e2a0898696df78e7 to your computer and use it in GitHub Desktop.
narp comment
// import React from 'react'
// import {_, _c, _p, _cp} from 'i18n-utils'
var nodeGettext = require('node-gettext')
window.ifrxGT = window.ifrxGT || new nodeGettext()
function GetText(props) {
let {args, message, children, messagePlural, context} = props
let msg = message || children
let isPlural = props.messagePlural && args.count
let hasContext = props.context
if (hasContext) {
if (isPlural) {
msg = window.ifrxGT.npgettext(context, msg, messagePlural, args.count) // 上下文 && 单复数
} else {
msg = window.ifrxGT.pgettext(context, msg) // 上下文
}
} else if (isPlural) {
msg = window.ifrxGT.ngettext(msg, messagePlural, args.count) // 单复数
} else {
msg = window.ifrxGT.gettext(msg) // 普通文本
}
// 注入模板变量
msg = msg.replace(/\\{/g, '\u007B').replace(/\\}/g, '\u007D')
for (let key in args) {
let reg = new RegExp(`\\{\\s*${key}\\s*\\}`, 'g')
msg = msg.replace(reg, args[key])
}
return msg
}
class T extends React.Component {
render() {
return <GetText comment='这里是注释' context='这是上下文' message={'测试单数'} messagePlural={'测试复数'} args={{}} />
}
}
//
// _('最新版本为 {latestVersion}', {latestVersion: 'v2.3'})
// _c('所有弹窗右下角取消按钮的标题', '取消')
// _p('已欠费 1 天', '已欠费 {count} 天', {count: 233})
// _cp('数据表个数', '一个数据表', '{count} 个数据表', {count: 2})
c
@Jiab77
Copy link

Jiab77 commented Jun 3, 2018

nice gettext code for nodejs / reactjs, I might use it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment