Skip to content

Commit 216b52b

Browse files
committed
feat: new config "keystyle"
1 parent 4c403e2 commit 216b52b

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@
233233
"vue-i18n-ally.ignoredLocales": {
234234
"type": "array",
235235
"description": "%config.ignored_locales%"
236+
},
237+
"vue-i18n-ally.keystyle": {
238+
"type": "string",
239+
"enum": [
240+
"auto",
241+
"nested",
242+
"flat"
243+
],
244+
"default": "auto",
245+
"description": "%config.keystyle%"
236246
}
237247
}
238248
}

package.nls.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"config.source_language": "Source language for machine translation",
2323
"config.display_language": "Displaying language",
2424
"config.ignored_locales": "Locale codes to be ingored",
25+
"config.keystyle": "Locale key style",
2526
"misc.not_exists": "🔴Not exist",
2627
"refactor.extract_text": "Extract text to vue-i18n locales",
2728
"prompt.config_locales_auto_success": "Locales path auto set to \"{0}\".",
@@ -41,5 +42,10 @@
4142
"prompt.translation_saved": "Translation saved.",
4243
"prompt.failed_to_locale_key": "Failed to locale key \"{0}\".",
4344
"prompt.edit_key_in_locale": "{1} | Editing key \"{0}\"",
44-
"prompt.enter_new_keypath": "Enter the new keypath"
45+
"prompt.enter_new_keypath": "Enter the new keypath",
46+
"prompt.keystyle_flat": "Flat style",
47+
"prompt.keystyle_flat_example": "for example: { \"a.b.c\": \"...\" }",
48+
"prompt.keystyle_nested": "Nested style",
49+
"prompt.keystyle_nested_example": "for example: { \"a\": { \"b\": { \"c\": \"...\" } } }",
50+
"prompt.keystyle_select": "Which kind of key style do you use to organize your locales?"
4551
}

src/core/Errors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export enum ErrorType {
33
translating_unknown_error,
44
translating_empty_source_value,
55
filepath_not_specified,
6-
unsupported_file_type
6+
unsupported_file_type,
7+
keystyle_not_set
78
}
89

910
export class AllyError extends Error {

src/core/Global.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { YamlParser } from '../parsers/YamlParser'
77
import { JavascriptParser } from '../parsers/JavascriptParser'
88
import { ConfigLocalesGuide } from '../commands/configLocales'
99
import { extname } from 'path'
10+
import i18n from '../i18n'
1011

1112
const configPrefix = 'vue-i18n-ally'
13+
export type KeyStyle = 'auto' | 'nested' | 'flat'
1214

1315
export class Global {
1416
private static _loaders: Record<string, LocaleLoader> = {}
@@ -190,6 +192,38 @@ export class Global {
190192
this._onDidChangeLoader.fire(this.loader)
191193
}
192194

195+
static get keyStyle (): KeyStyle {
196+
return (Global.getConfig('keystyle') || 'auto') as KeyStyle
197+
}
198+
199+
static set keyStyle (value: KeyStyle) {
200+
Global.setConfig('keystyle', value, false)
201+
}
202+
203+
static async requestKeyStyle (): Promise<KeyStyle | undefined> {
204+
if (this.keyStyle !== 'auto')
205+
return this.keyStyle
206+
207+
const result = await window.showQuickPick([{
208+
value: 'nested',
209+
label: i18n.t('prompt.keystyle_nested'),
210+
description: i18n.t('prompt.keystyle_nested_example'),
211+
}, {
212+
value: 'flat',
213+
label: i18n.t('prompt.keystyle_flat'),
214+
description: i18n.t('prompt.keystyle_flat_example'),
215+
}], {
216+
placeHolder: i18n.t('prompt.keystyle_select'),
217+
})
218+
219+
if (!result) {
220+
this.keyStyle = 'nested'
221+
return 'nested'
222+
}
223+
this.keyStyle = result.value as KeyStyle
224+
return result.value as KeyStyle
225+
}
226+
193227
static toggleLocaleVisibility (locale: string, visible?: boolean) {
194228
const ignored = this.ignoredLocales
195229
if (visible == null)

src/core/LocaleLoader.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,15 @@ export class LocaleLoader extends Disposable {
303303
if (!parser)
304304
throw new AllyError(ErrorType.unsupported_file_type, ext)
305305

306-
let original: object = {}
306+
let original: any = {}
307307
if (existsSync(filepath))
308308
original = await parser.load(filepath)
309309

310-
_.set(original, pending.keypath, pending.value)
310+
const keyStyle = await Global.requestKeyStyle()
311+
if (keyStyle === 'flat')
312+
original[pending.keypath] = pending.value
313+
else
314+
_.set(original, pending.keypath, pending.value)
311315

312316
await parser.save(filepath, original)
313317
}

0 commit comments

Comments
 (0)