Skip to content

Commit

Permalink
fix(assets): non localized default assets should not be copied to des…
Browse files Browse the repository at this point in the history
…t languages
  • Loading branch information
ultrabug committed Aug 22, 2023
1 parent fb4bbf1 commit c2215b2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.
105 changes: 69 additions & 36 deletions mkdocs_static_i18n/reconfigure.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def reconfigure_files(
i18n_files = I18nFiles(self, [])
i18n_alternate_dest_uris = defaultdict(list)
for file in files:
# documentation files
# user provided files in docs_dir
if is_relative_to(file.abs_src_path, mkdocs_config.docs_dir):
i18n_file = create_i18n_file(
file,
Expand All @@ -463,50 +463,83 @@ def reconfigure_files(
mkdocs_config,
)

# never seen that file?
if i18n_file.dest_uri not in i18n_dest_uris:
# best case scenario
# use the file since its locale is our current build language
if i18n_file.locale == self.current_language:
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use {i18n_file.locale} {i18n_file}")
# if locale is the default language AND default language fallback is enabled
# we are using a file that is not really our locale
elif (
self.config.fallback_to_default is True
and i18n_file.locale == self.default_language
) or i18n_file.src_uri.startswith("assets/"):
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use default {i18n_file.locale} {i18n_file}")
if not i18n_file.src_uri.startswith("assets/"):
# user provided documentation page
if i18n_file.is_documentation_page():
# never seen that file?
if i18n_file.dest_uri not in i18n_dest_uris:
# best case scenario
# use the file since its locale is our current build language
if i18n_file.locale == self.current_language:
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use {i18n_file.locale} {i18n_file}")
# if locale is the default language AND default language fallback is enabled
# we are using a file that is not really our locale
elif (
self.config.fallback_to_default is True
and i18n_file.locale == self.default_language
):
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use default {i18n_file.locale} {i18n_file}")
i18n_alternate_dest_uris[i18n_file.dest_uri].append(file)
else:
log.debug(f"Ignore {i18n_file.locale} {i18n_file}")
i18n_alternate_dest_uris[i18n_file.dest_uri].append(file)

# we've seen that file already
else:
log.debug(f"Ignore {i18n_file.locale} {i18n_file}")
i18n_alternate_dest_uris[i18n_file.dest_uri].append(file)
# override it only if this is our language
if i18n_file.locale == self.current_language:
# users should not add default non suffixed/folder files + suffixed/folder
# files when multiple languages are configured
if (
len(self.all_languages) > 1
and i18n_dest_uris[i18n_file.dest_uri].locale == i18n_file.locale
):
raise Exception(
f"Conflicting files for the default language '{self.default_language}': "
f"choose either '{i18n_file.src_uri}' or "
f"'{i18n_dest_uris[i18n_file.dest_uri].src_uri}' but not both"
)
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use localized {i18n_file.locale} {i18n_file}")
else:
log.debug(f"Ignore {i18n_file.locale} {i18n_file}")
i18n_alternate_dest_uris[i18n_file.dest_uri].append(file)

# we've seen that file already
# user provided asset
else:
# override it only if this is our language
if i18n_file.locale == self.current_language:
# users should not add default non suffixed/folder files + suffixed/folder
# files when multiple languages are configured
if (
len(self.all_languages) > 1
and i18n_dest_uris[i18n_file.dest_uri].locale == i18n_file.locale
# never seen that file?
if i18n_file.dest_uri not in i18n_dest_uris:
# best case scenario
# use the file since its locale is our current build language
if i18n_file.locale == self.current_language:
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use asset {i18n_file.locale} {i18n_file}")
# if locale is the default language AND default language fallback is enabled
# we are using a file that is not really our locale
elif (
self.config.fallback_to_default is True
and i18n_file.locale == self.default_language
):
raise Exception(
f"Conflicting files for the default language '{self.default_language}': "
f"choose either '{i18n_file.src_uri}' or "
f"'{i18n_dest_uris[i18n_file.dest_uri].src_uri}' but not both"
i18n_asset = create_i18n_file(
file,
self.default_language,
self.default_language,
self.all_languages,
mkdocs_config,
)
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use localized {i18n_file.locale} {i18n_file}")
i18n_dest_uris[i18n_file.dest_uri] = i18n_asset
log.debug(f"Use default asset {i18n_asset.locale} {i18n_asset}")

# we've seen that file already
else:
log.debug(f"Ignore {i18n_file.locale} {i18n_file}")
i18n_alternate_dest_uris[i18n_file.dest_uri].append(file)
# override it only if this is our language
if i18n_file.locale == self.current_language:
i18n_dest_uris[i18n_file.dest_uri] = i18n_file
log.debug(f"Use localized asset {i18n_file.locale} {i18n_file}")

# theme (and overrides) files
elif self.is_default_language_build or file.src_uri.startswith("assets/"):
elif self.is_default_language_build:
i18n_files.append(file)

# populate the resulting Files and keep track of all the alternates
Expand Down
Binary file not shown.
11 changes: 8 additions & 3 deletions tests/test_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,14 @@ def test_control_en_fr(
#
assert control_nav_fr.__str__() == test_nav_fr.__str__()
assert control_env_fr.filters.keys() == test_env_fr.filters.keys()
assert {f"fr/{file.dest_uri}" for file in control_files_fr} == {
file.dest_uri for file in test_files_fr
}

control_dest_uris = set()
for file in control_files_fr:
if not file.dest_uri.startswith("assets/"):
control_dest_uris.add(f"fr/{file.dest_uri}")
else:
control_dest_uris.add(file.dest_uri)
assert control_dest_uris == {file.dest_uri for file in test_files_fr}

control_pages = control_files_fr.documentation_pages()
control_pages.sort(key=lambda p: p.src_uri)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_structure_suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def test_build_no_use_directory_urls():
Path("fr/index.html"),
Path("fr/image.png"),
Path("fr/image.fake"),
Path("fr/assets/image_non_localized.png"),
Path("fr/english_default/index.html"),
Path("fr/topic1/named_file/index.html"),
Path("fr/topic2/index.html"),
Expand All @@ -102,7 +101,6 @@ def test_build_no_use_directory_urls():
Path("fr/index.html"),
Path("fr/image.png"),
Path("fr/image.fake"),
Path("fr/assets/image_non_localized.png"),
Path("fr/english_default/index.html"),
Path("fr/topic1/named_file.html"),
Path("fr/topic2/index.html"),
Expand Down

0 comments on commit c2215b2

Please sign in to comment.