Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fork
G
github
11ty
eleventy
Commits
513e253e
Commit
513e253e
authored
2 years ago
by
Zach Leatherman
Browse files
Options
Download
Email Patches
Plain Diff
Adds `page.lang` when using i18n plugin! Fixes #2501 prerequisite of #243 eleventy-base-blog
parent
3d8dda10
master
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
36 additions
and
16 deletions
+36
-16
src/Plugins/I18nPlugin.js
src/Plugins/I18nPlugin.js
+20
-8
test/I18nPluginTest.js
test/I18nPluginTest.js
+8
-4
test/stubs-i18n/en-us/index.11ty.js
test/stubs-i18n/en-us/index.11ty.js
+2
-1
test/stubs-i18n/en/index.liquid
test/stubs-i18n/en/index.liquid
+2
-1
test/stubs-i18n/es/index.njk
test/stubs-i18n/es/index.njk
+2
-1
test/stubs-i18n/non-lang-file.njk
test/stubs-i18n/non-lang-file.njk
+2
-1
No files found.
src/Plugins/I18nPlugin.js
View file @
513e253e
...
...
@@ -11,6 +11,11 @@ const bcp47Normalize = require("bcp-47-normalize");
const
iso639
=
require
(
"
iso-639-1
"
);
class
LangUtils
{
static
getLanguageCodeFromUrl
(
url
)
{
let
s
=
(
url
||
""
).
split
(
"
/
"
);
return
s
.
length
>
0
&&
Comparator
.
isLangCode
(
s
[
1
])
?
s
[
1
]
:
""
;
}
static
swapLanguageCode
(
str
,
langCode
)
{
if
(
!
Comparator
.
isLangCode
(
langCode
))
{
return
str
;
...
...
@@ -197,20 +202,27 @@ function EleventyPlugin(eleventyConfig, opts = {}) {
}
);
eleventyConfig
.
addGlobalData
(
"
eleventyComputed.page.lang
"
,
()
=>
{
// if addGlobalData receives a function it will execute it immediately,
// so we return a nested function for computed data
return
(
data
)
=>
{
return
(
LangUtils
.
getLanguageCodeFromUrl
(
data
.
page
.
url
)
||
options
.
defaultLanguage
);
};
});
// Normalize a theoretical URL based on the current page’s language
// If a non-localized file exists, returns the URL without a language assigned
// Fails if no file exists (localized and not localized)
eleventyConfig
.
addFilter
(
options
.
filters
.
url
,
function
(
url
,
langCodeOverride
)
{
let
langCode
=
langCodeOverride
;
if
(
!
langCode
)
{
let
pageUrl
=
getPageInFilter
(
this
)?.
url
;
let
s
=
pageUrl
.
split
(
"
/
"
);
langCode
=
(
s
.
length
>
0
&&
Comparator
.
isLangCode
(
s
[
1
])
?
s
[
1
]
:
""
)
||
options
.
defaultLanguage
;
}
let
langCode
=
langCodeOverride
||
LangUtils
.
getLanguageCodeFromUrl
(
getPageInFilter
(
this
)?.
url
)
||
options
.
defaultLanguage
;
// Already has a language code on it and has a relevant url with the target language code
if
(
...
...
This diff is collapsed.
Click to expand it.
test/I18nPluginTest.js
View file @
513e253e
...
...
@@ -148,7 +148,8 @@ test("locale_url and locale_links Filters", async (t) => {
/en-us/
/non-lang-file/
[]
[]`
[]
en`
);
t
.
is
(
...
...
@@ -159,7 +160,8 @@ test("locale_url and locale_links Filters", async (t) => {
/en-us/
/non-lang-file/
[{"url":"/en/","lang":"en","label":"English"},{"url":"/en-us/","lang":"en-us","label":"English"}]
[{"url":"/en/","lang":"en","label":"English"},{"url":"/en-us/","lang":"en-us","label":"English"}]`
[{"url":"/en/","lang":"en","label":"English"},{"url":"/en-us/","lang":"en-us","label":"English"}]
es`
);
t
.
is
(
...
...
@@ -170,7 +172,8 @@ test("locale_url and locale_links Filters", async (t) => {
/en-us/
/non-lang-file/
[{"url":"/en-us/","lang":"en-us","label":"English"},{"url":"/es/","lang":"es","label":"Español"}]
[{"url":"/en-us/","lang":"en-us","label":"English"},{"url":"/es/","lang":"es","label":"Español"}]`
[{"url":"/en-us/","lang":"en-us","label":"English"},{"url":"/es/","lang":"es","label":"Español"}]
en`
);
t
.
is
(
...
...
@@ -181,6 +184,7 @@ test("locale_url and locale_links Filters", async (t) => {
/es/
/non-lang-file/
[{"url":"/en/","lang":"en","label":"English"},{"url":"/es/","lang":"es","label":"Español"}]
[{"url":"/en/","lang":"en","label":"English"},{"url":"/es/","lang":"es","label":"Español"}]`
[{"url":"/en/","lang":"en","label":"English"},{"url":"/es/","lang":"es","label":"Español"}]
en-us`
);
});
This diff is collapsed.
Click to expand it.
test/stubs-i18n/en-us/index.11ty.js
View file @
513e253e
...
...
@@ -5,5 +5,6 @@ ${this.locale_url("/es/")}
${
this
.
locale_url
(
"
/
"
,
"
es
"
)}
${
this
.
locale_url
(
"
/non-lang-file/
"
)}
${
JSON
.
stringify
(
this
.
locale_links
(
data
.
page
.
url
).
sort
())}
${
JSON
.
stringify
(
this
.
locale_links
().
sort
())}
`
;
${
JSON
.
stringify
(
this
.
locale_links
().
sort
())}
${
data
.
page
.
lang
}
`
;
};
This diff is collapsed.
Click to expand it.
test/stubs-i18n/en/index.liquid
View file @
513e253e
...
...
@@ -4,4 +4,5 @@
{{
"/"
|
locale_url
:
"en-us"
}}
{{
"/non-lang-file/"
|
locale_url
}}
{{
page
.
url
|
locale_links
|
json
}}
{{
""
|
locale_links
|
json
}}
\ No newline at end of file
{{
""
|
locale_links
|
json
}}
{{
page
.
lang
}}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/stubs-i18n/es/index.njk
View file @
513e253e
...
...
@@ -4,4 +4,5 @@
{{ "/" | locale_url("en-us") }}
{{ "/non-lang-file/" | locale_url }}
{{ page.url | locale_links | dump | safe }}
{{ "" | locale_links | dump | safe }}
\ No newline at end of file
{{ "" | locale_links | dump | safe }}
{{ page.lang }}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/stubs-i18n/non-lang-file.njk
View file @
513e253e
...
...
@@ -2,4 +2,5 @@
{{ "/" | locale_url("en-us") }}
{{ "/non-lang-file/" | locale_url }}
{{ page.url | locale_links | dump | safe }}
{{ "" | locale_links | dump | safe }}
\ No newline at end of file
{{ "" | locale_links | dump | safe }}
{{ page.lang }}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment