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 extraction support to react-i18next framework #4

Merged
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
3 changes: 1 addition & 2 deletions examples/by-frameworks/react-i18next/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"i18n-ally.localesPaths": "public/locales",
"i18n-ally.enabledFrameworks": [
"react-i18next",
"general"
"react-i18next"
],
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json",
Expand Down
45 changes: 41 additions & 4 deletions src/frameworks/react-i18next.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TextDocument } from 'vscode'
import { Framework, ScopeRange } from './base'
import { LanguageId } from '~/utils'
import { RewriteKeySource, RewriteKeyContext } from '~/core'
import { extractionsParsers, DefaultExtractionRules, DefaultDynamicExtractionsRules } from '~/extraction'
import { Config, RewriteKeySource, RewriteKeyContext } from '~/core'

class ReactI18nextFramework extends Framework {
id = 'react-i18next'
Expand Down Expand Up @@ -33,6 +34,14 @@ class ReactI18nextFramework extends Framework {
'\\Wi18nKey=[\'"`]({key})[\'"`]',
]

supportAutoExtraction = [
'javascript',
'typescript',
'javascriptreact',
'typescriptreact',
'html',
]

derivedKeyRules = [
'{key}_plural',
'{key}_0',
Expand All @@ -51,9 +60,37 @@ class ReactI18nextFramework extends Framework {
'{key}_two',
'{key}_few',
'{key}_many',
'{key}_other'
'{key}_other',
]

detectHardStrings(doc: TextDocument) {
const lang = doc.languageId
const text = doc.getText()

if (lang === 'html') {
return extractionsParsers.html.detect(
text,
DefaultExtractionRules,
DefaultDynamicExtractionsRules,
Config.extractParserHTMLOptions,
// <script>
script => extractionsParsers.babel.detect(
script,
DefaultExtractionRules,
DefaultDynamicExtractionsRules,
Config.extractParserBabelOptions,
),
)
}
else {
return extractionsParsers.babel.detect(
text,
DefaultExtractionRules,
DefaultDynamicExtractionsRules,
)
}
}

refactorTemplates(keypath: string) {
return [
`{t('${keypath}')}`,
Expand Down Expand Up @@ -126,7 +163,7 @@ 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
let prevGlobalScope = false;
let prevGlobalScope = false
for (const match of text.matchAll(regUse)) {
if (typeof match.index !== 'number')
continue
Expand All @@ -137,7 +174,7 @@ class ReactI18nextFramework extends Framework {

// start a new scope if namespace is provided
if (match[1]) {
prevGlobalScope = true;
prevGlobalScope = true
ranges.push({
start: match.index,
end: text.length,
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/frameworks/react-i18next/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ setupTest('React with i18next', () => {
it('enables correct frameworks', async() => {
not(Global, undefined)
is(Global.enabled, true)
is(Global.enabledFrameworks.length, 2)
is(Global.enabledFrameworks.length, 1)
is(Global.enabledFrameworks[0].id, 'react-i18next')
is(Global.enabledFrameworks[1].id, 'general')
})

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