-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathi18n-locale-plugin.ts
64 lines (54 loc) · 1.82 KB
/
i18n-locale-plugin.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import type { Plugin } from 'vite';
/**
* The base module location used to search for locale specific data.
*/
export const LOCALE_DATA_BASE_MODULE = '@angular/common/locales/global';
/**
* Creates a Vite plugin that resolves Angular locale data files from `@angular/common`.
*
* @returns A Vite plugin.
*/
export function createAngularLocaleDataPlugin(): Plugin {
return {
name: 'angular-locale-data',
enforce: 'pre',
async resolveId(source) {
if (!source.startsWith('angular:locale/data:')) {
return;
}
// Extract the locale from the path
const originalLocale = source.split(':', 3)[2];
// Remove any private subtags since these will never match
let partialLocale = originalLocale.replace(/-x(-[a-zA-Z0-9]{1,8})+$/, '');
let exact = true;
while (partialLocale) {
const potentialPath = `${LOCALE_DATA_BASE_MODULE}/${partialLocale}`;
const result = await this.resolve(potentialPath);
if (result) {
if (!exact) {
this.warn(
`Locale data for '${originalLocale}' cannot be found. Using locale data for '${partialLocale}'.`,
);
}
return result;
}
// Remove the last subtag and try again with a less specific locale
const parts = partialLocale.split('-');
partialLocale = parts.slice(0, -1).join('-');
exact = false;
// The locales "en" and "en-US" are considered exact to retain existing behavior
if (originalLocale === 'en-US' && partialLocale === 'en') {
exact = true;
}
}
return null;
},
};
}