Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
- move keyPrefix to parsedOptions
- don't let user know he's about to break translations
  • Loading branch information
BorisTB committed Dec 18, 2023
1 parent 49d2cbe commit 59fd6fb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/extractors/getFixedTFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default function extractGetFixedTFunction(
...k,
parsedOptions: {
...k.parsedOptions,
keyPrefix: k.parsedOptions.keyPrefix || keyPrefix,
ns: k.parsedOptions.ns || ns,
},
})),
Expand All @@ -99,7 +100,6 @@ export default function extractGetFixedTFunction(

return keys.map((k) => ({
...k,
keyPrefix: keyPrefix || undefined,
sourceNodes: [path.node, ...k.sourceNodes],
extractorName: extractGetFixedTFunction.name,
}));
Expand Down
7 changes: 7 additions & 0 deletions src/extractors/tFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function parseTCallOptions(
contexts: false,
hasCount: false,
ns: null,
keyPrefix: null,
defaultValue: null,
};

Expand All @@ -76,6 +77,12 @@ function parseTCallOptions(
const defaultValueNodeValue = defaultValueNode.get('value');
res.defaultValue = evaluateIfConfident(defaultValueNodeValue);
}

const keyPrefixNode = findKeyInObjectExpression(path, 'keyPrefix');
if (keyPrefixNode !== null && keyPrefixNode.isObjectProperty()) {
const keyPrefixNodeValue = keyPrefixNode.get('value');
res.keyPrefix = evaluateIfConfident(keyPrefixNodeValue);
}
}

return res;
Expand Down
4 changes: 2 additions & 2 deletions src/extractors/useTranslationHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function extractUseTranslationHook(
const tBinding = id.scope.bindings['t'];
if (!tBinding) return [];

let keyPrefix: string | undefined;
let keyPrefix: string | null = null;

const optionsArgument = path.get('arguments')[1];
const options = getFirstOrNull(evaluateIfConfident(optionsArgument))
Expand All @@ -86,6 +86,7 @@ export default function extractUseTranslationHook(
...k,
parsedOptions: {
...k.parsedOptions,
keyPrefix: k.parsedOptions.keyPrefix || keyPrefix,
ns: k.parsedOptions.ns || ns,
},
})),
Expand All @@ -95,7 +96,6 @@ export default function extractUseTranslationHook(

return keys.map((k) => ({
...k,
keyPrefix: keyPrefix,
sourceNodes: [path.node, ...k.sourceNodes],
extractorName: extractUseTranslationHook.name,
}));
Expand Down
22 changes: 9 additions & 13 deletions src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface I18NextParsedOptions {
contexts: string[] | boolean;
hasCount: boolean;
ns: string | null;
keyPrefix: string | null;
defaultValue: string | null;
}

Expand All @@ -18,7 +19,6 @@ interface I18NextParsedOptions {
*/
export interface ExtractedKey {
key: string;
keyPrefix?: string;
parsedOptions: I18NextParsedOptions;

// Nodes (not node paths) from which the key was extracted.
Expand Down Expand Up @@ -50,30 +50,26 @@ export interface TranslationKey extends ExtractedKey {
function parseExtractedKey(key: ExtractedKey, config: Config): TranslationKey {
let cleanKey = key.key;

const keyPrefix = key.parsedOptions.keyPrefix;
if (keyPrefix) {
// Imitate behavior of i18next and just connect prefix with key before any other action
const keySeparator = config.keySeparator || '.';
cleanKey = `${keyPrefix}${keySeparator}${cleanKey}`;
}

let ns: string = key.parsedOptions.ns || config.defaultNS;
if (config.nsSeparator) {
const nsSeparatorPos = cleanKey.indexOf(config.nsSeparator);

if (nsSeparatorPos !== -1) {
if (key.keyPrefix) {
throw new Error(
`Do not use the keyPrefix option if you want to use keys with prefixed namespace notation.
key: ${cleanKey}
keyPrefix: ${key.keyPrefix}`,
)
}

ns = cleanKey.slice(0, nsSeparatorPos);
cleanKey = cleanKey.slice(nsSeparatorPos + 1);
}
}

let keyPath = Array<string>();
if (config.keySeparator) {
if (key.keyPrefix) {
keyPath = key.keyPrefix.split(config.keySeparator);
}

if (config.keySeparator) {
const fullPath = cleanKey.split(config.keySeparator);
keyPath = [...keyPath, ...fullPath.slice(0, fullPath.length - 1)];
cleanKey = fullPath[fullPath.length - 1];
Expand Down
5 changes: 5 additions & 0 deletions tests/__fixtures__/testUseTranslationHook/keyPrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export function MyComponent4() {
const [t] = useTranslation('ns3', { keyPrefix: 'deep5.deep6' });
return <p>{t('key7.key8')}{t('key9.key10')}</p>
}

export function MyComponent5() {
const [t] = useTranslation('ns4', { keyPrefix: 'deep7.deep8' });
return <p>{t('ns5:key11')}</p>
}
3 changes: 2 additions & 1 deletion tests/__fixtures__/testUseTranslationHook/keyPrefix.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[{ "deep0": {"key0": "", "key1": ""}}, {"ns": "ns0"}],
[{ "deep1": {"deep2": {"key2": "", "key3": ""}}}, {"ns": "ns1"}],
[{ "deep3": {"deep4": {"key4": { "key5": "", "key6": "" }}}}, {"ns": "ns2"}],
[{ "deep5": {"deep6": {"key7": { "key8": "" }, "key9": {"key10": ""}}}}, {"ns": "ns3"}]
[{ "deep5": {"deep6": {"key7": { "key8": "" }, "key9": {"key10": ""}}}}, {"ns": "ns3"}],
[{ "key11": ""}, {"ns": "deep7.deep8.ns5"}]
]
}

0 comments on commit 59fd6fb

Please sign in to comment.