Skip to content

Commit

Permalink
Reduce missing i18n log spam (close #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Mar 29, 2024
1 parent f5e75f1 commit fd80eac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
* Adding spoilers to individual pages with the `advancement` field is now supported.
* Use [`uv`](https://github.com/astral-sh/uv) instead of `pip` for all reusable workflows.
* `hexdoc ci build` now attempts to read the site url from `HEXDOC_SITE_URL` and `GITHUB_PAGES_URL` environment variables before querying the GitHub API.
* "No translation in {lang} for key {key}" warnings are now only printed once per key for each language to reduce log spam.

## `1!0.1.0a10`

Expand Down
19 changes: 14 additions & 5 deletions src/hexdoc/minecraft/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,15 @@ def localize(
else:
log_level = logging.WARNING

logger.log(
log_level,
f"No translation in {self.lang} for "
+ (f"key {keys[0]}" if len(keys) == 1 else f"keys {keys}"),
)
log_keys = set(keys) - self._logged_missing_keys
if log_keys:
self._logged_missing_keys.update(log_keys)
match list(log_keys):
case [key]:
message = f"key {key}"
case _:
message = "keys " + ", ".join(log_keys)
logger.log(log_level, f"No translation in {self.lang} for {message}")

if default is not None:
return LocalizedStr.skip_i18n(default)
Expand Down Expand Up @@ -345,3 +349,8 @@ def localize_lang(self, silent: bool = False):
name = self.localize("language.name", silent=silent)
region = self.localize("language.region", silent=silent)
return f"{name} ({region})"

@model_validator(mode="after")
def _init_logger_cache(self):
self._logged_missing_keys = set[str]()
return self

0 comments on commit fd80eac

Please sign in to comment.