1
1
import { TextDocument } from 'vscode'
2
2
import { Framework , ScopeRange } from './base'
3
3
import { LanguageId } from '~/utils'
4
- import { RewriteKeySource , RewriteKeyContext } from '~/core'
4
+ import { extractionsParsers , DefaultExtractionRules , DefaultDynamicExtractionsRules } from '~/extraction'
5
+ import { Config , RewriteKeySource , RewriteKeyContext } from '~/core'
5
6
6
7
class ReactI18nextFramework extends Framework {
7
8
id = 'react-i18next'
@@ -33,6 +34,14 @@ class ReactI18nextFramework extends Framework {
33
34
'\\Wi18nKey=[\'"`]({key})[\'"`]' ,
34
35
]
35
36
37
+ supportAutoExtraction = [
38
+ 'javascript' ,
39
+ 'typescript' ,
40
+ 'javascriptreact' ,
41
+ 'typescriptreact' ,
42
+ 'html' ,
43
+ ]
44
+
36
45
derivedKeyRules = [
37
46
'{key}_plural' ,
38
47
'{key}_0' ,
@@ -51,9 +60,37 @@ class ReactI18nextFramework extends Framework {
51
60
'{key}_two' ,
52
61
'{key}_few' ,
53
62
'{key}_many' ,
54
- '{key}_other'
63
+ '{key}_other' ,
55
64
]
56
65
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
+
57
94
refactorTemplates ( keypath : string ) {
58
95
return [
59
96
`{t('${ keypath } ')}` ,
@@ -126,7 +163,7 @@ class ReactI18nextFramework extends Framework {
126
163
// Add first namespace as a global scope resetting on each occurrence
127
164
// useTranslation(ns1) and useTranslation(['ns1', ...])
128
165
const regUse = / u s e T r a n s l a t i o n \( \s * \[ ? \s * [ ' " ` ] ( .* ?) [ ' " ` ] / g
129
- let prevGlobalScope = false ;
166
+ let prevGlobalScope = false
130
167
for ( const match of text . matchAll ( regUse ) ) {
131
168
if ( typeof match . index !== 'number' )
132
169
continue
@@ -137,7 +174,7 @@ class ReactI18nextFramework extends Framework {
137
174
138
175
// start a new scope if namespace is provided
139
176
if ( match [ 1 ] ) {
140
- prevGlobalScope = true ;
177
+ prevGlobalScope = true
141
178
ranges . push ( {
142
179
start : match . index ,
143
180
end : text . length ,
0 commit comments