Skip to content

Commit

Permalink
feat: make locale reusable and use ini to read file
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolPlayLin committed Dec 9, 2023
1 parent 88fe54e commit 80b665d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"devDependencies": {
"@tsconfig/node18": "^18.2.2",
"@types/eslint": "^8.44.8",
"@types/ini": "^1.3.34",
"@types/node": "^18.19.2",
"@types/prompts": "^2.4.9",
"@vue/create-eslint-config": "^0.3.2",
Expand All @@ -59,5 +60,8 @@
"*.{js,ts,vue,json}": [
"prettier --write"
]
},
"dependencies": {
"ini": "^4.1.1"
}
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions utils/getLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'node:fs'
import * as path from 'node:path'
import { parse } from 'ini'

interface LanguageItem {
hint?: string
Expand Down Expand Up @@ -43,6 +44,20 @@ interface Language {
}
}

function linkLocale(locale: string): string {
let linkedLocale: string
switch (locale) {
case 'zh-CN':
case 'zh-SG':
linkedLocale = 'zh-Hans'
break
default:
linkedLocale = locale
}

return linkedLocale
}

function getLocale() {
const shellLocale =
process.env.LC_ALL || // POSIX locale environment variables
Expand All @@ -51,9 +66,7 @@ function getLocale() {
Intl.DateTimeFormat().resolvedOptions().locale || // Built-in ECMA-402 support
'en-US' // Default fallback

const locale = shellLocale.split('.')[0].replace('_', '-')

return locale
return linkLocale(shellLocale.split('.')[0].replace('_', '-'))
}

export default function getLanguage() {
Expand All @@ -66,9 +79,9 @@ export default function getLanguage() {
const languageFilePath = path.resolve(localesRoot, `${locale}.json`)
const doesLanguageExist = fs.existsSync(languageFilePath)

const lang: Language = doesLanguageExist
? require(languageFilePath)
: require(path.resolve(localesRoot, 'en-US.json'))
const lang = (
doesLanguageExist ? parse(languageFilePath) : parse(path.resolve(localesRoot, 'en-US.json'))
) as Language

return lang
}

0 comments on commit 80b665d

Please sign in to comment.