Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ React i18n
import React from 'react'

import {
init,
createI18n
debug,
tct,
t,
} from 'r-i18n'
```

Expand All @@ -23,15 +21,15 @@ import {
Use [Jed](http://slexaxton.github.io/Jed) to initialize i18n in your project.

```jsx
init({ /* jed options */ }))
const i18n = createI18n({ /* jed options */ }))
```

#### t

Component as placeholder

```jsx
import {t} from 'r-i18n'
const {t} = i18n

t('Welcome to Strikingly')
// -> '欢迎使用 Strikingly'
Expand All @@ -53,7 +51,7 @@ t('%{author} assigned this event to %{assignee}', {
HTML inside translated string with a root wrapper

```jsx
import {tct} from 'r-i18n'
const {tct} = i18n

//...

Expand All @@ -71,13 +69,13 @@ Wrap `t` and `tct` with a wrapper `<span class="translation-wrapper"/>`
(for React Native, it just appends a flag emoji to the message)

```jsx
import {tct, debug} from 'r-i18n'
import {createI18n, debug} from 'r-i18n'

//...

const i18n = createI18n({ /* jed options */ }))
debug()

tct('Welcome. Click [link:here]', {
i18n.tct('Welcome. Click [link:here]', {
root: <p/>,
link: <a href="#" />
})
Expand Down
18 changes: 9 additions & 9 deletions lib/i18n.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 49 additions & 42 deletions src/i18n.es6
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ const DOMAIN = "i18n";

let LOCALE_DEBUG = false;

let i18n = null;

export function setLocale(options) {
if (options.noPo) {
i18n = false;
} else {
let jedInstance = options;
i18n = createJed(jedInstance);
}
}

export function setDebug() {
LOCALE_DEBUG = true;
}
Expand All @@ -40,14 +29,6 @@ function createJed(jedOptions) {
return new Jed(jedOptions);
}

function getTranslate(stringKey) {
if (i18n) {
return i18n.gettext(stringKey);
}

return stringKey;
}

function formatForReact(formatString, args) {
let rv = [];
let cursor = 0;
Expand Down Expand Up @@ -225,33 +206,59 @@ function format(formatString, args) {
}
}

export function gettext(string, ...args) {
let rv = getTranslate(string);
if (args.length > 0) {
rv = format(rv, args);

export class I18n {
constructor(options = {}) {
this.init(options)

this.t = this.gettext
this.tn = this.ngettext
this.tct = this.gettextComponentTemplate
}
return mark(rv);
}

export function ngettext(singular, plural, ...args) {
return mark(format(i18n.ngettext(singular, plural, args[0] || 0), args));
}
init(options = {}) {
if (options.noPo) {
this._i18n = false;
} else {
let jedInstance = options;
this._i18n = createJed(jedInstance);
}
}

/* special form of gettext where you can render nested react
components in template strings. Example:
gettext(string, ...args) {
let rv = this._getTranslate(string);
if (args.length > 0) {
rv = format(rv, args);
}
return mark(rv);
}

gettextComponentTemplate('Welcome. Click [link:here]', {
root: <p/>,
link: <a href="#" />
});
ngettext(singular, plural, ...args) {
return mark(format(this._i18n.ngettext(singular, plural, args[0] || 0), args));
}

/* special form of gettext where you can render nested react
components in template strings. Example:

the root string is always called "root", the rest is prefixed
with the name in the brackets */
export function gettextComponentTemplate(template, components) {
let tmpl = parseComponentTemplate(getTranslate(template));
return mark(renderComponentTemplate(tmpl, components));
gettextComponentTemplate('Welcome. Click [link:here]', {
root: <p/>,
link: <a href="#" />
});

the root string is always called "root", the rest is prefixed
with the name in the brackets */
gettextComponentTemplate(template, components) {
let tmpl = parseComponentTemplate(this._getTranslate(template));
return mark(renderComponentTemplate(tmpl, components));
}


_getTranslate(stringKey) {
if (this._i18n) {
return this._i18n.gettext(stringKey);
}

return stringKey;
}
}

export const t = gettext;
export const tn = ngettext;
export const tct = gettextComponentTemplate;
8 changes: 5 additions & 3 deletions src/index.es6
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict'

import {setLocale, setDebug, tct, tn, t} from './i18n'
import {setDebug, I18n} from './i18n'

const createI18n = m => new I18n(m)
const init = createI18n

const init = m => setLocale(m)
const debug = setDebug

export {init, debug, tct, tn, t}
export {init, createI18n, debug }
6 changes: 4 additions & 2 deletions test/test.es6
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Jed from 'jed'
import ReactDOMServer from 'react-dom/server'
import assert from 'assert'

let i18n = require('../')
const { createI18n, debug } = require('../')

let i18n = createI18n()

describe('i18n', () => {
it('should return i18n strings or react components', () => {
Expand Down Expand Up @@ -79,7 +81,7 @@ describe('i18n', () => {
})

it('should return debugging wrapper', () => {
i18n.debug()
debug()
assert(ReactDOMServer.renderToStaticMarkup(i18n.tct('lorem [li] ipsum', {
root: <div/>,
li: <b>hey</b>
Expand Down