-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathfallbacker.ts
171 lines (157 loc) · 4.19 KB
/
fallbacker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
isString,
isArray,
isBoolean,
isPlainObject,
isObject
} from '@intlify/shared'
import { DEFAULT_LOCALE } from './context'
import type { Locale, FallbackLocale } from './runtime'
import type { CoreContext, CoreInternalContext } from './context'
/**
* The locale fallbacker
*
* @VueI18nGeneral
*/
export type LocaleFallbacker = <Message = string>(
ctx: CoreContext<Message>,
fallback: FallbackLocale,
start: Locale
) => Locale[]
/**
* Fallback with simple implemenation
*
* @remarks
* A fallback locale function implemented with a simple fallback algorithm.
*
* Basically, it returns the value as specified in the `fallbackLocale` props, and is processed with the fallback inside intlify.
*
* @param ctx - A {@link CoreContext | context}
* @param fallback - A {@link FallbackLocale | fallback locale}
* @param start - A starting {@link Locale | locale}
*
* @returns Fallback locales
*
* @VueI18nGeneral
*/
export function fallbackWithSimple<Message = string>(
ctx: CoreContext<Message>,
fallback: FallbackLocale,
start: Locale // eslint-disable-line @typescript-eslint/no-unused-vars
): Locale[] {
// prettier-ignore
return [...new Set(
[
start,
...(isArray(fallback)
? fallback
: isObject(fallback)
? Object.keys(fallback)
: isString(fallback)
? [fallback]
: [start])
]
)]
}
/**
* Fallback with locale chain
*
* @remarks
* A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
*
* @param ctx - A {@link CoreContext | context}
* @param fallback - A {@link FallbackLocale | fallback locale}
* @param start - A starting {@link Locale | locale}
*
* @returns Fallback locales
*
* @VueI18nSee [Fallbacking](../guide/essentials/fallback)
*
* @VueI18nGeneral
*/
export function fallbackWithLocaleChain<Message = string>(
ctx: CoreContext<Message>,
fallback: FallbackLocale,
start: Locale
): Locale[] {
const startLocale = isString(start) ? start : DEFAULT_LOCALE
const context = ctx as unknown as CoreInternalContext
if (!context.__localeChainCache) {
context.__localeChainCache = new Map()
}
let chain = context.__localeChainCache.get(startLocale)
if (!chain) {
chain = []
// first block defined by start
let block: unknown = [start]
// while any intervening block found
while (isArray(block)) {
block = appendBlockToChain(chain, block, fallback)
}
// prettier-ignore
// last block defined by default
const defaults = isArray(fallback) || !isPlainObject(fallback)
? fallback
: fallback['default']
? fallback['default']
: null
// convert defaults to array
block = isString(defaults) ? [defaults] : defaults
if (isArray(block)) {
appendBlockToChain(chain, block, false)
}
context.__localeChainCache.set(startLocale, chain)
}
return chain
}
function appendBlockToChain(
chain: Locale[],
block: Locale[],
blocks: FallbackLocale
): unknown {
let follow: unknown = true
for (let i = 0; i < block.length && isBoolean(follow); i++) {
const locale = block[i]
if (isString(locale)) {
follow = appendLocaleToChain(chain, block[i], blocks)
}
}
return follow
}
function appendLocaleToChain(
chain: Locale[],
locale: Locale,
blocks: FallbackLocale
): unknown {
let follow: unknown
const tokens = locale.split('-')
do {
const target = tokens.join('-')
follow = appendItemToChain(chain, target, blocks)
tokens.splice(-1, 1)
} while (tokens.length && follow === true)
return follow
}
function appendItemToChain(
chain: Locale[],
target: Locale,
blocks: FallbackLocale
): unknown {
let follow: unknown = false
if (!chain.includes(target)) {
follow = true
if (target) {
follow = target[target.length - 1] !== '!'
const locale = target.replace(/!/g, '')
chain.push(locale)
if (
(isArray(blocks) || isPlainObject(blocks)) &&
(blocks as any)[locale] // eslint-disable-line @typescript-eslint/no-explicit-any
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
follow = (blocks as any)[locale]
}
}
}
return follow
}