diff --git a/README.md b/README.md index 8688a62..8da973e 100644 --- a/README.md +++ b/README.md @@ -323,7 +323,7 @@ module.exports = { locale: data => data.lang, permalink: data => { const locale = data.locale; - return generatePermalink(data, "posts", _(locale, "posts")); + return generatePermalink(data, "pages", _(locale, "pages"), _(locale, "page")); } } }; @@ -349,6 +349,26 @@ module.exports = () => { This helper is a wrapper for [`i18n.enhance11tydata`](https://github.com/sgissinger/eleventy-plugin-i18n-gettext#i18nenhance11tydataobj-locale-dir). +#### Disabling String Translation + +If you don't need string translation features in your project, you can disable string translation by setting the `i18n` +option to false when registering `eleventy-plugin-fluid` in your config: + +```diff +const fluidPlugin = require("eleventy-plugin-fluid"); + +module.exports = function (config) { +- config.addPlugin(fluidPlugin); ++ config.addPlugin(fluidPlugin, { ++ i18n: false ++ }); +}; +``` + +Note that if you do this, you will need to remove any uses of the `localizeData` helper or +[`eleventy-plugin-i18n-gettext` functions](https://github.com/sgissinger/eleventy-plugin-i18n-gettext#api) in your +project. + #### Additional Reference For additional information on setting up localization/internationalization, see: diff --git a/index.js b/index.js index 20558cc..45e380f 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,7 @@ module.exports = { }, supportedLanguages: languages, defaultLanguage: "en", + i18n: true, localesDirectory: `./${eleventyConfig.dir.input || "src"}/_locales`, templateFormats: [ "html", @@ -94,9 +95,11 @@ module.exports = { eleventyConfig.addPlugin(EleventyI18nPlugin, { defaultLanguage: options.defaultLanguage }); - eleventyConfig.addPlugin(i18n, { - localesDirectory: options.localesDirectory - }); + if (options.i18n) { + eleventyConfig.addPlugin(i18n, { + localesDirectory: options.localesDirectory + }); + } eleventyConfig.addPlugin(EleventyRenderPlugin); eleventyConfig.addPlugin(pluginWebc); diff --git a/src/utils/generate-permalink.js b/src/utils/generate-permalink.js index 27e6079..caaf194 100644 --- a/src/utils/generate-permalink.js +++ b/src/utils/generate-permalink.js @@ -1,44 +1,43 @@ "use strict"; -const { EleventyI18nPlugin } = require("@11ty/eleventy"); -const i18n = require("eleventy-plugin-i18n-gettext"); const TemplateConfig = require("@11ty/eleventy/src/TemplateConfig.js"); /** * @param {Object} data - The data object for the current collection item. * @param {String} collectionType - The collection type. * @param {String} collectionSlug - A localized, URL-safe slug for the collection type, used in the generated permalink. + * @param {String} paginationSlug - A localized, URL-safe slug for paginated URLs such as /page/2, used in the generated permalink. * * @return {String} - The generated permalink. */ -const generatePermalink = (data, collectionType, collectionSlug) => { +const generatePermalink = (data, collectionType, collectionSlug, paginationSlug = "pages") => { /* If this post is a "stub" with no localized title, we assume it does not exist and prevent it from building. */ if (!data.hasOwnProperty("title")) { return false; } const eleventyConfig = new TemplateConfig(); - const lang = EleventyI18nPlugin.LangUtils.getLanguageCodeFromInputPath(data.page.inputPath); - const locale = lang; + const lang = data.lang || data.defaultLanguage; const langSlug = data.supportedLanguages[lang].slug || lang; collectionSlug = collectionSlug || collectionType; const slugify = eleventyConfig.userConfig.getFilter("slugify"); if (collectionType === "pages") { + /* If the page is a 404 page, return 404.html, optionally prepended with the language code. */ if (data.page.fileSlug === "404") { return (lang === data.defaultLanguage) ? "/404.html" : `/${langSlug}/404.html`; } /** If the page is the index page, the base path, optionally prepended with the language code. */ - if (data.page.fileSlug === lang) { + if (data.page.fileSlug === lang || data.page.inputPath.endsWith("index.md")) { return (lang === data.defaultLanguage) ? "/" : `/${langSlug}/`; } /* If the page is not the index page, return the page title in a URL-safe format, optionally prepended with the language code. */ const slug = slugify(data.title); if (data.hasOwnProperty("pagination") && data.pagination.pageNumber > 0) { - return (lang === data.defaultLanguage) ? `/${slug}/${i18n._(locale, "page")}/${data.pagination.pageNumber + 1}/` : `/${langSlug}/${slug}/${i18n._(locale, "page")}/${data.pagination.pageNumber + 1}/`; + return (lang === data.defaultLanguage) ? `/${slug}/${paginationSlug}/${data.pagination.pageNumber + 1}/` : `/${langSlug}/${slug}/${paginationSlug}/${data.pagination.pageNumber + 1}/`; } return (lang === data.defaultLanguage) ? `/${slug}/` : `/${langSlug}/${slug}/`; } else {