Skip to content

Commit

Permalink
multilanguage plugin improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 19, 2023
1 parent c798f4b commit 9a10f33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@

- New option `pageSubExtension`.
- Changed `extensions` option type to `string[]`.

## `multilanguage` Plugin

- Apply the default language to all pages without defined language.
18 changes: 17 additions & 1 deletion plugins/multilanguage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Page } from "../core/filesystem.ts";
import { isPlainObject, merge } from "../core/utils.ts";
import { isPlainObject, log, merge } from "../core/utils.ts";
import { posix } from "../deps/path.ts";
import { getUrl } from "../core/source.ts";

Expand Down Expand Up @@ -34,6 +34,22 @@ export default function multilanguage(userOptions?: Partial<Options>): Plugin {
const { data } = page;
const languages = data.lang as string | string[] | undefined;

// If the "lang" variable is not defined, use the default language
if (languages === undefined) {
data.lang = options.defaultLanguage;
return;
}

// If the "lang" variable is a string, check if it's a valid language
if (typeof languages === "string") {
if (!options.languages.includes(languages)) {
log.warning(
`[multilanguage plugin] The language "${languages}" in the page ${page.sourcePath} is not defined in the "languages" option.`,
);
}
return;
}

// The "lang" variable of the pages must be an array
if (!Array.isArray(languages)) {
return;
Expand Down

0 comments on commit 9a10f33

Please sign in to comment.