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

feat: add prefix suffix config of args #965

Open
wants to merge 7 commits 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 locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
"config.translate_override_existing": "Override existing messages on translating",
"config.translate_save_as_candidates": "Save translations result to \"Translation Candidates\" instead of writting to the locale files.",
"config.usage.scanning_ignore": "Ignore glob patterns for scanning key usage",
"config.args_prefix": "Set prefix of args in hard code",
"config.args_suffix": "Set suffix of args in hard code",
"editor.add_description": "Add description...",
"editor.empty": "(empty)",
"editor.title": "i18n Ally Editor",
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,16 @@
"type": "string",
"description": "%config.default_namespace%"
},
"i18n-ally.argsPrefix": {
"type": "string",
"default": "{",
"description": "%config.args_prefix%"
},
"i18n-ally.argsSuffix": {
"type": "string",
"default": "}",
"description": "%config.args_suffix%"
},
"i18n-ally.derivedKeyRules": {
"deprecationMessage": "Deprecated. Use \"i18n-ally.usage.derivedKeyRules\" instead."
},
Expand Down
4 changes: 2 additions & 2 deletions src/commands/extractString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { commands, window, QuickPickItem, Range, TextDocument } from 'vscode'
import { trim } from 'lodash'
import { overrideConfirm } from './overrideConfirm'
import { Commands } from './commands'
import { keypathValidate, Log, promptTemplates } from '~/utils'
import { keypathValidate, Log, preSufArgString, promptTemplates } from '~/utils'
import { ExtensionModule } from '~/modules'
import { extractHardStrings, generateKeyFromText, Config, CurrentFile, DetectionResult, Telemetry, TelemetryKey } from '~/core'
import i18n from '~/i18n'
Expand Down Expand Up @@ -53,7 +53,7 @@ async function ExtractOrInsertCommnad(options?: ExtractTextOptions, detection?:
const loader = CurrentFile.loader

if (options.rawText && !options.text) {
const result = parseHardString(options.rawText, options.document?.languageId, options.isDynamic)
const result = parseHardString(options.rawText, options.document?.languageId, options.isDynamic, preSufArgString)
options.text = result?.text || ''
options.args = result?.args
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/extractStringBulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { extractHardStrings, generateKeyFromText } from '~/core/Extract'
import { Config, Global } from '~/core'
import { parseHardString } from '~/extraction/parseHardString'
import { DetectionResultToExtraction } from '~/editor/extract'
import { Log } from '~/utils'
import { Log, preSufArgString } from '~/utils'
import { gitignoredGlob } from '~/utils/glob'
import { ActionSource, Telemetry, TelemetryKey } from '~/core/Telemetry'

Expand Down Expand Up @@ -72,7 +72,7 @@ export async function BatchHardStringExtraction(...args: any[]) {
const options = DetectionResultToExtraction(i, document)

if (options.rawText && !options.text) {
const result = parseHardString(options.rawText, options.document.languageId, options.isDynamic)
const result = parseHardString(options.rawText, options.document.languageId, options.isDynamic, preSufArgString)
options.text = result?.text || ''
options.args = result?.args
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,12 @@ export class Config {
static get telemetry(): boolean {
return workspace.getConfiguration().get('telemetry.enableTelemetry') as boolean
}

static get argsSuffix() {
return this.getConfig<string>('argsSuffix')
}

static get argsPrefix() {
return this.getConfig<string>('argsPrefix')
}
}
2 changes: 1 addition & 1 deletion src/core/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class Global {
}
return true
})
const argsString = args?.length ? `,${args?.join(',')}` : ''
const argsString = args?.length ? `${args?.join(',')}` : ''

const customReplacers = customTemplates
.flatMap(i => i.templates)
Expand Down
3 changes: 2 additions & 1 deletion src/editor/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import i18n from '~/i18n'
import { parseHardString } from '~/extraction/parseHardString'
import { ExtractTextOptions } from '~/commands/extractString'
import { DetectionResult } from '~/core/types'
import { preSufArgString } from '~/utils'

export function DetectionResultToExtraction(detection: DetectionResult, document: TextDocument): ExtractTextOptions {
return {
Expand Down Expand Up @@ -80,7 +81,7 @@ class ExtractProvider implements CodeActionProvider {
if (!(selection instanceof Selection))
return []

const result = parseHardString(document.getText(selection), document.languageId)
const result = parseHardString(document.getText(selection), document.languageId, false, preSufArgString)
if (!result)
return []

Expand Down
5 changes: 2 additions & 3 deletions src/extraction/parseHardString.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* 'foo' + bar() + ' is cool' -> `foo${bar()} is cool`
*/
Expand All @@ -23,7 +22,7 @@ export function stringConcatenationToTemplate(text: string) {
return text
}

export function parseHardString(text = '', languageId?: string, isDynamic = false) {
export function parseHardString(text = '', languageId?: string, isDynamic = false, argsToStringCallback = (args:string[])=>`{${args.length - 1}}`) {
const trimmed = text.trim().replace(/\s*\r?\n\s*/g, ' ')
let processed = trimmed
const args: string[] = []
Expand All @@ -35,7 +34,7 @@ export function parseHardString(text = '', languageId?: string, isDynamic = fals

processed = processed.replace(/(?:\{\{(.*?)\}\}|\$\{(.*?)\})/g, (full, content, content2) => {
args.push((content ?? content2 ?? '').trim())
return `{${args.length - 1}}`
return argsToStringCallback(args)
})

return {
Expand Down
8 changes: 4 additions & 4 deletions src/frameworks/react-i18next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class ReactI18nextFramework extends Framework {
}
}

refactorTemplates(keypath: string) {
refactorTemplates(keypath: string, args: string[]) {
return [
`{t('${keypath}')}`,
`t('${keypath}')`,
`<Trans i18nKey="${keypath}"></Trans>`,
`t('${keypath}'${args.length ? `, [${args.join(', ')}]` : ''})`,
`{t('${keypath}'${args.length ? `, [${args.join(', ')}]` : ''})}`,
`<Trans i18nKey="${keypath}"${args.length ? ` values={[${args.join(', ')}]}` : ''}></Trans>`,
keypath,
]
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ export function getLocaleCompare(
const sortLocale = sortLocaleSetting ? sortLocaleSetting : fileLocale;
return new Intl.Collator(sortLocale).compare;
}

export function preSufArgString(args: string[]){
return `${Config.argsPrefix}${args.length - 1}${Config.argsSuffix}`
}