File size: 10,841 Bytes
09ad14a dd12bff 09ad14a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
"""
This code is based on the gradio-i18n==0.0.10 project by hoveychen.
Original source: https://github.com/hoveychen/gradio-i18n
Modifications: When loading the app, force language switching and reload translations.
"""
import functools
import inspect
import json
import os
from contextlib import contextmanager
import gradio as gr
import yaml
from gradio.blocks import Block, BlockContext, Context, LocalContext
# Monkey patch to escape I18nString type being stripped in gradio.Markdown
def escape_caller(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if args and isinstance(args[0], I18nString):
return I18nString(func(*args, **kwargs))
return func(*args, **kwargs)
return wrapper
inspect.cleandoc = escape_caller(inspect.cleandoc)
class TranslateContext:
dictionary: dict = {}
def add_translation(translation: dict):
for k, v in translation.items():
if k not in TranslateContext.dictionary:
TranslateContext.dictionary[k] = {}
TranslateContext.dictionary[k].update(v)
lang_per_session = {}
def get_lang_from_request(request: gr.Request):
lang = request.headers["Accept-Language"].split(",")[0].split("-")[0].lower()
if not lang:
return "en"
return lang
class I18nString(str):
def __new__(cls, value):
request: gr.Request = LocalContext.request.get()
if request is None:
return super().__new__(cls, value)
lang = TranslateContext.lang_per_session.get(request.session_hash, "en")
result = TranslateContext.dictionary.get(lang, {}).get(value, value)
return result
def __str__(self):
request: gr.Request = LocalContext.request.get()
if request is None:
return self
lang = TranslateContext.lang_per_session.get(request.session_hash, "en")
result = TranslateContext.dictionary.get(lang, {}).get(self, super().__str__())
return result
def __add__(self, other):
v = str(self)
if isinstance(v, I18nString):
return super().__add__(other)
return v.__add__(other)
def __radd__(self, other):
v = str(self)
if isinstance(v, I18nString):
return other.__add__(other)
return other.__add__(v)
def __hash__(self) -> int:
return super().__hash__()
def format(self, *args, **kwargs) -> str:
v = str(self)
if isinstance(v, I18nString):
return super().format(*args, **kwargs)
return v.format(*args, **kwargs)
def unwrap(self):
return super().__str__()
@staticmethod
def unwrap_string(obj):
if isinstance(obj, I18nString):
return obj.unwrap()
return obj
def gettext(key: str):
"""Wrapper text string to return I18nString
:param key: The key of the I18nString
"""
return I18nString(key)
def iter_i18n_choices(choices):
"""Iterate all I18nStrings in the choice, returns the indices of the I18nStrings"""
if not isinstance(choices, list) or len(choices) == 0:
return
if isinstance(choices[0], tuple):
for i, (k, v) in enumerate(choices):
if isinstance(k, I18nString):
yield i
else:
for i, v in enumerate(choices):
if isinstance(v, I18nString):
yield i
def iter_i18n_fields(component: gr.components.Component):
"""Iterate all I18nStrings in the component"""
for name, value in inspect.getmembers(component):
if name == "value" and hasattr(component, "choices"):
# for those components with choices, the value will be kept as is
continue
if isinstance(value, I18nString):
yield name
elif name == "choices" and any(iter_i18n_choices(value)):
yield name
def iter_i18n_components(block: Block):
"""Iterate all I18nStrings in the block"""
if isinstance(block, BlockContext):
for component in block.children:
for c in iter_i18n_components(component):
yield c
if any(iter_i18n_fields(block)):
yield block
def has_new_i18n_fields(block: Block, langs=["en"], existing_translation={}):
"""Check if there are new I18nStrings in the block
:param block: The block to check
:param langs: The languages to check
:param existing_translation: The existing translation dictionary
:return: True if there are new I18nStrings, False otherwise
"""
components = list(iter_i18n_components(block))
for lang in langs:
for component in components:
for field in iter_i18n_fields(component):
if field == "choices":
for idx in iter_i18n_choices(component.choices):
if isinstance(component.choices[idx], tuple):
value = component.choices[idx][0]
else:
value = component.choices[idx]
if value not in existing_translation.get(lang, {}):
return True
else:
value = getattr(component, field)
if value not in existing_translation.get(lang, {}):
return True
return False
def dump_blocks(block: Block, langs=["en"], include_translations={}):
"""Dump all I18nStrings in the block to a dictionary
:param block: The block to dump
:param langs: The languages to dump
:param include_translations: The existing translation dictionary
:return: The dumped dictionary
"""
components = list(iter_i18n_components(block))
def translate(lang, key):
return include_translations.get(lang, {}).get(key, key)
ret = {}
for lang in langs:
ret[lang] = {}
for component in components:
for field in iter_i18n_fields(component):
if field == "choices":
for idx in iter_i18n_choices(component.choices):
if isinstance(component.choices[idx], tuple):
value = component.choices[idx][0]
else:
value = component.choices[idx]
value = I18nString.unwrap_string(value)
ret[lang][value] = translate(lang, value)
else:
value = getattr(component, field)
value = I18nString.unwrap_string(value)
ret[lang][value] = translate(lang, value)
return ret
def translate_blocks(
block: gr.Blocks = None, translation={}, lang: gr.components.Component = None
):
"""Translate all I18nStrings in the block
:param block: The block to translate, default is the root block
:param translation: The translation dictionary
:param lang: The language component to change the language
"""
if block is None:
block = Context.root_block
"""Translate all I18nStrings in the block"""
if not isinstance(block, gr.Blocks):
raise ValueError("block must be an instance of gradio.Blocks")
components = list(iter_i18n_components(block))
TranslateContext.add_translation(translation)
def on_load(request: gr.Request):
lang = get_lang_from_request(request)
if lang not in translation.keys():
lang = "en"
return lang
def on_lang_change(request: gr.Request, lang: str):
TranslateContext.lang_per_session[request.session_hash] = lang
outputs = []
for component in components:
fields = list(iter_i18n_fields(component))
if component == lang and "value" in fields:
raise ValueError("'lang' component can't has I18nStrings as value")
modified = {}
for field in fields:
if field == "choices":
choices = component.choices.copy()
for idx in iter_i18n_choices(choices):
if isinstance(choices[idx], tuple):
k, v = choices[idx]
choices[idx] = (str(k), I18nString.unwrap_string(v))
else:
v = choices[idx]
choices[idx] = (str(v), I18nString.unwrap_string(v))
modified[field] = choices
else:
modified[field] = str(getattr(component, field))
new_comp = gr.update(**modified)
outputs.append(new_comp)
if len(outputs) == 1:
return outputs[0]
return outputs
if lang is None:
lang = gr.State()
block.load(on_load, outputs=[lang]).then(on_lang_change, inputs=[lang], outputs=components)
lang.change(on_lang_change, inputs=[lang], outputs=components)
@contextmanager
def Translate(translation, lang: gr.components.Component = None, placeholder_langs=[]):
"""Translate all I18nStrings in the block
:param translation: The translation dictionary or file path
:param lang: The language component to change the language
:param placeholder_langs: The placeholder languages to create a new translation file if translation is a file path
:return: The language component
"""
if lang is None:
lang = gr.State()
yield lang
if isinstance(translation, dict):
# Static translation
translation_dict = translation
pass
elif isinstance(translation, str):
if os.path.exists(translation):
# Regard as a file path
with open(translation, "r") as f:
if translation.endswith(".json"):
translation_dict = json.load(f)
elif translation.endswith(".yaml"):
translation_dict = yaml.safe_load(f)
else:
raise ValueError("Unsupported file format")
else:
translation_dict = {}
else:
raise ValueError("Unsupported translation type")
block = Context.block
translate_blocks(block=block, translation=translation_dict, lang=lang)
if (
placeholder_langs
and isinstance(translation, str)
and has_new_i18n_fields(
block, langs=placeholder_langs, existing_translation=translation_dict
)
):
merged = dump_blocks(
block, langs=placeholder_langs, include_translations=translation_dict
)
with open(translation, "w") as f:
if translation.endswith(".json"):
json.dump(merged, f, indent=2, ensure_ascii=False)
elif translation.endswith(".yaml"):
yaml.dump(merged, f, allow_unicode=True, sort_keys=False)
|