Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keyPath support for react-i18n-next #770

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class Config {
'ignoreFiles',
'parserOptions',
'parsers.extendFileExtensions',
'enableKeyPrefix',
]

static readonly refreshConfigs = [
Expand Down Expand Up @@ -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')
}
}
28 changes: 23 additions & 5 deletions src/core/KeyDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
)
}

Expand All @@ -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('.'))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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*["'`](?<keyPath>.+)["'`]\s*}\s*\)/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, little typo here ! ;)


const keyPath = mathces?.groups?.keyPath
if (!keyPath) return key

return `${keyPath}.${key}`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the key separator should be dynamic, as it can be configured in the i18nally configuration

}

export function detectedKeys<T extends {key: string}>(keys: T[], documentText?: string): T[] {
return keys.map(value => ({ ...value, key: detectedKey(value.key, documentText) }))
}