forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: integrate and replace
vue-i18n-routing
- Loading branch information
1 parent
d1a4790
commit 6a5fbf2
Showing
18 changed files
with
146 additions
and
116 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,99 @@ | ||
import { isString, isObject, makeSymbol } from '@intlify/shared' | ||
import { warn, getLocalesRegex } from '../utils' | ||
|
||
import type { I18nRoutingOptions } from '#build/i18n.options.mjs' | ||
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded, Router } from 'vue-router' | ||
|
||
/** | ||
* Global options for i18n routing | ||
*/ | ||
export type I18nRoutingGlobalOptions<Context = unknown> = Pick< | ||
I18nRoutingOptions<Context>, | ||
| 'defaultLocale' | ||
| 'defaultDirection' | ||
| 'defaultLocaleRouteNameSuffix' | ||
| 'trailingSlash' | ||
| 'routesNameSeparator' | ||
| 'strategy' | ||
| 'prefixable' | ||
| 'switchLocalePathIntercepter' | ||
| 'dynamicRouteParamsKey' | ||
> & { localeCodes?: string[] } | ||
|
||
const GlobalOptionsRegistry = makeSymbol('nuxt-i18n-routing-gor') | ||
|
||
/** | ||
* Register global i18n routing option registory | ||
* | ||
* @param router - A router instance, about router type | ||
* @param options - A global options, about options type, see {@link I18nRoutingGlobalOptions} | ||
*/ | ||
export function registerGlobalOptions<Context = unknown>( | ||
router: Router, | ||
options: I18nRoutingGlobalOptions<Context> | ||
): void { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const _options: I18nRoutingGlobalOptions | undefined = (router as any)[GlobalOptionsRegistry] | ||
if (_options) { | ||
warn('already registered global options') | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
;(router as any)[GlobalOptionsRegistry] = options | ||
} | ||
} | ||
|
||
/** | ||
* Get global i18n routing options | ||
* | ||
* @param router - A router instance, about router type | ||
* | ||
* @returns - {@link I18nRoutingGlobalOptions | global options} from i18n routing options registory, if registered, return it, else empty object | ||
*/ | ||
export function getGlobalOptions(router: Router): I18nRoutingGlobalOptions { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (router as any)[GlobalOptionsRegistry] ?? {} | ||
} | ||
|
||
export function createLocaleFromRouteGetter( | ||
localeCodes: string[], | ||
routesNameSeparator: string, | ||
defaultLocaleRouteNameSuffix: string | ||
) { | ||
const localesPattern = `(${localeCodes.join('|')})` | ||
const defaultSuffixPattern = `(?:${routesNameSeparator}${defaultLocaleRouteNameSuffix})?` | ||
const regexpName = new RegExp(`${routesNameSeparator}${localesPattern}${defaultSuffixPattern}$`, 'i') | ||
const regexpPath = getLocalesRegex(localeCodes) | ||
|
||
/** | ||
* extract locale code from given route: | ||
* - if route has a name, try to extract locale from it | ||
* - otherwise, fall back to using the routes'path | ||
*/ | ||
const getLocaleFromRoute = (route: RouteLocationNormalizedLoaded | RouteLocationNormalized | string): string => { | ||
// extract from route name | ||
if (isObject(route)) { | ||
if (route.name) { | ||
const name = isString(route.name) ? route.name : route.name.toString() | ||
const matches = name.match(regexpName) | ||
if (matches && matches.length > 1) { | ||
return matches[1] | ||
} | ||
} else if (route.path) { | ||
// Extract from path | ||
const matches = route.path.match(regexpPath) | ||
if (matches && matches.length > 1) { | ||
return matches[1] | ||
} | ||
} | ||
} else if (isString(route)) { | ||
const matches = route.match(regexpPath) | ||
if (matches && matches.length > 1) { | ||
return matches[1] | ||
} | ||
} | ||
|
||
return '' | ||
} | ||
|
||
return getLocaleFromRoute | ||
} |
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
Oops, something went wrong.