-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the i18n middleware for the server
- Loading branch information
1 parent
22bf140
commit 005546b
Showing
10 changed files
with
142 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"starter0": "Introduce the project", | ||
"starter1": "Review the contribution guidelines", | ||
"starter2": "How can I quickly get started?", | ||
"hello_message": "I'm your dedicated Q&A bot. Feel free to ask me anything about the current project~" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"starter0": "このプロジェクトを紹介します", | ||
"starter1": "コントリビューションガイドラインを確認する", | ||
"starter2": "どうやって素早く始められますか?", | ||
"hello_message": "私はあなた専用のQ&Aボットです。現在のプロジェクトについて何でも聞いてくださいね~" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"starter0": "이 프로젝트를 소개합니다", | ||
"starter1": "기여 가이드라인 보기", | ||
"starter2": "어떻게 빠르게 시작할 수 있나요?", | ||
"hello_message": "저는 여러분의 전용 Q&A 봇입니다. 현재 프로젝트에 대해 무엇이든 물어보세요~" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Dict | ||
from fastapi import FastAPI, Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
import json | ||
from pathlib import Path | ||
|
||
|
||
class I18nConfig: | ||
def __init__( | ||
self, default_language: str = "en", translations_dir: str = "translations" | ||
): | ||
self.default_language = default_language | ||
self.translations_dir = Path(translations_dir) | ||
self.translations: Dict[str, Dict] = {} | ||
self._load_translations() | ||
|
||
def _load_translations(self): | ||
"""load translations from the translations directory""" | ||
if not self.translations_dir.exists(): | ||
raise FileNotFoundError( | ||
f"Translations directory {self.translations_dir} not found" | ||
) | ||
|
||
for lang_file in self.translations_dir.glob("*.json"): | ||
lang_code = lang_file.stem | ||
with open(lang_file, "r", encoding="utf-8") as f: | ||
self.translations[lang_code] = json.load(f) | ||
|
||
def get_text(self, key: str, lang: str) -> str: | ||
if lang not in self.translations: | ||
lang = self.default_language | ||
return self.translations[lang].get(key, key) | ||
|
||
|
||
class I18nMiddleware(BaseHTTPMiddleware): | ||
def __init__(self, app: FastAPI, i18n_config: I18nConfig): | ||
super().__init__(app) | ||
self.i18n_config = i18n_config | ||
|
||
async def dispatch(self, request: Request, call_next): | ||
lang = request.query_params.get( | ||
"lang", self.i18n_config.default_language | ||
) or request.query_params.get("lang", self.i18n_config.default_language) | ||
|
||
request.state.i18n = self.i18n_config | ||
request.state.lang = lang | ||
|
||
response = await call_next(request) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"starter0": "介绍一下这个项目", | ||
"starter1": "查看贡献指南", | ||
"starter2": "我该怎样快速上手", | ||
"hello_message": "我是你专属的答疑机器人,你可以问我关于当前项目的任何问题~" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"starter0": "Introduce the project", | ||
"starter1": "Review the contribution guidelines", | ||
"starter2": "How can I quickly get started?", | ||
"hello_message": "I'm your dedicated Q&A bot. Feel free to ask me anything about the current project~" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters