@@ -4,15 +4,20 @@ const defaultLang = "en"
4
4
// Only allow languages defined in the LOCALES env variable.
5
5
// This allows us to compile only languages that are "complete" or test only
6
6
// a single language
7
- function getLanguages ( ) {
7
+ function getLanguages ( localeStr ) {
8
8
// If `LOCALES` isn't defined, only have default language (English)
9
- if ( ! process . env . LOCALES ) {
9
+ if ( ! localeStr ) {
10
10
return [ ]
11
11
}
12
12
13
- const langCodes = process . env . LOCALES . split ( " " )
13
+ const langCodes = localeStr . split ( " " )
14
14
const langs = [ ]
15
15
for ( let code of langCodes ) {
16
+ if ( code === defaultLang ) {
17
+ throw new Error (
18
+ `${ code } is the default locale and should not be put in the locale list.`
19
+ )
20
+ }
16
21
const lang = allLangs . find ( lang => lang . code === code )
17
22
// Error if one of the locales provided isn't a valid locale
18
23
if ( ! lang ) {
@@ -25,7 +30,7 @@ function getLanguages() {
25
30
return langs
26
31
}
27
32
28
- const langs = getLanguages ( )
33
+ const langs = getLanguages ( process . env . LOCALES )
29
34
const langCodes = langs . map ( lang => lang . code )
30
35
31
36
function isDefaultLang ( locale ) {
@@ -34,12 +39,10 @@ function isDefaultLang(locale) {
34
39
35
40
/**
36
41
* Get the path prefixed with the locale
37
- * @param {* } locale the locale to prefix with
38
- * @param {* } path the path to prefix
42
+ * @param {string } locale the locale to prefix with
43
+ * @param {string } path the path to prefix
39
44
*/
40
45
function localizedPath ( locale , path ) {
41
- const isIndex = path === `/`
42
-
43
46
// Our default language isn't prefixed for back-compat
44
47
if ( isDefaultLang ( locale ) ) {
45
48
return path
@@ -50,25 +53,25 @@ function localizedPath(locale, path) {
50
53
// If for whatever reason we receive an already localized path
51
54
// (e.g. if the path was made with location.pathname)
52
55
// just return it as-is.
53
- if ( langCodes . includes ( base ) ) {
56
+ if ( base === locale ) {
54
57
return path
55
58
}
56
59
57
- // If it's another language, add the "path"
58
- // However, if the homepage/index page is linked don't add the "to"
59
- // Because otherwise this would add a trailing slash
60
- return `${ locale } ${ isIndex ? `` : `${ path } ` } `
60
+ // If it's another language, prefix with the locale
61
+ return `/${ locale } ${ path } `
61
62
}
62
63
63
64
/**
64
65
* Split a path into its locale and base path.
65
66
*
66
67
* e.g. /es/tutorial/ -> { locale: "es", basePath: "/tutorial/"}
67
68
* @param {string } path the path to extract locale information from
69
+ * @param {Array } langCodes a list of valid language codes, defaulting to
70
+ * the ones defined in `process.env.LOCALES`
68
71
*/
69
- function getLocaleAndBasePath ( path ) {
72
+ function getLocaleAndBasePath ( path , codes = langCodes ) {
70
73
const [ , code , ...rest ] = path . split ( "/" )
71
- if ( langCodes . includes ( code ) ) {
74
+ if ( codes . includes ( code ) ) {
72
75
return { locale : code , basePath : `/${ rest . join ( "/" ) } ` }
73
76
}
74
77
return { locale : defaultLang , basePath : path }
@@ -78,6 +81,7 @@ module.exports = {
78
81
langCodes,
79
82
langs,
80
83
defaultLang,
84
+ getLanguages,
81
85
localizedPath,
82
86
getLocaleAndBasePath,
83
87
}
0 commit comments