Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin): fix handling of files with dotted suffixes, thx to @gnaegi #259

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mkdocs_static_i18n/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ def create_i18n_file(

file_dest_path = Path(file.dest_path)
file_locale = default_language
file_localization = None

for language in all_languages:
if is_relative_to(file.src_path, language):
file_locale = language
file_localization = language
break
else:
# maybe the language folder is present BUT not configured in the plugin.languages yet
# so we use the locale regex to guess that it's a localization folder
maybe_file_locale = PurePath(file.src_path).parts[0]
if RE_LOCALE.match(maybe_file_locale):
file_locale = maybe_file_locale
file_localization = maybe_file_locale
break

# README.html should be renamed to index.html
Expand Down Expand Up @@ -83,6 +86,7 @@ def create_i18n_file(
file.alternates = {current_language: file}
file.locale = file_locale
file.locale_alternate_of = current_language
file.localization = file_localization

log.debug(f"reconfigure {file} from locale {file_locale}")

Expand Down
1 change: 1 addition & 0 deletions mkdocs_static_i18n/reconfigure.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ def reconfigure_files(
# files when multiple languages are configured
if (
len(self.all_languages) > 1
and i18n_file.localization is not None
and i18n_dest_uris[i18n_file.dest_uri].locale == i18n_file.locale
):
raise Exception(
Expand Down
6 changes: 5 additions & 1 deletion mkdocs_static_i18n/suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def create_i18n_file(

file_dest_path = Path(file.dest_path)
file_locale = default_language
file_localization = None
locale_site_dir = current_language if current_language != default_language else ""

try:
Expand All @@ -47,12 +48,14 @@ def create_i18n_file(
file_dest_path = Path(f"{file_dest_path_no_suffix}{file_dest_path.suffix}")
# file name suffixed by locale, remove it
file.name = Path(file.name).stem
# store file localization
file_localization = file_locale
else:
file_locale = default_language
except IndexError:
pass

if config.use_directory_urls:
if config.use_directory_urls and file_localization:
if file_dest_path.parent != Path("."):
parent_dest_path_no_suffix = file_dest_path.parent.with_suffix("")
file_dest_path = parent_dest_path_no_suffix / file_dest_path.name
Expand Down Expand Up @@ -88,6 +91,7 @@ def create_i18n_file(
file.alternates = {current_language: file}
file.locale = file_locale
file.locale_alternate_of = current_language
file.localization = file_localization

log.debug(f"reconfigure {file} from locale {file_locale}")

Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def _make_config(mkdocs_fp=None, docs_dir=None, use_directory_urls=True, plugins

@pytest.fixture
def make_localized_config(make_config):
created_configs = []

def _make_localized_config(config, locale):
config.plugins["i18n"].current_language = locale
#
Expand All @@ -46,6 +48,11 @@ def _make_localized_config(config, locale):
nav = get_navigation(files, config)
nav = config.plugins.run_event("nav", nav, files=files, config=config)
#
created_configs.append(config)
#
return config, files, env, nav

yield _make_localized_config

for config in created_configs:
utils.clean_directory(config.site_dir)
Empty file.
Empty file.
Empty file.
116 changes: 116 additions & 0 deletions tests/docs_folder_structure_two_languages/en/topic2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Topic 2 (english version + french version)

!!! tip
**Use the language switcher in the header** to switch between the localized versions of this demo website. This switcher is part of [mkdocs-material >= 7.1.0](https://squidfunk.github.io/mkdocs-material/setup/changing-the-language/#site-language-selector) and is **automatically configured by this plugin** or can be statically configured from the [mkdocs.yml file](https://github.com/ultrabug/mkdocs-static-i18n/blob/main/mkdocs.yml).

## What you are seeing now

This page source file and media content have been localized after applying
the [localized build logic](#localized-build-logic) described below. Here is a
quick recap of the files used as source and the generated build structure of
what you see:

```
docs
├── image.en.png <-- this image file is used on the /en version
├── image.fr.png <-- this image file is used on the /fr version
├── index.fr.md
├── index.md
├── topic1
│   ├── named_file.en.md
│   └── named_file.fr.md
└── topic2
├── index.en.md <-- this file is used for both /en and /fr versions
└── index.md
```

```
site
├── en
│   ├── image.png <-- you see this image on the /en version
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html <-- you are here on the /en version
├── fr
│   ├── image.png <-- you see this image on the /fr version
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html <-- you are here on the /fr version
├── image.png
├── index.html
├── topic1
│   └── named_file
│   └── index.html
└── topic2
└── index.html
```

## Automatic media / link / asset localization

![localized image](../image.png)

This image source is dynamically localized while still being referenced in the
markdown source of the page as `![localized image](../image.png)`. This means that
this plugin allows you to not worry about links, media and static content file
names, just use their simple name and concentrate on your content translation!

---

## Localized build logic

The settings used to build this site is:

```
plugins:
- i18n:
default_language: en
languages:
en: english
fr: français
```

Meaning that we will get three versions of our website:

1. the `default_language` version which will use any `.md` documentation file first and fallback to any `.en.md` file found since `en` is the default language
2. the `/en` language version which will use any `.en.md` documentation file first and fallback to any `.md` file found
3. the `/fr` language version which will use any `.fr.md` documentation file first and fallback to either `.en.md` file (default language) or `.md` file (default language fallback) whichever comes first

Given that logic, the following `site` structure is built:

```
site
├── 404.html
├── assets
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── en
│   ├── image.png
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html
├── fr
│   ├── image.png
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html
├── image.png
├── index.html
├── topic1
│   └── named_file
│   └── index.html
└── topic2
└── index.html
```
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# English default fallback

This is the english version of a page that will show up on other languages as a fallback.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This page only exists on the French version

Et voilà !
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions tests/docs_suffix_structure_one_language/index.fr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Page d'accueil (french version)

!!! tip "Astuce"
**Utilisez le sélecteur de langue dans la barre de titre** pour passer d'une version localisée à l'autre de ce site de démonstration. Ce sélecteur fait partie de [mkdocs-material >= 7.1.0](https://squidfunk.github.io/mkdocs-material/setup/changing-the-language/#site-language-selector) et est **configuré automatiquement par ce plugin** ou peut être défini de manière statique depuis le [fichier mkdocs.yml](https://github.com/ultrabug/mkdocs-static-i18n/blob/main/mkdocs.yml).

## Ce que vous voyez en ce moment

La source de cette page et son contenu média ont été localisés en suivant la
[logique de localisation](#logique-de-localisation) décrite ci-dessous. Voici
un rapide récapitulatif des fichiers utilisés comme source et de la structure
générée que vous voyez en ce moment:

```
docs
├── image.en.png
├── image.fr.png <-- this image file is used on the /fr version
├── index.fr.md <-- this file is used on the /fr version
├── index.md
├── topic1
│   ├── named_file.en.md
│   └── named_file.fr.md
└── topic2
├── index.en.md
└── index.md
```

```
site
├── en
│   ├── image.png
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html
├── fr
│   ├── image.png <-- you see this image on the /fr version
│   ├── index.html <-- you are here on the /fr version
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html
├── image.png
├── index.html
├── topic1
│   └── named_file
│   └── index.html
└── topic2
└── index.html
```

## Localisation automatique des médias / liens / assets

![localized image](image.png)

La source de cette image est dynamiquement localisée bien qu'elle soit
référencée dans la source du markdown par `![localized image](image.png)`.
Cela démontre que ce plugin vous permet de ne pas vous préoccuper du nom
des fichiers dans vos liens, médias et contenus statiques : utilisez leurs
noms sans extension localisée et concentrez-vous sur la traduction de vos
contenus !

Bien entendu, les images peuvent aussi ne pas être localisées comme l'image
ci-dessous qui est utilisée par toutes les versions :

![non localized image](assets/image_non_localized.png)

---

## Logique de localisation

Ce site est construit avec cette configuration :

```
plugins:
- i18n:
default_language: en
languages:
en: english
fr: français
```

Ce qui veut dire que nous obtiendrons trois versions de notre site :

1. la version `default_language` qui utilisera en premier les fichiers dont
l'extension est `.md` et prendra quelconque fichier en `.en.md` en repli
puisque la version `en` est configurée comme la langue par défaut
2. la version localisée `/en` qui utilisera les fichiers dont l'extension est
`.en.md` en premier et utilisera leur version `.md` en repli si elle existe
3. la version localisée `/fr` qui utilisera les fichiers dont l'extension est
`.fr.md` en premier et utilisera en repli soit la version `.en.md` (dérivée
du langage par défaut) ou `.md`

En suivant cette logique, la structure `site` générée est :

```
site
├── 404.html
├── assets
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── en
│   ├── image.png
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html
├── fr
│   ├── image.png
│   ├── index.html
│   ├── topic1
│   │   └── named_file
│   │   └── index.html
│   └── topic2
│   └── index.html
├── image.png
├── index.html
├── topic1
│   └── named_file
│   └── index.html
└── topic2
└── index.html
```
Loading