Skip to content

Commit

Permalink
feat: add detect hard string function
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Sep 5, 2023
1 parent afe0901 commit d83daf3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"i18n-ally.enabledFrameworks": ["next-international"],
"eslint.enable": false,
"i18n-ally.enabledParsers": ["ts"],
"i18n-ally.keystyle": "nested"
"i18n-ally.keystyle": "flat"
}
2 changes: 2 additions & 0 deletions src/extraction/parsers/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function detect(
rules: ExtractionRule[] = DefaultExtractionRules,
dynamicRules: ExtractionRule[] = DefaultDynamicExtractionsRules,
userOptions: ExtractionBabelOptions = {},
customCallExpression?: (path: any, recordIgnore: (path: any) => void) => void,
) {
const {
ignoredJSXAttributes,
Expand Down Expand Up @@ -99,6 +100,7 @@ export function detect(
},
// ignore `console.xxx`
CallExpression(path: any) {
if (customCallExpression) customCallExpression(path, recordIgnore)
const callee = path.get('callee')
if (!callee.isMemberExpression()) return
if (isGlobalConsoleId(callee.get('object')))
Expand Down
46 changes: 44 additions & 2 deletions src/frameworks/next-international.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-console */
import { TextDocument } from 'vscode'
import { Framework, ScopeRange } from './base'
import { LanguageId } from '~/utils'
import { RewriteKeySource, RewriteKeyContext, KeyStyle } from '~/core'
import { RewriteKeySource, RewriteKeyContext, KeyStyle, Config } from '~/core'
import { extractionsParsers, DefaultExtractionRules, DefaultDynamicExtractionsRules } from '~/extraction'

class NextInternationalFramework extends Framework {
id = 'next-international'
Expand All @@ -26,13 +26,55 @@ class NextInternationalFramework extends Framework {
'typescriptreact',
]

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

usageMatchRegex = [
// Basic usage
'[^\\w\\d]t\\([\'"`]({key})[\'"`]',
// Scoped usage
'[^\\w\\d]scopedT\\([\'"`]({key})[\'"`]',
]

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,
{},
(path, recordIgnore) => {
const callee = path.get('callee')
if (callee.node.name === 't' || callee.node.name === 'scopedT')
recordIgnore(path)
},
)
}
}

refactorTemplates(keypath: string) {
const keypaths = keypath.split('.').map((_, index, parts) => {
return parts.slice(parts.length - index - 1).join('.')
Expand Down

0 comments on commit d83daf3

Please sign in to comment.