Skip to content

Conversation

@maekawataiki
Copy link

@maekawataiki maekawataiki commented Mar 27, 2025

Description

This PR addresses problem of broken link/image path due to moving default language to top level. (/en/ to /)

This PR implementation introduces force_default_in_subdirectory config option, which let user to decide moving default language to top level or subdirectory with backward compatibility.

However, this change introduces new configuration option, so I would like to discuss best configuration option.

plugins:
  - i18n:
    force_default_in_subdirectory: true

Other alternative implementation option:

Alternative Configuration 1: "Move files to the path if link config is set to language"

Currently, link generated for default language is broken (404 error) if link is set to path other than / (i.e. /en/) since file is moved to top level.
Allowing moving path to link is more intuitive but may introduce change to existing user using link option configuration.

languages:
  - locale: en
    default: true
    link: `/en/`    # Currently link generated is broken since file is moved to top level.

Consideration

Handling contents without locale info

This PR implementation have discrepancy between folder configuration and suffix configuration.

For folder configuration, files not under lang folder does not belong to specific language and path is not changed.

docs
|- asset/*  # do not move
|- en/*   # default: move to top level `/` or `/en/` with `force_default_in_subdirectory` option
|- fr/*

But in suffix configuration, file without suffix is currently treated as default language, so files is moved too.

docs
|- *.png,jpg,md    # default: move to top level `/` or `/en/` with `force_default_in_subdirectory` option
|- *.en.md  # default: move to top level `/` or `/en/` with `force_default_in_subdirectory` option
|- *.fr.md

Current design of plugin and option introduced by this PR may create discrepancy between folder and suffix configuration.

Top level path redirection

I am developing my own site with this branch with index.html which redirects to default language page, but this can possibly done in plugin.

# folder structure
docs
|- en/*
|- fr/*
|- index.html
# index.html
<!doctype html>
<html>
  <head>
    <meta
      http-equiv="refresh"
      content="0; url=/en/" />
    <link rel="canonical" href="/en/" />
    <title>Redirecting...</title>
  </head>
  <body>
    <p>
      If you are not redirected automatically, please
      <a href="/en/">click here</a>.
    </p>
  </body>
</html>

Related Issue

#306

@abwuge
Copy link

abwuge commented Jun 17, 2025

This branch is great!

I would like to use this feature to redirect users to their preferred language first, rather than defaulting to my choice.

Here is my index.html, which might be help.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Redirecting ...</title>

    <!-- Redirect to ./en/ after 3s (Which usually happens on that JavaScript is disabled) -->
    <meta http-equiv="refresh" content="3;url=./en/" />
  </head>
  <body>
    <script>
      (function () {
        const LANGUAGE_MAP = {
          zh: "./zh/",
          en: "./en/",
        };

        const DEFAULT_LANGUAGE = "./en/";

        const userLang = (
          navigator.language ||
          navigator.userLanguage ||
          navigator.browserLanguage
        ).toLowerCase();

        window.location.href =
          LANGUAGE_MAP[userLang] ||
          LANGUAGE_MAP[userLang.split("-")[0]] ||
          DEFAULT_LANGUAGE;
      })();
    </script>
  </body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants