File size: 13,146 Bytes
4b2a9c2
2c0627c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# pylint: skip-file
import functools
import inspect
import json
import os
from contextlib import contextmanager

import gradio as gr
import langcodes
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):
            add_values = args[0].add_values
            radd_values = args[0].radd_values
            result = I18nString(func(*args, **kwargs))
            result.add_values = add_values
            result.radd_values = radd_values

            return result
        return func(*args, **kwargs)

    return wrapper


inspect.cleandoc = escape_caller(inspect.cleandoc)


class TranslateContext:
    available_languages = ["en"]
    dictionary: dict = {}
    lang_per_session = {}

    def get_available_languages():
        return TranslateContext.available_languages

    def set_available_languages(langs: list):
        if not langs or not isinstance(langs, list):
            raise ValueError("langs must be a list of languages")
        TranslateContext.available_languages = langs

    def get_default_language():
        return TranslateContext.get_available_languages()[0]

    def add_translation(translation: dict):
        for k, v in translation.items():
            if k not in TranslateContext.available_languages:
                continue
            if k not in TranslateContext.dictionary:
                TranslateContext.dictionary[k] = {}
            TranslateContext.dictionary[k].update(v)

    def get_current_language(request: gr.Request):
        return TranslateContext.lang_per_session.get(
            request.session_hash, TranslateContext.get_default_language()
        )

    def set_current_language(request: gr.Request, lang: str):
        TranslateContext.lang_per_session[request.session_hash] = lang

    def get_lang_from_request(request: gr.Request):
        if "Accept-Language" not in request.headers:
            return TranslateContext.get_default_language()

        # Get the first language from the Accept-Language header
        lang = request.headers["Accept-Language"].split(",")[0]
        lang, _ = langcodes.closest_match(
            lang, TranslateContext.get_available_languages()
        )

        if not lang or lang == "und":
            return TranslateContext.get_default_language()
        return lang


class I18nString(str):
    __slots__ = ("_key", "add_values", "radd_values")

    def __new__(cls, value):
        obj = super().__new__(cls, value)
        obj._key = value
        obj.add_values = []
        obj.radd_values = []
        return obj

    def __str__(self):
        try:
            request = LocalContext.request.get()
        except LookupError:
            request = None
        if request is None:
            return self._key

        lang = TranslateContext.get_current_language(request)
        result = TranslateContext.dictionary.get(lang, {}).get(self._key, self._key)

        for v in self.radd_values:
            result = str(v) + result
        for v in self.add_values:
            result = result + str(v)

        while len(result) >= 2 and result.startswith("'") and result.endswith("'"):
            result = result[1:-1]
        return result

    def __add__(self, other):
        self.add_values.append(other)
        return self

    def __radd__(self, other):
        self.radd_values.append(other)
        return self

    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_strings(obj):
        """Unwrap all keys in I18nStrings in the object"""
        if isinstance(obj, I18nString):
            yield obj.unwrap()
            for v in obj.add_values:
                yield from I18nString.unwrap_strings(v)
            for v in obj.radd_values:
                yield from I18nString.unwrap_strings(v)
            return
        yield 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, existing_translation={}):
    """Check if there are new I18nStrings in the block
    :param block: The block 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 TranslateContext.get_available_languages():
        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, include_translations={}):
    """Dump all I18nStrings in the block to a dictionary
    :param block: The block 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 TranslateContext.get_available_languages():
        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]
                        for key in I18nString.unwrap_strings(value):
                            ret[lang][key] = translate(lang, key)
                else:
                    value = getattr(component, field)
                    for key in I18nString.unwrap_strings(value):
                        ret[lang][key] = translate(lang, key)

    return ret


def translate_blocks(
    block: gr.Blocks = None,
    translation={},
    lang: gr.components.Component = None,
    persistant=False,
):
    """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
    :param persistant: Whether to persist 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)

    hidden = gr.HTML(
        value="""<style>
                    gradio-app {
                        visibility: hidden;
                    }
                     </style>"""
    )

    if persistant:
        try:
            from gradio import BrowserState
        except ImportError:
            raise ValueError("gradio>=5.6.0 is required for persistant language")

    def on_lang_change(request: gr.Request, lang: str, saved_lang: str):
        if not lang:
            if saved_lang:
                lang = saved_lang
            else:
                lang = TranslateContext.get_lang_from_request(request)

        outputs = [lang, lang, ""]
        TranslateContext.set_current_language(request, lang)

        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]
                            # We don't need to translate the value
                            choices[idx] = (str(k), next(I18nString.unwrap_strings(v)))
                        else:
                            v = choices[idx]
                            choices[idx] = (str(v), next(I18nString.unwrap_strings(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()

    if persistant:
        saved_lang = gr.BrowserState(storage_key="lang")
    else:
        saved_lang = gr.State()

    gr.on(
        [block.load, lang.change],
        on_lang_change,
        inputs=[lang, saved_lang],
        outputs=[lang, saved_lang, hidden] + components,
    )


@contextmanager
def Translate(
    translation,
    lang: gr.components.Component = None,
    placeholder_langs=[],
    persistant=False,
):
    """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
    :param persistant: Whether to persist the language
    :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", encoding="utf-8") as f:  # Force utf-8 encoding
                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")

    if placeholder_langs:
        TranslateContext.set_available_languages(placeholder_langs)

    block = Context.block
    translate_blocks(
        block=block, translation=translation_dict, lang=lang, persistant=persistant
    )

    if isinstance(translation, str) and has_new_i18n_fields(
        block, existing_translation=translation_dict
    ):
        merged = dump_blocks(block, 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)