# python: FastAPI 快速入门 URL: https://generaltranslation.com/zh/docs/python/tutorials/fastapi-quickstart.mdx --- title: FastAPI 快速入门 description: 使用 gt-fastapi 为你的 FastAPI 应用添加国际化支持 --- **实验性:** `gt-fastapi` 目前仍处于实验阶段,后续可能会有破坏性变更。 ## 安装 ```bash pip install gt-fastapi ``` ## 创建配置文件 在项目根目录下创建一个 `gt.config.json`: ```json title="gt.config.json" { "projectId": "your-project-id", "defaultLocale": "en", "locales": ["es", "fr"] } ``` ## 初始化 `initialize_gt` 会自动从当前工作目录中读取 `gt.config.json`。如果配置文件位于其他位置,请通过 `config_path` 传入其路径: ```python title="app.py" from fastapi import FastAPI from gt_fastapi import initialize_gt, t app = FastAPI() initialize_gt(app) # 或:initialize_gt(app, config_path="path/to/gt.config.json") ``` ## 翻译字符串 在路由中使用 `t` 函数: ```python title="app.py" @app.get("/") def index(): return {"message": t("Hello, world!")} ``` ## 后续步骤 * 关于变量插值和相关选项,请参阅 [`t`](/docs/python/api/t)。 * 关于所有配置选项,请参阅 [`initialize_gt`](/docs/python/api/initialize-gt)。