Skip to content

Commit

Permalink
feat: support detection for js/ts/jsx/tsx, close #558
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 5, 2021
1 parent f5c58cb commit e11ec25
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/by-frameworks/react-i18next/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"i18n-ally.enabledFrameworks": [
"react",
"i18next",
"general"
],
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@
"globalize",
"ui5",
"next-translate",
"php-gettext"
"php-gettext",
"general"
]
},
"description": "%config.enabled_frameworks%"
Expand Down
1 change: 1 addition & 0 deletions src/frameworks/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { TextDocument } from 'vscode'
import { LanguageId } from '~/utils'
import { DirStructure, OptionalFeatures, RewriteKeySource, RewriteKeyContext, DataProcessContext, KeyStyle, Config } from '~/core'
Expand Down
4 changes: 2 additions & 2 deletions src/frameworks/chrome-ext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RewriteKeySource, RewriteKeyContext } from '../core/types'
import { RewriteKeySource } from '../core/types'
import { Framework } from './base'
import { LanguageId } from '~/utils'

Expand Down Expand Up @@ -26,7 +26,7 @@ class ChromeExtensionFramework extends Framework {
]
}

rewriteKeys(key: string, source: RewriteKeySource, context: RewriteKeyContext = {}) {
rewriteKeys(key: string, source: RewriteKeySource) {
if (source === 'reference' && !key.endsWith('.message'))
return `${key}.message`
if (source === 'write' && !key.endsWith('.message'))
Expand Down
42 changes: 42 additions & 0 deletions src/frameworks/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { TextDocument } from 'vscode'
import { Framework } from './base'
import { LanguageId } from '~/utils'
import { extractionsParsers, DefaultExtractionRules, DefaultDynamicExtractionsRules } from '~/extraction'

class GeneralFramework extends Framework {
id ='general'
display = 'General'

detection = {
packageJSON: () => true,
}

languageIds: LanguageId[] = [
'javascript',
'typescript',
'javascriptreact',
'typescriptreact',
]

refactorTemplates = () => []
usageMatchRegex = []

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

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

return extractionsParsers.babel.detect(
text,
DefaultExtractionRules,
DefaultDynamicExtractionsRules,
)
}
}

export default GeneralFramework
2 changes: 2 additions & 0 deletions src/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import GlobalizeFramework from './globalize'
import UI5Framework from './ui5'
import NextTranslateFramework from './next-translate'
import PhpGettextFramework from './php-gettext'
import GeneralFramework from './general'
import i18n from '~/i18n'
import { Log } from '~/utils'

Expand Down Expand Up @@ -52,6 +53,7 @@ export const frameworks: Framework[] = [
new UI5Framework(),
new NextTranslateFramework(),
new PhpGettextFramework(),
new GeneralFramework(),

// Vue SFC should be the last one
new VueSFCFramework(),
Expand Down
5 changes: 2 additions & 3 deletions src/frameworks/ui5.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Framework } from './base'
import { DirStructure, RewriteKeySource, RewriteKeyContext } from '~/core'
import { LanguageId } from '~/utils'

class UI5Framework extends Framework {
Expand Down Expand Up @@ -35,7 +34,7 @@ class UI5Framework extends Framework {
]
}

rewriteKeys(key: string, source: RewriteKeySource, context: RewriteKeyContext = {}) {
rewriteKeys(key: string) {
const regexI8n = /i18n>([\w\d\-.]*)/gm
const matches = regexI8n.exec(key)
if (matches && matches.length > 1)
Expand All @@ -45,7 +44,7 @@ class UI5Framework extends Framework {

enabledParsers = ['properties']

pathMatcher(dirStructure?: DirStructure): string {
pathMatcher(): string {
return 'i18n_{locale}.properties'
}

Expand Down

0 comments on commit e11ec25

Please sign in to comment.