1. Design concept
The internationalization solution of this project is based on Python's gettext module and provides a flexible and extensible multilingual support system.
2. Language support
2.1 List of supported languages
The project supports over 35 languages, including:
- Chinese (simplified, traditional)
- English
- Hindi language (Hindi, Punjabi, etc.)
- Southeast Asian Languages
- European Languages
- African Languages
2.2 Language code design
Use standard language code formats, such as:
- zh_Hans: Simplified Chinese
- zh_Hant: Traditional Chinese
- en: English
3. Core function analysis
3.1 Get the translator get_translator()
@lru_cache(maxsize=None) def get_translator(locale: str): return ( 'messages', localedir=((__file__), 'locales'), languages=[locale], fallback=True )
Use @lru_cache to cache translator instances to improve performance
Load translation files from locales directory
Support language fallback mechanism
3.2 Language Get_locale_from_request()
def get_locale_from_request(request: Request) -> str: locale = ( request.query_params.get('lang') or ('locale') or ('accept-language', 'en')[:2] ) if locale not in SUPPORTED_LANGUAGES: locale = 'en' # Use English by default return locale
Language acquisition priority:
- URL query parameters
- Cookie
- Browser language header
- Default English
3.3 Middleware i18n_middleware()
def i18n_middleware(get_locale: Callable[[Request], str]): async def middleware(request: Request, call_next): locale = get_locale(request) translator = get_translator(locale) = locale = .supported_languages = SUPPORTED_LANGUAGES response = await call_next(request) return response return middleware
Middleware functions:
- Set the requested locale
- Inject the translation function gettext
- List of supported languages
4. Use examples
4.1 Used in templates
# In Jinja2 template{{ _('welcome_message') }}
4.2 Used in the code
def some_function(request): # Use the translation function in the request welcome_text = ('welcome_message')
5. Directory structure
locales/
├── en/
│ └── LC_MESSAGES/
│ └──
├── zh_Hans/
│ └── LC_MESSAGES/
│ └──
└── zh_Hant/
└── LC_MESSAGES/
└──
6. Performance optimization
Use @lru_cache Cache Translator
Support lazy loading of translation files
Provide language fallback mechanism
7. Best Practices
Using standardized language code
Provides a complete language back-up
Support dynamic language switching
8. Conclusion
This internationalization solution provides a flexible and efficient multilingual support mechanism that can meet the localization needs of complex web applications.
Key technologies:
Python gettext
FastAPI Middleware
LRU cache
Locale detection
This is the end of this article about the detailed explanation of the example of Python implementing the internationalization of i18n in web applications. For more related content on Python's internationalization of i18n, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!