Commit 513e253e authored by Zach Leatherman's avatar Zach Leatherman
Browse files

Adds `page.lang` when using i18n plugin! Fixes #2501 prerequisite of #243 eleventy-base-blog

parent 3d8dda10
......@@ -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 (
......
......@@ -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`
);
});
......@@ -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}`;
};
......@@ -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
......@@ -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
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment