Skip to content

Commit

Permalink
fix: handle flags more properly, #313
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 29, 2020
1 parent bc8331b commit 141a561
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
8 changes: 5 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module.exports = {
rules: {
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'no-useless-escape': 'off',
'no-cond-assign': 'off',
'no-unused-expressions': 'off'
'no-unused-expressions': 'off',
},
env: {
jest: true
}
jest: true,
},
}
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"config.include_subfolders": "Search recursively inside locale directories",
"config.indent": "Indent space size for locale files",
"config.keep_fulfill": "Always keep all keys fulfilled with empty strings",
"config.keygen_strategy": "Stragety of generating key.",
"config.keygen_strategy": "Strategy of generating key.",
"config.locale_country_map": "An object to map two letters locale code to country code",
"config.key_prefix": "String prepend to the extracting key.",
"config.keys_in_use": "Keys to mark as in use despite not appearing in the code",
"config.keystyle": "Locale key style",
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@
"default": false,
"description": "%config.keep_fulfill%"
},
"i18n-ally.localeCountryMap": {
"type": "object",
"default": {},
"description": "%config.locale_country_map%"
},
"i18n-ally.indent": {
"type": "number",
"default": 2,
Expand Down
14 changes: 11 additions & 3 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execSync } from 'child_process'
import { workspace, extensions, ExtensionContext } from 'vscode'
import { trimEnd, uniq } from 'lodash'
import { TagSystems } from '../tagSystems'
import { EXT_NAMESPACE, EXT_ID, EXT_LEGACY_NAMESPACE, KEY_REG_DEFAULT, KEY_REG_ALL } from '../meta'
import { EXT_NAMESPACE, EXT_ID, EXT_LEGACY_NAMESPACE, KEY_REG_DEFAULT, KEY_REG_ALL, DEFAULT_LOCALE_COUNTRY_MAP } from '../meta'
import { KeyStyle, DirStructureAuto, TargetPickingStrategy } from '.'

export class Config {
Expand Down Expand Up @@ -213,7 +213,7 @@ export class Config {
return `node "${path.resolve(this.extensionPath!, config)}"`
}

static get parsersTypescriptCompilerOption(): object {
static get parsersTypescriptCompilerOption(): any {
return this.getConfig<any>('parsers.typescript.compilerOptions') || {}
}

Expand Down Expand Up @@ -393,8 +393,16 @@ export class Config {
return this.getConfig<boolean>('showFlags') ?? true
}

static get localeCountryMap() {
return Object.assign(
DEFAULT_LOCALE_COUNTRY_MAP,
this.getConfig<Record<string, string>>('localeCountryMap'),
)
}

static get targetPickingStrategy(): TargetPickingStrategy {
return this.getConfig<TargetPickingStrategy | undefined>('extract.targetPickingStrategy') ?? TargetPickingStrategy.None
return this.getConfig<TargetPickingStrategy | undefined>('extract.targetPickingStrategy')
?? TargetPickingStrategy.None
}

// config
Expand Down
8 changes: 8 additions & 0 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ export const linkedKeyModifiers = {
upper: (str: string) => str.toLocaleUpperCase(),
lower: (str: string) => str.toLocaleLowerCase(),
} as Record<string, (str: string) => string>

export const DEFAULT_LOCALE_COUNTRY_MAP = {
en: 'us',
zh: 'cn',
de: 'de',
fr: 'fr',
ja: 'ja',
}
13 changes: 2 additions & 11 deletions src/tagSystems/bcp47.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
// @ts-ignore
import bcp47 from 'bcp-47'
import { Config } from '../core'
import { BaseTagSystem } from './base'

// https://tools.ietf.org/html/bcp47
export class BCP47 extends BaseTagSystem {
// this maps language code to its default region
flagMapping: Record<string, string> = {
en: 'us',
zh: 'cn',
cs: 'cz',
da: 'dk',
el: 'gr',
}

normalize(locale?: string, fallback = 'en', strict = false) {
if (!locale)
return fallback
Expand All @@ -26,8 +18,7 @@ export class BCP47 extends BaseTagSystem {

toFlagname(locale: string) {
const { region, language } = bcp47.parse(locale, { normalize: true, forgiving: true })
const flag = (region || language || '').toLowerCase()
return this.flagMapping[flag] || flag
return (region || Config.localeCountryMap[language] || '').toLowerCase()
}

lookup(locale: string) {
Expand Down

0 comments on commit 141a561

Please sign in to comment.