Skip to content

Commit

Permalink
1.1.0: Improved configuration handling and docs
Browse files Browse the repository at this point in the history
(also cleaned up how logging was handled)
  • Loading branch information
Download committed Nov 3, 2016
1 parent 0299a96 commit 14e1856
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 160 deletions.
35 changes: 9 additions & 26 deletions ComponentInterpolator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
var log;
try {
// use ulog when available
log = require('ulog')('preact-i18nline:ComponentInterpolator');
} catch (e) {

/* satisfy eslint */
}
var log = require('./util/createLogger')('preact-i18nline:ComponentInterpolator');

var invariant = require('invariant');
var Component = require('preact').Component;
Expand All @@ -17,9 +10,7 @@ var PLACEHOLDER_PATTERN = /(%\{.*?\})/;

// Replace a "$1" text descendant in this tree with the newDescendants
var injectNewDescendants = function(element, newDescendants, props, ensureInjected) {
if (log) {
log.debug(log.name + ': injectNewDescendants', element, newDescendants, props);
}
log.debug(log.name + ': injectNewDescendants', element, newDescendants, props);

newDescendants.injectedCount = newDescendants.injectedCount || 0;
props = props || {};
Expand All @@ -44,9 +35,7 @@ var injectNewDescendants = function(element, newDescendants, props, ensureInject
};

var getInjectIndex = function(children, containerName) {
if (log) {
log.debug(log.name + ': getInjectIndex', children, containerName);
}
log.debug(log.name + ': getInjectIndex', children, containerName);
var child, index = -1;
for (var i = 0; i < children.length; i++) {
child = children[i];
Expand All @@ -60,9 +49,7 @@ var getInjectIndex = function(children, containerName) {

class ComponentInterpolator extends Component {
inferChildren() {
if (log) {
log.debug(log.name + ': inferChildren');
}
log.debug(log.name + ': inferChildren');

var tokens = (this.props.string || '').split(WRAPPER_PATTERN);
this.keyCounter = 0;
Expand All @@ -77,9 +64,8 @@ class ComponentInterpolator extends Component {
}

interpolateAllComponents(tokens, eof) {
if (log) {
log.debug(log.name + ': interpolateAllComponents', tokens, eof);
}
log.debug(log.name + ': interpolateAllComponents', tokens, eof);

var token, child;
var children = [];
var wrappers = this.props.wrappers || {};
Expand Down Expand Up @@ -108,9 +94,8 @@ class ComponentInterpolator extends Component {
}

interpolatePlaceholders(string) {
if (log) {
log.debug(log.name + ': interpolatePlaceholders', string, this.props);
}
log.debug(log.name + ': interpolatePlaceholders', string, this.props);

var token, child;
var tokens = string.split(PLACEHOLDER_PATTERN);
var children = [];
Expand Down Expand Up @@ -139,6 +124,4 @@ class ComponentInterpolator extends Component {

module.exports = ComponentInterpolator;

if (log) {
log.log('Initialized ' + log.name);
}
log.log('Initialized ' + log.name);
61 changes: 40 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,22 @@ I18n.translations = {
}
```

## Configuration / Advanced Settings
## Configuration

If you have certain tags that you always want to translate (e.g. `<h1>`),
you can specify them in your `.i18nrc` via `autoTranslateTags`, e.g.
In addition to the [i18nline configuration](https://github.com/download/i18nline#configuration),
preact-i18nline adds some options specific to JSX processing:

```js
### autoTranslateTags
An array of strings, or a string with (a comma separated list of)
tag names that should be translated automatically. Defaults to `[]`.

#### example
*package.json*
```json
{
"autoTranslateTags": ["h1", "h2", "h3", "h4", "h5", "h6", "p"]
"i18n": {
"autoTranslateTags": ["h1", "h2", "h3", "h4", "h5", "h6", "p"]
}
}
```

Expand All @@ -273,18 +281,29 @@ class T extends Component {
}
```

Similarly, if you have certain tags you **don't** want to auto-translate
(e.g. `<code>`), you can specify those in a similar manner:
### neverTranslateTags
An array of strings, or a string with (a comma separated list of)
tag names that should **not** be translated automatically.
Defaults to `[]`.

```js
Similarly to `autoTranslateTags`, if you have certain tags you
**don't** want to translate automatically, (e.g. `<code>`),
you can specify those in a similar manner.

#### example
*package.json*
```json
{
"neverTranslateTags": ["code"],
"i18n": {
"neverTranslateTags": ["code"],
}
}
```

Then if those are ever nested in a larger translatable element, they
If those are ever nested in a larger translatable element, they
will be assumed to be untranslatable, and a placeholder will be created
for them.
for them.


## Gotchas

Expand All @@ -299,18 +318,17 @@ to use pure js for that, e.g.
</div>
```

i18n-js doesn't support gender-based localizations, but I do plan on
making i18nline work with other backends soon (e.g. i18next, FormatJS).
Watch this space, or better yet, create a pull request ;)

### Every JSX expression makes a placeholder

This kind of gets to a general rule of i18n: don't concatenate strings. For example,
This kind of gets to a general rule of i18n: don't concatenate strings.
For example,

```js
return (<b translate="yes">
You are {this.props.isAuthorized ? "authorized" : "NOT authorized"}
</b>);
return (
<b translate="yes">
You are {this.props.isAuthorized ? "authorized" : "NOT authorized"}
</b>
);
```

The extracted string will be `"You are %{opaque_placeholder}"` and the
Expand All @@ -324,8 +342,9 @@ return (this.props.isAuthorized ?
<b translate="yes">You are NOT authorized</b>);
```

**NOTE:** in a subsequent release of preact-i18nline, the former example
will cause an `i18nline:check` failure. You've been warned :)
**NOTE:** A subsequent release of preact-i18nline may add a check for
this situation that will cause an `i18nline:check` failure.
You've been warned :)

### Cloning this project under Windows

Expand Down
2 changes: 1 addition & 1 deletion __tests__/ComponentInterpolator.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var log = require('ulog')('preact-i18nline:test:ComponentInterpolator');
var log = require('../util/createLogger')('preact-i18nline:test:ComponentInterpolator');

var expect = require('chai').expect;
var subjector = require('../test_utils/subjector');
Expand Down
7 changes: 6 additions & 1 deletion __tests__/preprocess.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var log = require('ulog')('preact-i18nline:test:preprocess');
var log = require('../util/createLogger')('preact-i18nline:test:preprocess');

var expect = require('chai').expect;

Expand Down Expand Up @@ -90,6 +90,11 @@ describe('preprocess', function() {
.to.equal('<h1>{I18n.t("Hello World")}</h1>');
});

it('can handle missing autoTranslateTags', function() {
expect(subject('<h1>Hello World</h1>', {}))
.to.equal('<h1>Hello World</h1>');
});

it('doesn\'t translate autoTranslateTags with translate="no"', function() {
expect(subject('<h1 translate="no">Hello World</h1>', {autoTranslateTags: ['h1']}))
.to.equal('<h1>Hello World</h1>');
Expand Down
13 changes: 2 additions & 11 deletions browserify-transform.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
var log;
try {
// use ulog when available
log = require('ulog')('preact-i18nline:browserify-transform');
} catch (e) {

/* satisfy eslint */
}
var log = require('./util/createLogger')('preact-i18nline:browserify-transform');

var through = require("through2");
var I18nline = require("i18nline");
Expand All @@ -24,6 +17,4 @@ module.exports = function() {
});
};

if (log) {
log.log('Initialized ' + log.name);
}
log.log('Initialized ' + log.name);
35 changes: 9 additions & 26 deletions dist/ComponentInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var log;
try {
// use ulog when available
log = require('ulog')('preact-i18nline:ComponentInterpolator');
} catch (e) {

/* satisfy eslint */
}
var log = require('./util/createLogger')('preact-i18nline:ComponentInterpolator');

var invariant = require('invariant');
var Component = require('preact').Component;
Expand All @@ -27,9 +20,7 @@ var PLACEHOLDER_PATTERN = /(%\{.*?\})/;

// Replace a "$1" text descendant in this tree with the newDescendants
var injectNewDescendants = function injectNewDescendants(element, newDescendants, props, ensureInjected) {
if (log) {
log.debug(log.name + ': injectNewDescendants', element, newDescendants, props);
}
log.debug(log.name + ': injectNewDescendants', element, newDescendants, props);

newDescendants.injectedCount = newDescendants.injectedCount || 0;
props = props || {};
Expand All @@ -54,9 +45,7 @@ var injectNewDescendants = function injectNewDescendants(element, newDescendants
};

var getInjectIndex = function getInjectIndex(children, containerName) {
if (log) {
log.debug(log.name + ': getInjectIndex', children, containerName);
}
log.debug(log.name + ': getInjectIndex', children, containerName);
var child,
index = -1;
for (var i = 0; i < children.length; i++) {
Expand All @@ -81,9 +70,7 @@ var ComponentInterpolator = function (_Component) {
_createClass(ComponentInterpolator, [{
key: 'inferChildren',
value: function inferChildren() {
if (log) {
log.debug(log.name + ': inferChildren');
}
log.debug(log.name + ': inferChildren');

var tokens = (this.props.string || '').split(WRAPPER_PATTERN);
this.keyCounter = 0;
Expand All @@ -99,9 +86,8 @@ var ComponentInterpolator = function (_Component) {
}, {
key: 'interpolateAllComponents',
value: function interpolateAllComponents(tokens, eof) {
if (log) {
log.debug(log.name + ': interpolateAllComponents', tokens, eof);
}
log.debug(log.name + ': interpolateAllComponents', tokens, eof);

var token, child;
var children = [];
var wrappers = this.props.wrappers || {};
Expand All @@ -122,9 +108,8 @@ var ComponentInterpolator = function (_Component) {
}, {
key: 'interpolatePlaceholders',
value: function interpolatePlaceholders(string) {
if (log) {
log.debug(log.name + ': interpolatePlaceholders', string, this.props);
}
log.debug(log.name + ': interpolatePlaceholders', string, this.props);

var token, child;
var tokens = string.split(PLACEHOLDER_PATTERN);
var children = [];
Expand Down Expand Up @@ -154,6 +139,4 @@ var ComponentInterpolator = function (_Component) {

module.exports = ComponentInterpolator;

if (log) {
log.log('Initialized ' + log.name);
}
log.log('Initialized ' + log.name);
13 changes: 2 additions & 11 deletions hasTranslatableText.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
var log;
try {
// use ulog when available
log = require('ulog')('preact-i18nline:hasTranslatableText');
} catch (e) {

/* satisfy eslint */
}
var log = require('./util/createLogger')('preact-i18nline:hasTranslatableText');

var escapeRegExp = require("./util/escapeRegExp");

Expand All @@ -26,6 +19,4 @@ module.exports = function(config) {
};
};

if (log) {
log.log('Initialized ' + log.name);
}
log.log('Initialized ' + log.name);
13 changes: 2 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
var log;
try {
// use ulog when available
log = require('ulog')('preact-i18nline');
} catch (e) {

/* satisfy eslint */
}
var log = require('./util/createLogger')('preact-i18nline');

var recast = require("recast");

Expand Down Expand Up @@ -33,6 +26,4 @@ module.exports = function(i18nline) {
};
};

if (log) {
log.log('Initialized ' + log.name);
}
log.log('Initialized ' + log.name);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "preact-i18nline",
"version": "1.0.0",
"version": "1.1.0",
"description": "Keep your translations in line - with Preact!",
"main": "main.js",
"scripts": {
Expand Down
Loading

0 comments on commit 14e1856

Please sign in to comment.