Internationalization (i18n)
Relevant source files
The following files were used as context for generating this wiki page:
Internationalization in the Technical Blog Monitor project addresses two distinct areas: the localization of the application's user interface (primarily command-line messages) and the handling of language attributes for the monitored blog content.
The current implementation provides a foundational framework using Python's standard gettext module for UI translations, activated by an environment variable. For content, the data model includes a field to store language information, although automatic language detection is not yet implemented. The Japanese README (README.ja.md) indicates that i18n is a feature intended for future expansion.
Sources: README.ja.md, monitor/i18n.py, monitor/models/article.py
Application UI Localization
The application's user interface, specifically command-line text, can be translated into different languages. This is managed through a simple gettext-based system.
Sources: monitor/i18n.py, monitor/main.py, README.ja.md
Architecture and Flow
The localization logic is contained within monitor/i18n.py. It checks for the MONITOR_LANG environment variable to determine the desired language. If the variable is set, it attempts to find the corresponding compiled message file (.mo) in the locales directory. If the variable is not set or the translation is not found, it defaults to a NullTranslations class, which simply returns the original English string.
The following diagram illustrates the translation lookup process:
Sources: monitor/i18n.py:9-24
Configuration
UI localization is controlled by a single environment variable.
| Variable | Description | Example |
|---|---|---|
MONITOR_LANG | Sets the language for application messages. The system will look for a corresponding .mo file in locales/<lang>/LC_MESSAGES/monitor.mo. | ja |
As noted in the Japanese README, setting MONITOR_LANG=ja will enable Japanese messages where available.
Sources: README.ja.md:23-24, monitor/i18n.py:12
Usage
The translation function, conventionally named _, is imported from monitor.i18n and used to wrap strings intended for translation. An example of this is in the command-line argument parser setup.
# monitor/main.py:413-416
def parse_args() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description=_("Technical Blog Monitor - Track and embed technical blog posts")
)
This ensures that the application's description can be displayed in the language specified by MONITOR_LANG.
Sources: monitor/main.py:20, 416
Content Language Handling
The system also has provisions for tracking the language of the articles it monitors, which is distinct from the application's own UI language.
Data Model
The ArticleContent data model includes an optional language field to store the detected language of the article's text.
# monitor/models/article.py:38-39
class ArticleContent(BaseModel):
# ...
language: Optional[str] = None
This field is designed to hold a language code (e.g., 'en', 'ja') for the processed article. However, the current extraction logic in monitor/extractor/article_parser.py does not populate this field, meaning it will default to None.
Sources: monitor/models/article.py:39, monitor/extractor/article_parser.py
Browser Language
When rendering pages with the headless browser (Playwright), the browser's language is hardcoded to English (en-US, en). This is done as part of the "stealth mode" to make the scraper appear more like a standard user agent.
// monitor/fetcher/browser.py:102-106
# Add language
await page.add_init_script("""
Object.defineProperty(navigator, 'languages', {
get: () => ['en-US', 'en'],
});
""")
This setting may influence the content served by websites that perform language negotiation based on browser headers.
Sources: monitor/fetcher/browser.py:102-106
Summary and Future Scope
The internationalization support in the Technical Blog Monitor is currently foundational.
- UI Localization: A working
gettext-based system is in place, controlled by theMONITOR_LANGenvironment variable. The existence ofREADME.ja.mdand its reference to this variable confirms that Japanese is a target language. - Content Language: The data model is prepared to store content language, but the logic to detect and populate this information has not been implemented.
- Scraping: The headless browser used for scraping identifies as English-speaking, which could affect multi-language sites.
The Japanese README file notes that i18n features are planned for future expansion ("将来的に拡充予定"), suggesting that more comprehensive language support is on the roadmap.
Sources: README.ja.md:24-25, monitor/i18n.py, monitor/models/article.py:39, monitor/fetcher/browser.py:102-106