Skip to content

Commit 7ee58be

Browse files
Add extraction support to react-i18next framework
This commit adds the `supportAutoExtraction` array of supported formats and the `detectHardStrings` method from `GeneralFramework` to enable hard-coded string extraction for react-i18next framework, without needing the "general" framework to be enabled/loaded in the `.vscode/settings.json` file.
1 parent 43df97d commit 7ee58be

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

examples/by-frameworks/react-i18next/.vscode/settings.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"i18n-ally.localesPaths": "public/locales",
33
"i18n-ally.enabledFrameworks": [
4-
"react-i18next",
5-
"general"
4+
"react-i18next"
65
],
76
"i18n-ally.namespace": true,
87
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json",

src/frameworks/react-i18next.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { TextDocument } from 'vscode'
22
import { Framework, ScopeRange } from './base'
33
import { LanguageId } from '~/utils'
4-
import { RewriteKeySource, RewriteKeyContext } from '~/core'
4+
import { extractionsParsers, DefaultExtractionRules, DefaultDynamicExtractionsRules } from '~/extraction'
5+
import { Config, RewriteKeySource, RewriteKeyContext } from '~/core'
56

67
class ReactI18nextFramework extends Framework {
78
id = 'react-i18next'
@@ -33,6 +34,14 @@ class ReactI18nextFramework extends Framework {
3334
'\\Wi18nKey=[\'"`]({key})[\'"`]',
3435
]
3536

37+
supportAutoExtraction = [
38+
'javascript',
39+
'typescript',
40+
'javascriptreact',
41+
'typescriptreact',
42+
'html',
43+
]
44+
3645
derivedKeyRules = [
3746
'{key}_plural',
3847
'{key}_0',
@@ -51,9 +60,37 @@ class ReactI18nextFramework extends Framework {
5160
'{key}_two',
5261
'{key}_few',
5362
'{key}_many',
54-
'{key}_other'
63+
'{key}_other',
5564
]
5665

66+
detectHardStrings(doc: TextDocument) {
67+
const lang = doc.languageId
68+
const text = doc.getText()
69+
70+
if (lang === 'html') {
71+
return extractionsParsers.html.detect(
72+
text,
73+
DefaultExtractionRules,
74+
DefaultDynamicExtractionsRules,
75+
Config.extractParserHTMLOptions,
76+
// <script>
77+
script => extractionsParsers.babel.detect(
78+
script,
79+
DefaultExtractionRules,
80+
DefaultDynamicExtractionsRules,
81+
Config.extractParserBabelOptions,
82+
),
83+
)
84+
}
85+
else {
86+
return extractionsParsers.babel.detect(
87+
text,
88+
DefaultExtractionRules,
89+
DefaultDynamicExtractionsRules,
90+
)
91+
}
92+
}
93+
5794
refactorTemplates(keypath: string) {
5895
return [
5996
`{t('${keypath}')}`,
@@ -126,7 +163,7 @@ class ReactI18nextFramework extends Framework {
126163
// Add first namespace as a global scope resetting on each occurrence
127164
// useTranslation(ns1) and useTranslation(['ns1', ...])
128165
const regUse = /useTranslation\(\s*\[?\s*['"`](.*?)['"`]/g
129-
let prevGlobalScope = false;
166+
let prevGlobalScope = false
130167
for (const match of text.matchAll(regUse)) {
131168
if (typeof match.index !== 'number')
132169
continue
@@ -137,7 +174,7 @@ class ReactI18nextFramework extends Framework {
137174

138175
// start a new scope if namespace is provided
139176
if (match[1]) {
140-
prevGlobalScope = true;
177+
prevGlobalScope = true
141178
ranges.push({
142179
start: match.index,
143180
end: text.length,

test/e2e/frameworks/react-i18next/basic.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ setupTest('React with i18next', () => {
1414
it('enables correct frameworks', async() => {
1515
not(Global, undefined)
1616
is(Global.enabled, true)
17-
is(Global.enabledFrameworks.length, 2)
17+
is(Global.enabledFrameworks.length, 1)
1818
is(Global.enabledFrameworks[0].id, 'react-i18next')
19-
is(Global.enabledFrameworks[1].id, 'general')
2019
})
2120

2221
it('get correct coverage report', async() => {

0 commit comments

Comments
 (0)