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 typesafe-i18n support #681

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import GeneralFramework from './general'
import LinguiFramework from './lingui'
import JekyllFramework from './jekyll'
import FluentVueSFCFramework from './fluent-vue-sfc'
import TypesafeI18nFramework from './typesafe-i18n'
import i18n from '~/i18n'
import { Log } from '~/utils'

Expand Down Expand Up @@ -61,6 +62,7 @@ export const frameworks: Framework[] = [
new LinguiFramework(),
new JekyllFramework(),
new GeneralFramework(),
new TypesafeI18nFramework(),

// Vue SFC and FluentVue SFC should be the last ones
new VueSFCFramework(),
Expand Down
74 changes: 74 additions & 0 deletions src/frameworks/typesafe-i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { TextDocument } from 'vscode'
import { Framework } from './base'
import { DetectionResult } from '~/core'
import { LanguageId } from '~/utils'

class TypesafeI18nFramework extends Framework {
id = 'typesafe-i18n'
display = 'typesafe-i18n'

monopoly = true

detection = {
packageJSON: [
'typesafe-i18n',
],
}

enabledParsers = ['ts', 'js']

pathMatcher = () => '{locale}/index.{ext}'

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

usageMatchRegex = [
'\\$?LL\\.({key})\\(((\\{.*\\})|([^)]*))\\)',
]

perferredKeystyle = 'nested' as const

perferredLocalePaths = ['src/i18n']

private replaceKeypath = (keypath: string, text: string) =>
text.replace('$1', keypath.split('.').map(part => `['${part}']`).join(''))

private getHtmlRefactorTemplates = (keypath: string, params: string) => [
`{LL{key}(${params})}`, // other
`{$LL{key}(${params})}`, // svelte
].map(text => this.replaceKeypath(keypath, text))

private getJavaScriptRefactorTemplates = (keypath: string, params: string) => [
`LL{key}(${params})`, // other
`$LL{key}(${params})`, // svelte
].map(text => this.replaceKeypath(keypath, text))

refactorTemplates(keypath: string, args: string[] = [], _doc?: TextDocument, detection?: DetectionResult) {
let params = ''
if (args.length)
params += `${args.join(', ')}`

switch (detection?.source) {
case 'html-inline':
case 'html-attribute':
return this.getHtmlRefactorTemplates(keypath, params)
case 'js-string':
case 'js-template':
case 'jsx-text':
return this.getJavaScriptRefactorTemplates(keypath, params)
}

return [
...this.getHtmlRefactorTemplates(keypath, params),
...this.getJavaScriptRefactorTemplates(keypath, params),
]
}
}

export default TypesafeI18nFramework