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

feat(react-i18n): add support for custom useTranslation function aliases #1232

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
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"config.editor_prefer_editor": "Prefer to use webview editor for locale editing.",
"config.enabled_frameworks": "Specify the supported framework(s) to enable. If no value is set, the extension will detect frameworks automatically.",
"config.enabled_parsers": "Locale file format parsers. By default, It will be handled by activated frameworks",
"config.use_translation_functions": "Aliases for a useTranslation function that can set a scoped namespace. By default, It will use \"useTranslation\".",
"config.encoding": "File encoding for reading and writing locale files",
"config.full_reload_on_changed": "Perform a full reload on locale file changes. (for .js/.ts dynamic import)",
"config.google_api_key": "API key for Google Translate (Optional)",
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,13 @@
},
"description": "%config.enabled_parsers%"
},
"i18n-ally.useTranslationFunctions": {
"type": "array",
"items": {
"type": "string"
},
"description": "%config.use_translation_functions%"
},
"i18n-ally.keysInUse": {
"type": "array",
"items": {
Expand Down
10 changes: 10 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Config {
'includeSubfolders',
'enabledFrameworks',
'enabledParsers',
'useTranslationFunctions',
'dirStructure',
'encoding',
'namespace',
Expand Down Expand Up @@ -155,6 +156,15 @@ export class Config {
return ids
}

static get useTranslationFunctions(): string[] | undefined {
let ids = this.getConfig<string | string[]>('useTranslationFunctions')
if (!ids || !ids.length)
return undefined
if (typeof ids === 'string')
ids = [ids]
return ids
}

static get enabledParsers(): string[] | undefined {
let ids = this.getConfig<string | string[]>('enabledParsers')
if (!ids || !ids.length)
Expand Down
8 changes: 5 additions & 3 deletions src/frameworks/react-i18next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ class ReactI18nextFramework extends Framework {

// Add first namespace as a global scope resetting on each occurrence
// useTranslation(ns1) and useTranslation(['ns1', ...])
const regUse = /useTranslation\(\s*\[?\s*['"`](.*?)['"`]/g
// we support alternate naming of useTranslation via config
const useTranslationFunctions = Config.useTranslationFunctions ?? ['useTranslation'];
const regUse = new RegExp(`(${useTranslationFunctions.join('|')})\\(\\s*\\[?\\s*['"\`](?<namespace>.*?)['"\`]`, 'g');
let prevGlobalScope = false
for (const match of text.matchAll(regUse)) {
if (typeof match.index !== 'number')
Expand All @@ -173,12 +175,12 @@ class ReactI18nextFramework extends Framework {
ranges[ranges.length - 1].end = match.index

// start a new scope if namespace is provided
if (match[1]) {
if (match.groups?.namespace) {
prevGlobalScope = true
ranges.push({
start: match.index,
end: text.length,
namespace: match[1],
namespace: match.groups.namespace,
})
}
}
Expand Down