From a961d294621f3d647d421ca62e4e412ee6addde0 Mon Sep 17 00:00:00 2001 From: Vladimir Semenov Date: Sun, 29 May 2022 15:45:47 +0300 Subject: [PATCH] add keyPath support for react-i18n-next --- package.json | 5 +++++ src/core/Config.ts | 5 +++++ src/core/KeyDetector.ts | 28 +++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1eed9e06..073b7733 100644 --- a/package.json +++ b/package.json @@ -776,6 +776,11 @@ "default": true, "description": "%config.annotation_in_place%" }, + "i18n-ally.enableKeyPrefix": { + "type": "boolean", + "default": false, + "description": "Enable keyPrefix parsing for react-i18n-next" + }, "i18n-ally.annotationMaxLength": { "type": "number", "default": 40, diff --git a/src/core/Config.ts b/src/core/Config.ts index 06c8d7e6..11721a4b 100644 --- a/src/core/Config.ts +++ b/src/core/Config.ts @@ -28,6 +28,7 @@ export class Config { 'ignoreFiles', 'parserOptions', 'parsers.extendFileExtensions', + 'enableKeyPrefix', ] static readonly refreshConfigs = [ @@ -559,4 +560,8 @@ export class Config { static get telemetry(): boolean { return workspace.getConfiguration().get('telemetry.enableTelemetry') as boolean } + + static get enableKeyPrefix(): boolean { + return !!this.getConfig('enableKeyPrefix') + } } diff --git a/src/core/KeyDetector.ts b/src/core/KeyDetector.ts index eea634e5..8212384e 100644 --- a/src/core/KeyDetector.ts +++ b/src/core/KeyDetector.ts @@ -21,8 +21,10 @@ export class KeyDetector { for (const reg of regs) { (text.match(reg) || []) - .forEach(key => - keys.add(key.replace(reg, '$1')), + .forEach((rawKey) => { + const key = Config.enableKeyPrefix ? detectedKey(rawKey.replace(reg, '$1'), text) : rawKey + keys.add(key) + }, ) } @@ -37,7 +39,8 @@ export class KeyDetector { for (const regex of regs) { const range = document.getWordRangeAtPosition(position, regex) if (range) { - const key = document.getText(range).replace(regex, '$1') + const rawKey = document.getText(range).replace(regex, '$1') + const key = Config.enableKeyPrefix ? detectedKey(rawKey, document.getText()) : rawKey if (dotEnding) { if (!key || key.endsWith('.')) @@ -108,7 +111,8 @@ export class KeyDetector { const keys = regexFindKeys(text, regs, dotEnding, rewriteContext, scopes) if (filepath) this._get_keys_cache[filepath] = keys - return keys + + return Config.enableKeyPrefix ? detectedKeys(keys, typeof document === 'string' ? document : document.getText()) : keys } static getUsages(document: TextDocument, loader?: Loader): KeyUsages | undefined { @@ -146,9 +150,23 @@ export class KeyDetector { return { type, - keys, + keys: Config.enableKeyPrefix ? detectedKeys(keys, document.getText()) : keys, locale, namespace, } } } + +function detectedKey(key: string, documentText?: string): string { + if (!documentText) return key + const mathces = documentText.match(/useTranslation\(.+,\s*{\s*keyPrefix:\s*["'`](?.+)["'`]\s*}\s*\)/) + + const keyPath = mathces?.groups?.keyPath + if (!keyPath) return key + + return `${keyPath}.${key}` +} + +export function detectedKeys(keys: T[], documentText?: string): T[] { + return keys.map(value => ({ ...value, key: detectedKey(value.key, documentText) })) +}