Skip to content

Commit

Permalink
feat: allow generatePermalink to work for unlocalized content (fix #195
Browse files Browse the repository at this point in the history
…) (#196)

fix: allow generatePermalink to work for unlocalized content (fix #195)
  • Loading branch information
greatislander authored Aug 3, 2023
1 parent 9b3f812 commit f1d2376
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
};
Expand All @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = {
},
supportedLanguages: languages,
defaultLanguage: "en",
i18n: true,
localesDirectory: `./${eleventyConfig.dir.input || "src"}/_locales`,
templateFormats: [
"html",
Expand All @@ -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);

Expand Down
13 changes: 6 additions & 7 deletions src/utils/generate-permalink.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit f1d2376

Please sign in to comment.