Skip to content

Commit

Permalink
added reportNS function to I18NextProvider to report used namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierpascal committed Aug 23, 2018
1 parent 2d2ec21 commit d398635
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
21 changes: 16 additions & 5 deletions react-i18next.js
Expand Up @@ -622,6 +622,11 @@ function translate(namespaceArg) {
var i18nOptions = _this.i18n && _this.i18n.options && _this.i18n.options.react || {};
_this.options = _extends({}, getDefaults(), i18nOptions, options);

if (context.reportNS) {
var namespaces = Array.isArray(namespaceArg) ? namespaceArg : [namespaceArg];
namespaces.forEach(context.reportNS);
}

_this.getWrappedInstance = _this.getWrappedInstance.bind(_this);
return _this;
}
Expand Down Expand Up @@ -675,7 +680,8 @@ function translate(namespaceArg) {

Translate.contextTypes = {
i18n: PropTypes.object,
defaultNS: PropTypes.string
defaultNS: PropTypes.string,
reportNS: PropTypes.func
};

Translate.displayName = 'Translate(' + getDisplayName(WrappedComponent) + ')';
Expand Down Expand Up @@ -1173,6 +1179,7 @@ var I18nextProvider = function (_Component) {
if (props.initialLanguage) {
_this.i18n.changeLanguage(props.initialLanguage);
}
_this.reportNS = props.reportNS;
return _this;
}

Expand All @@ -1181,7 +1188,8 @@ var I18nextProvider = function (_Component) {
value: function getChildContext() {
return {
i18n: this.i18n,
defaultNS: this.defaultNS
defaultNS: this.defaultNS,
reportNS: this.reportNS
};
}
}, {
Expand All @@ -1205,16 +1213,19 @@ var I18nextProvider = function (_Component) {
I18nextProvider.propTypes = {
i18n: PropTypes.object.isRequired,
children: PropTypes.element.isRequired,
defaultNS: PropTypes.string
defaultNS: PropTypes.string,
reportNS: PropTypes.func
};

I18nextProvider.childContextTypes = {
i18n: PropTypes.object.isRequired,
defaultNS: PropTypes.string
defaultNS: PropTypes.string,
reportNS: PropTypes.func
};

I18nextProvider.defaultProps = {
defaultNS: undefined
defaultNS: undefined,
reportNS: undefined
};

var objectEntries = Object.entries || function (obj) {
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/I18nextProvider.js
Expand Up @@ -13,12 +13,14 @@ class I18nextProvider extends Component {
if (props.initialLanguage) {
this.i18n.changeLanguage(props.initialLanguage);
}
this.reportNS = props.reportNS;
}

getChildContext() {
return {
i18n: this.i18n,
defaultNS: this.defaultNS
defaultNS: this.defaultNS,
reportNS: this.reportNS
};
}

Expand All @@ -37,16 +39,19 @@ class I18nextProvider extends Component {
I18nextProvider.propTypes = {
i18n: PropTypes.object.isRequired,
children: PropTypes.element.isRequired,
defaultNS: PropTypes.string
defaultNS: PropTypes.string,
reportNS: PropTypes.func
};

I18nextProvider.childContextTypes = {
i18n: PropTypes.object.isRequired,
defaultNS: PropTypes.string
defaultNS: PropTypes.string,
reportNS: PropTypes.func
};

I18nextProvider.defaultProps = {
defaultNS: undefined
defaultNS: undefined,
reportNS: undefined
};

export default I18nextProvider;
8 changes: 7 additions & 1 deletion src/translate.js
Expand Up @@ -28,6 +28,11 @@ export default function translate(namespaceArg, options = {}) {
const i18nOptions = (this.i18n && this.i18n.options && this.i18n.options.react) || {};
this.options = { ...getDefaults(), ...i18nOptions, ...options };

if (context.reportNS) {
const namespaces = Array.isArray(namespaceArg) ? namespaceArg : [namespaceArg];
namespaces.forEach(context.reportNS);
}

this.getWrappedInstance = this.getWrappedInstance.bind(this);
}

Expand Down Expand Up @@ -80,7 +85,8 @@ export default function translate(namespaceArg, options = {}) {

Translate.contextTypes = {
i18n: PropTypes.object,
defaultNS: PropTypes.string
defaultNS: PropTypes.string,
reportNS: PropTypes.func
};

Translate.displayName = `Translate(${getDisplayName(WrappedComponent)})`;
Expand Down
15 changes: 15 additions & 0 deletions test/i18nextProvider.spec.js
Expand Up @@ -50,6 +50,21 @@ describe('I18nextProvider', () => {
expect(I18nextProvider.childContextTypes.defaultNS)
.toBe(PropTypes.string);
});
it('should provide reportNS', () => {
const i18n = {
options: {},
services: {
resourceStore: {
data: {}
}
},
changeLanguage: () => {}
};
const wrapper = new I18nextProvider({ i18n, defaultNS: '', initialI18nStore: {}, initialLanguage: 'en', reportNS: (ns) => ns });
expect(wrapper.getChildContext().reportNS('_ns_')).toBe('_ns_');
expect(I18nextProvider.childContextTypes.reportNS)
.toBe(PropTypes.func);
});
it('should have i18n proptype required', () => {
expect(I18nextProvider.propTypes.i18n)
.toBe(PropTypes.object.isRequired);
Expand Down
11 changes: 11 additions & 0 deletions test/translate.spec.js
@@ -1,6 +1,7 @@
jest.unmock('../src/translate');
import React from 'react';
import PropTypes from 'prop-types';
import { shallow } from 'enzyme';
import translate from '../src/translate';

describe('translate', () => {
Expand Down Expand Up @@ -121,4 +122,14 @@ describe('translate', () => {
expect(instance.namespaces.length).toBe(1);
expect(instance.namespaces[0]).toBe('i18nDefaultNS');
});
it('should report namespaces', () => {
const namespaces = [];
const C = translate(['ns1', 'ns2'])(<div>text</div>);
shallow(<C />, {
context: {
reportNS: ns => namespaces.push(ns)
}
});
expect(namespaces).toEqual(['ns1', 'ns2']);
});
});

0 comments on commit d398635

Please sign in to comment.