Skip to content

Commit

Permalink
makeComponents takes jed instance as a single argument
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Jan 22, 2024
1 parent 439829d commit a55aa27
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
46 changes: 16 additions & 30 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,11 @@ export class Plural extends React.Component {
}
}

const getGettextFuncs = args => {
let gettext, ngettext, pgettext, npgettext;
if (args.length === 1) {
const [obj] = args;
gettext = obj.gettext.bind(obj);
ngettext = obj.ngettext.bind(obj);
pgettext = obj.pgettext ? obj.pgettext.bind(obj) : undefined;
npgettext = obj.npgettext ? obj.npgettext.bind(obj) : undefined;
} else if (args.length === 2) {
[gettext, ngettext] = args;
} else if (args.length === 4) {
[gettext, ngettext, pgettext, npgettext] = args;
} else {
throw new Error(
'Expected object containing gettext/ngettext(/pgettext/npgattext) or 2/4 args with the funcs'
);
}
const getGettextFuncs = jedInstance => {
const gettext = jedInstance.gettext.bind(jedInstance);
const ngettext = jedInstance.ngettext.bind(jedInstance);
let pgettext = jedInstance.pgettext ? jedInstance.pgettext.bind(jedInstance) : undefined;
let npgettext = jedInstance.npgettext ? jedInstance.npgettext.bind(jedInstance) : undefined;

if (!pgettext) {
pgettext = (ctx, ...params) => gettext(...params);
Expand Down Expand Up @@ -202,27 +190,25 @@ const getContextParams = args => {
}
};

const getNPlurals = (...args) => {
// Only true in tests
if (args.length !== 1) {
const getNPlurals = jedInstance => {
const {domain, locale_data: localeData} = jedInstance.options || {};
const pluralForms = localeData?.[domain]?.['']?.plural_forms;

if (!pluralForms) {
return null;
}

const jed = args[0];
const {domain, locale_data: localeData} = jed.options;
const pluralForms = localeData[domain][''].plural_forms;

if (!pluralForms) {
const match = pluralForms.match(/^nplurals=(\d+)/);
if (!match) {
return null;
}

const [, plurals] = pluralForms.match(/^nplurals=(\d+)/);
return Number(plurals);
return Number(match[1]);
};

export const makeComponents = (...args) => {
const {gettext, ngettext, pgettext, npgettext} = getGettextFuncs(args);
const nPlurals = getNPlurals(...args);
export const makeComponents = jedInstance => {
const {gettext, ngettext, pgettext, npgettext} = getGettextFuncs(jedInstance);
const nPlurals = getNPlurals(jedInstance);

class Translate extends React.Component {
static propTypes = {
Expand Down
2 changes: 1 addition & 1 deletion test-data/example.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const pgettext = (ctx, msg) =>
const npgettext = (ctx, msg, msgpl, n) =>
TRANSLATE ? jedTranslated.npgettext(ctx, msg, msgpl, n) : jedEmpty.npgettext(ctx, msg, msgpl, n);

const {Translate, PluralTranslate} = makeComponents(gettext, ngettext, pgettext, npgettext);
const {Translate, PluralTranslate} = makeComponents({gettext, ngettext, pgettext, npgettext});

export const testStrings = translate => {
TRANSLATE = translate;
Expand Down
18 changes: 9 additions & 9 deletions tests/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const gettext = expression => jsonifyMessage(`translated-${expression}`);
const ngettext = (sExpression, pExpression, n) =>
jsonifyMessage(n !== 1 ? `plural-${pExpression}` : `singular-${sExpression}`);

const {Translate, PluralTranslate} = makeComponents(gettext, ngettext);
const {Translate, PluralTranslate} = makeComponents({gettext, ngettext});

// This prevents printing warnings to the console about missing error boundaries
beforeEach(() => {
Expand Down Expand Up @@ -122,10 +122,10 @@ test('Invalid translate children fail', () => {

test('Missing params in string translations fail (without translations)', () => {
// eslint-disable-next-line no-shadow
const {Translate, PluralTranslate} = makeComponents(
msg => msg,
(msg, msgpl, n) => (n === 1 ? msg : msgpl)
);
const {Translate, PluralTranslate} = makeComponents({
gettext: msg => msg,
ngettext: (msg, msgpl, n) => (n === 1 ? msg : msgpl),
});

expect(() => Translate.string('{foo}')).toThrow("Placeholder '{foo}' got no value");
expect(() => PluralTranslate.string('{foo}', '{bar}', 1)).toThrow(
Expand Down Expand Up @@ -236,10 +236,10 @@ test('Prop updates are working with translation', () => {

test('Prop updates are working without translation', () => {
// eslint-disable-next-line no-shadow
const {Translate, PluralTranslate} = makeComponents(
msg => msg,
(msg, msgpl, n) => (n === 1 ? msg : msgpl)
);
const {Translate, PluralTranslate} = makeComponents({
gettext: msg => msg,
ngettext: (msg, msgpl, n) => (n === 1 ? msg : msgpl),
});

class MyComponent extends React.Component {
constructor() {
Expand Down

0 comments on commit a55aa27

Please sign in to comment.