gt-fastapi 0.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 General Translation, Inc.
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gt-fastapi
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: FastAPI integration for General Translation
|
|
5
|
+
Author: Ernest McCarter
|
|
6
|
+
Author-email: Ernest McCarter <ernest@generaltranslation.com>
|
|
7
|
+
License-Expression: FSL-1.1-ALv2
|
|
8
|
+
License-File: LICENSE.md
|
|
9
|
+
Requires-Dist: generaltranslation
|
|
10
|
+
Requires-Dist: gt-i18n
|
|
11
|
+
Requires-Dist: fastapi>=0.100.0
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "gt-fastapi"
|
|
3
|
+
version = "0.0.0"
|
|
4
|
+
description = "FastAPI integration for General Translation"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Ernest McCarter", email = "ernest@generaltranslation.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
license = "FSL-1.1-ALv2"
|
|
11
|
+
license-files = ["LICENSE.md"]
|
|
12
|
+
dependencies = [
|
|
13
|
+
"generaltranslation",
|
|
14
|
+
"gt-i18n",
|
|
15
|
+
"fastapi>=0.100.0",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[tool.uv.sources]
|
|
19
|
+
generaltranslation = { workspace = true }
|
|
20
|
+
gt-i18n = { workspace = true }
|
|
21
|
+
|
|
22
|
+
[build-system]
|
|
23
|
+
requires = ["uv_build>=0.10.8,<0.11.0"]
|
|
24
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""FastAPI integration: initialize_gt() and locale detection."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from contextlib import asynccontextmanager
|
|
6
|
+
from typing import Any, AsyncGenerator, Callable
|
|
7
|
+
|
|
8
|
+
from generaltranslation.locales import determine_locale
|
|
9
|
+
|
|
10
|
+
from gt_i18n import I18nManager, set_i18n_manager, t # noqa: F401
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _detect_from_accept_language(
|
|
14
|
+
request: Any, manager: I18nManager
|
|
15
|
+
) -> str:
|
|
16
|
+
"""Parse Accept-Language header and resolve against configured locales."""
|
|
17
|
+
accept = request.headers.get("accept-language", "")
|
|
18
|
+
if not accept:
|
|
19
|
+
return manager.default_locale
|
|
20
|
+
|
|
21
|
+
locales: list[tuple[float, str]] = []
|
|
22
|
+
for part in accept.split(","):
|
|
23
|
+
part = part.strip()
|
|
24
|
+
if not part:
|
|
25
|
+
continue
|
|
26
|
+
if ";q=" in part:
|
|
27
|
+
lang, q = part.split(";q=", 1)
|
|
28
|
+
try:
|
|
29
|
+
quality = float(q.strip())
|
|
30
|
+
except ValueError:
|
|
31
|
+
quality = 0.0
|
|
32
|
+
locales.append((quality, lang.strip()))
|
|
33
|
+
else:
|
|
34
|
+
locales.append((1.0, part))
|
|
35
|
+
|
|
36
|
+
locales.sort(key=lambda x: x[0], reverse=True)
|
|
37
|
+
locale_list = [loc for _, loc in locales]
|
|
38
|
+
|
|
39
|
+
approved = manager.get_locales()
|
|
40
|
+
if not approved:
|
|
41
|
+
return locale_list[0] if locale_list else manager.default_locale
|
|
42
|
+
|
|
43
|
+
result = determine_locale(locale_list, approved)
|
|
44
|
+
return result or manager.default_locale
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def initialize_gt(
|
|
48
|
+
app: Any,
|
|
49
|
+
*,
|
|
50
|
+
default_locale: str = "en",
|
|
51
|
+
locales: list[str] | None = None,
|
|
52
|
+
project_id: str | None = None,
|
|
53
|
+
cache_url: str | None = None,
|
|
54
|
+
get_locale: Callable[..., str] | None = None,
|
|
55
|
+
load_translations: Callable[[str], dict[str, str]] | None = None,
|
|
56
|
+
eager_loading: bool = True,
|
|
57
|
+
**kwargs: Any,
|
|
58
|
+
) -> I18nManager:
|
|
59
|
+
"""Initialize General Translation for a FastAPI app.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
app: The FastAPI application instance.
|
|
63
|
+
default_locale: Source locale.
|
|
64
|
+
locales: Target locales.
|
|
65
|
+
project_id: GT project ID for CDN loading.
|
|
66
|
+
cache_url: CDN base URL override.
|
|
67
|
+
get_locale: Custom locale detection callback ``(request) -> str``.
|
|
68
|
+
load_translations: Custom translation loader ``(locale) -> dict``.
|
|
69
|
+
eager_loading: Load all translations at startup (default True).
|
|
70
|
+
**kwargs: Additional kwargs passed to I18nManager.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
The configured I18nManager.
|
|
74
|
+
"""
|
|
75
|
+
manager = I18nManager(
|
|
76
|
+
default_locale=default_locale,
|
|
77
|
+
locales=locales,
|
|
78
|
+
project_id=project_id,
|
|
79
|
+
cache_url=cache_url,
|
|
80
|
+
load_translations=load_translations,
|
|
81
|
+
**kwargs,
|
|
82
|
+
)
|
|
83
|
+
set_i18n_manager(manager)
|
|
84
|
+
|
|
85
|
+
# Wrap existing lifespan if present
|
|
86
|
+
existing_lifespan = getattr(app, "router", app).lifespan_context
|
|
87
|
+
|
|
88
|
+
@asynccontextmanager
|
|
89
|
+
async def _gt_lifespan(a: Any) -> AsyncGenerator[Any, None]:
|
|
90
|
+
if eager_loading and locales:
|
|
91
|
+
await manager.load_all_translations()
|
|
92
|
+
async with existing_lifespan(a) as state:
|
|
93
|
+
yield state
|
|
94
|
+
|
|
95
|
+
app.router.lifespan_context = _gt_lifespan
|
|
96
|
+
|
|
97
|
+
@app.middleware("http")
|
|
98
|
+
async def gt_middleware(request: Any, call_next: Any) -> Any:
|
|
99
|
+
if get_locale:
|
|
100
|
+
locale = get_locale(request)
|
|
101
|
+
else:
|
|
102
|
+
locale = _detect_from_accept_language(request, manager)
|
|
103
|
+
manager.set_locale(locale)
|
|
104
|
+
return await call_next(request)
|
|
105
|
+
|
|
106
|
+
return manager
|