-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): add locales helper (#44)
- Loading branch information
1 parent
a692d7a
commit f1be46f
Showing
25 changed files
with
837 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { fromEntries, keys } from '../../shared/index.js' | ||
import type { KnownLangCode } from './types.js' | ||
|
||
export const lang2PathConfig = { | ||
'de-AT': '/de-at/', | ||
'de-DE': '/de/', | ||
'en-US': '/en/', | ||
'es-ES': '/es/', | ||
'fi-FI': '/fi/', | ||
'fr-FR': '/fr/', | ||
'hu-HU': '/hu/', | ||
'id-ID': '/id/', | ||
'ja-JP': '/ja/', | ||
'ko-KR': '/ko/', | ||
'nl-NL': '/nl/', | ||
'pl-PL': '/pl/', | ||
'pt-BR': '/br/', | ||
'ru-RU': '/ru/', | ||
'sk-SK': '/sk/', | ||
'tr-TR': '/tr/', | ||
'uk-UA': '/uk/', | ||
'vi-VN': '/vi/', | ||
'zh-CN': '/zh/', | ||
'zh-TW': '/zh-tw/', | ||
} | ||
|
||
export const supportedLangs = keys(lang2PathConfig) | ||
|
||
export const path2langConfig = fromEntries( | ||
(supportedLangs as KnownLangCode[]).map((lang) => [ | ||
lang2PathConfig[lang], | ||
lang, | ||
]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import type { App } from 'vuepress/core' | ||
import type { LocaleConfig, LocaleData } from 'vuepress/shared' | ||
import type { ExactLocaleConfig } from '../../shared/index.js' | ||
import { deepAssign, fromEntries, keys } from '../../shared/index.js' | ||
import { Logger } from '../utils/index.js' | ||
import { lang2PathConfig, path2langConfig } from './config.js' | ||
import type { KnownLangCode } from './types.js' | ||
|
||
/** Get language from path */ | ||
export const path2Lang = (path = '', debug = false): KnownLangCode => { | ||
if (path in path2langConfig) return path2langConfig[path] | ||
|
||
if (debug) | ||
console.warn( | ||
`${path} isn’t assign with a lang, and will return "en-US" instead.`, | ||
) | ||
|
||
return 'en-US' | ||
} | ||
|
||
/** Get path from language */ | ||
export const lang2Path = (lang = '', debug = false): string => { | ||
if (lang in lang2PathConfig) return lang2PathConfig[lang as KnownLangCode] | ||
|
||
if (debug) | ||
console.warn(`${lang} has no path config, and will return "/" instead.`) | ||
|
||
return '/' | ||
} | ||
|
||
/** | ||
* Get language of root directory | ||
* | ||
* @param app VuePress Node App | ||
* @returns root language | ||
*/ | ||
export const getRootLang = (app: App): string => { | ||
// infer from siteLocale | ||
const siteLocales = app.siteData.locales | ||
|
||
if (siteLocales?.['/'] && siteLocales['/']?.lang) return siteLocales['/'].lang | ||
|
||
return app.siteData.lang | ||
} | ||
|
||
/** | ||
* Get the infer language path from root directory language | ||
* | ||
* @param app VuePress Node App | ||
* @returns infer language | ||
*/ | ||
export const getRootLangPath = (app: App): string => | ||
lang2Path(getRootLang(app), app.env.isDebug) | ||
|
||
/** | ||
* Get locale paths | ||
* | ||
* @param app VuePress Node app | ||
* @returns locale paths | ||
*/ | ||
export const getLocalePaths = (app: App): string[] => | ||
Array.from(new Set(keys(app.siteData.locales))) | ||
|
||
export interface LocaleConfigOptions<T extends LocaleData> { | ||
/** VuePress Node app */ | ||
app: App | ||
/** Default locale config */ | ||
default: ExactLocaleConfig<T> | ||
/** user locale config */ | ||
config?: LocaleConfig<T> | undefined | ||
/** plugin name */ | ||
name?: string | ||
} | ||
|
||
/** | ||
* Get final locale config for client | ||
* | ||
* @returns final locale config | ||
*/ | ||
export const getLocaleConfig = <T extends LocaleData>({ | ||
app, | ||
name, | ||
default: defaultLocalesConfig, | ||
config: userLocalesConfig = {}, | ||
}: LocaleConfigOptions<T>): ExactLocaleConfig<T> => { | ||
const rootPath = getRootLangPath(app) | ||
const logger = new Logger(name) | ||
|
||
return fromEntries([ | ||
...getLocalePaths(app) | ||
.filter((localePath) => localePath !== '/') | ||
.map<[string, T]>((localePath) => { | ||
const defaultLocaleData = | ||
defaultLocalesConfig[localePath] || | ||
(lang2Path(app.options.locales[localePath].lang) === '/' | ||
? null | ||
: defaultLocalesConfig[ | ||
lang2Path(app.options.locales[localePath].lang) | ||
]) | ||
|
||
if (!defaultLocaleData) | ||
logger.warn(`Locale ${localePath} is missing it's i18n config`) | ||
|
||
return [ | ||
localePath, | ||
deepAssign( | ||
{}, | ||
defaultLocaleData || defaultLocalesConfig[rootPath] || {}, | ||
userLocalesConfig[localePath] || {}, | ||
), | ||
] | ||
}), | ||
[ | ||
'/', | ||
deepAssign( | ||
{}, | ||
defaultLocalesConfig[rootPath], | ||
userLocalesConfig['/'] || userLocalesConfig[rootPath] || {}, | ||
), | ||
], | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './helpers.js' | ||
export * from './types.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { lang2PathConfig } from './config.js' | ||
|
||
/** Types for supported lang codes */ | ||
export type KnownLangCode = keyof typeof lang2PathConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createRequire } from 'node:module' | ||
|
||
export const getInstalledStatus = ( | ||
pkg: string, | ||
currentUrl: string, | ||
): boolean => { | ||
try { | ||
pkg && createRequire(currentUrl).resolve(pkg) | ||
|
||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createRequire } from 'node:module' | ||
import { path } from 'vuepress/utils' | ||
|
||
export const getRealPath = (fileUrl: string, currentUrl: string): string => { | ||
const require = createRequire(currentUrl) | ||
|
||
return path.normalize(require.resolve(fileUrl)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './getInstalledStatus.js' | ||
export * from './getRealPath.js' | ||
export * from './logger.js' | ||
export * from './packageManager.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { entries, isArray, isPlainObject } from './helper.js' | ||
|
||
type IAnyObject = Record<string, any> | ||
|
||
/** Deep merge objects to the first one */ | ||
export const deepAssign = < | ||
T extends IAnyObject, | ||
U extends IAnyObject = T, | ||
V extends Partial<T> & Partial<U> = T & U, | ||
>( | ||
originObject: T, | ||
...overrideObjects: (U | null | undefined)[] | ||
): V => { | ||
if (overrideObjects.length === 0) return originObject as unknown as V | ||
|
||
/** Object being merged */ | ||
const assignObject = overrideObjects.shift() || null | ||
|
||
if (assignObject) | ||
entries(assignObject).forEach(([property, value]) => { | ||
if (property === '__proto__' || property === 'constructor') return | ||
if (isPlainObject(originObject[property]) && isPlainObject(value)) | ||
deepAssign(originObject[property], value) | ||
else if (isArray(value)) | ||
(originObject as IAnyObject)[property] = [...value] | ||
else if (isPlainObject(value)) | ||
(originObject as IAnyObject)[property] = { | ||
...value, | ||
} | ||
else (originObject as IAnyObject)[property] = assignObject[property] | ||
}) | ||
|
||
return deepAssign(originObject, ...overrideObjects) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from 'vuepress/shared' | ||
export * from './deepAssign.js' | ||
export * from './date.js' | ||
export * from './helper.js' | ||
export * from './url.js' | ||
export * from './locales.js' | ||
export * from './link.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { startsWith } from './helper.js' | ||
|
||
export { isLinkExternal, isLinkHttp, isLinkWithProtocol } from 'vuepress/shared' | ||
|
||
/** | ||
* Whether a variable is a valid absolute url | ||
*/ | ||
export const isLinkAbsolute = (test: unknown): boolean => startsWith(test, '/') |
Oops, something went wrong.