Skip to content

Commit

Permalink
add interpolation data to response if returnDetails is true (#2053)
Browse files Browse the repository at this point in the history
* add interpolation data to response if returnDetails is true

* avoid directly modifying incoming options

* remove console.logs

* pass TOpt to TFunctionDetailedResult in t.v4.d.ts

* add comment about extending array of options properties
  • Loading branch information
jedlikowski committed Oct 18, 2023
1 parent f6aea8d commit c580d7a
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
53 changes: 52 additions & 1 deletion src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,21 @@ class Translator extends EventEmitter {
exactUsedKey: key,
usedLng: lng,
usedNS: namespace,
usedParams: this.getUsedParamsDetails(options),
};
}
return `${namespace}${nsSeparator}${key}`;
}

if (returnDetails) {
return { res: key, usedKey: key, exactUsedKey: key, usedLng: lng, usedNS: namespace };
return {
res: key,
usedKey: key,
exactUsedKey: key,
usedLng: lng,
usedNS: namespace,
usedParams: this.getUsedParamsDetails(options),
};
}
return key;
}
Expand Down Expand Up @@ -163,6 +171,7 @@ class Translator extends EventEmitter {
: `key '${key} (${this.language})' returned an object instead of string.`;
if (returnDetails) {
resolved.res = r;
resolved.usedParams = this.getUsedParamsDetails(options);
return resolved;
}
return r;
Expand Down Expand Up @@ -323,6 +332,7 @@ class Translator extends EventEmitter {
// return
if (returnDetails) {
resolved.res = res;
resolved.usedParams = this.getUsedParamsDetails(options);
return resolved;
}
return res;
Expand Down Expand Up @@ -544,6 +554,47 @@ class Translator extends EventEmitter {
return this.resourceStore.getResource(code, ns, key, options);
}

getUsedParamsDetails(options = {}) {
// we need to remember to extend this array whenever new option properties are added
const optionsKeys = [
'defaultValue',
'ordinal',
'context',
'replace',
'lng',
'lngs',
'fallbackLng',
'ns',
'keySeparator',
'nsSeparator',
'returnObjects',
'returnDetails',
'joinArrays',
'postProcess',
'interpolation',
];

const useOptionsReplaceForData = options.replace && typeof options.replace !== 'string';
let data = useOptionsReplaceForData ? options.replace : options;
if (useOptionsReplaceForData && typeof options.count !== 'undefined') {
data.count = options.count;
}

if (this.options.interpolation.defaultVariables) {
data = { ...this.options.interpolation.defaultVariables, ...data };
}

// avoid reporting options (execpt count) as usedParams
if (!useOptionsReplaceForData) {
data = { ...data };
for (const key of optionsKeys) {
delete data[key];
}
}

return data;
}

static hasDefaultValue(options) {
const prefix = 'defaultValue';

Expand Down
76 changes: 76 additions & 0 deletions test/translator/translator.translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -83,6 +84,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -93,6 +95,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -103,6 +106,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'de',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -113,6 +117,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'de',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -123,6 +128,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -133,6 +139,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -143,6 +150,7 @@ describe('Translator', () => {
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -153,6 +161,7 @@ describe('Translator', () => {
exactUsedKey: 'deep.test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
Expand All @@ -163,6 +172,73 @@ describe('Translator', () => {
exactUsedKey: 'deep.test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {},
},
},
{
args: ['translation:test', { returnDetails: true, testParam: 'test-param' }],
expected: {
usedKey: 'test',
res: 'test_en',
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {
testParam: 'test-param',
},
},
},
{
args: [
'translation:test',
{
returnDetails: true,
replace: { testParam: 'test-param' },
},
],
expected: {
usedKey: 'test',
res: 'test_en',
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {
testParam: 'test-param',
},
},
},
{
args: [
'translation:test',
{
defaultValue: 'default',
ordinal: true,
context: undefined,
replace: { testParam: 'test-param' },
lng: 'en',
lngs: ['en', 'de'],
fallbackLng: 'en',
ns: ['translation', 'other'],
keySeparator: '.',
nsSeparator: ':',
returnObjects: false,
returnDetails: true,
joinArrays: true,
postProcess: false,
interpolation: { escapeValue: false },
count: 1,
},
],
expected: {
usedKey: 'test',
res: 'test_en',
exactUsedKey: 'test',
usedLng: 'en',
usedNS: 'translation',
usedParams: {
testParam: 'test-param',
count: 1,
},
},
},
];
Expand Down
1 change: 1 addition & 0 deletions test/typescript/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function interpolation(t: TFunction) {
resolved.exactUsedKey;
resolved.usedNS;
resolved.usedLng;
resolved.usedParams;

const r2 = t('keyTwo', { returnDetails: false });
r2?.substring(0, 2); // make sure it is a string
Expand Down
8 changes: 6 additions & 2 deletions typescript/t.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export type TFunctionReturn<
: ParseTReturn<ActualKey, Resources[$FirstNamespace<ActualNS>]>
: DefaultTReturn<TOpt>;

export type TFunctionDetailedResult<T = string> = {
export type TFunctionDetailedResult<T = string, TOpt extends TOptions = {}> = {
/**
* The plain used key
*/
Expand All @@ -196,10 +196,14 @@ export type TFunctionDetailedResult<T = string> = {
* The used namespace for this translation.
*/
usedNS: string;
/**
* The parameters used for interpolation.
*/
usedParams: InterpolationMap<T> & { count?: TOpt['count'] };
};

type TFunctionReturnOptionalDetails<Ret, TOpt extends TOptions> = TOpt['returnDetails'] extends true
? TFunctionDetailedResult<Ret>
? TFunctionDetailedResult<Ret, TOpt>
: Ret;

type AppendKeyPrefix<Key, KPrefix> = KPrefix extends string
Expand Down
8 changes: 6 additions & 2 deletions typescript/t.v4.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export type TFunctionReturn<
: ParseTReturn<ActualKey, Resources[$FirstNamespace<ActualNS>]>
: DefaultTReturn<TOpt>;

export type TFunctionDetailedResult<T = string> = {
export type TFunctionDetailedResult<T = string, TOpt extends TOptions = {}> = {
/**
* The plain used key
*/
Expand All @@ -196,10 +196,14 @@ export type TFunctionDetailedResult<T = string> = {
* The used namespace for this translation.
*/
usedNS: string;
/**
* The parameters used for interpolation.
*/
usedParams: InterpolationMap<T> & { count?: TOpt['count'] };
};

type TFunctionReturnOptionalDetails<Ret, TOpt extends TOptions> = TOpt['returnDetails'] extends true
? TFunctionDetailedResult<Ret>
? TFunctionDetailedResult<Ret, TOpt>
: Ret;

type AppendKeyPrefix<Key, KPrefix> = KPrefix extends string
Expand Down

0 comments on commit c580d7a

Please sign in to comment.