gradio-pr-bot commited on
Commit
a8874af
·
verified ·
1 Parent(s): e7aa9f3

Upload folder using huggingface_hub

Browse files
6.5.2/utils/package.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "@gradio/utils",
3
- "version": "0.11.2",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "main": "./src/index.ts",
 
1
  {
2
  "name": "@gradio/utils",
3
+ "version": "0.11.3",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "main": "./src/index.ts",
6.5.2/utils/src/utils.svelte.ts CHANGED
@@ -3,6 +3,17 @@ import type { Client } from "@gradio/client";
3
  import type { ComponentType, SvelteComponent } from "svelte";
4
  import { getContext, tick, untrack } from "svelte";
5
  import type { Component } from "svelte";
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  export interface SharedProps {
8
  elem_id?: string;
@@ -302,11 +313,40 @@ export const allowed_shared_props: (keyof SharedProps)[] = [
302
  ] as const;
303
 
304
  export type I18nFormatter = any;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  export class Gradio<T extends object = {}, U extends object = {}> {
306
  load_component: load_component;
307
  shared: SharedProps = $state<SharedProps>({} as SharedProps) as SharedProps;
308
  props = $state<U>({} as U) as U;
309
- i18n: I18nFormatter = $state<any>({}) as any;
 
310
  dispatcher!: Function;
311
  last_update: ReturnType<typeof tick> | null = null;
312
  shared_props: (keyof SharedProps)[] = allowed_shared_props;
@@ -334,7 +374,22 @@ export class Gradio<T extends object = {}, U extends object = {}> {
334
  }
335
  }
336
  // @ts-ignore same here
337
- this.i18n = this.props.i18n;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
 
339
  this.load_component = this.shared.load_component;
340
 
@@ -364,10 +419,13 @@ export class Gradio<T extends object = {}, U extends object = {}> {
364
  // Need to update the props here
365
  // otherwise UI won't reflect latest state from render
366
  for (const key in _props.shared_props) {
 
 
367
  // @ts-ignore i'm not doing pointless typescript gymanstics
368
  this.shared[key] = _props.shared_props[key];
369
  }
370
  for (const key in _props.props) {
 
371
  // @ts-ignore same here
372
  this.props[key] = _props.props[key];
373
  }
@@ -380,6 +438,45 @@ export class Gradio<T extends object = {}, U extends object = {}> {
380
  this.shared.id = _props.shared_props.id;
381
  });
382
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
  }
384
 
385
  dispatch<E extends keyof T>(event_name: E, data?: T[E]): void {
@@ -396,19 +493,26 @@ export class Gradio<T extends object = {}, U extends object = {}> {
396
 
397
  set_data(data: Partial<U & SharedProps>): void {
398
  for (const key in data) {
 
 
 
 
 
 
 
 
 
 
 
 
399
  if (this.shared_props.includes(key as keyof SharedProps)) {
400
  const _key = key as keyof SharedProps;
401
  // @ts-ignore i'm not doing pointless typescript gymanstics
402
-
403
- this.shared[_key] = data[_key];
404
-
405
  continue;
406
-
407
- // @ts-ignore same here
408
- } else {
409
- // @ts-ignore same here
410
- this.props[key] = data[key];
411
  }
 
 
412
  }
413
  }
414
  }
 
3
  import type { ComponentType, SvelteComponent } from "svelte";
4
  import { getContext, tick, untrack } from "svelte";
5
  import type { Component } from "svelte";
6
+ import { locale } from "svelte-i18n";
7
+
8
+ export const I18N_MARKER = "__i18n__";
9
+ const TRANSLATABLE_PROPS = [
10
+ "label",
11
+ "info",
12
+ "placeholder",
13
+ "description",
14
+ "title",
15
+ "value"
16
+ ];
17
 
18
  export interface SharedProps {
19
  elem_id?: string;
 
313
  ] as const;
314
 
315
  export type I18nFormatter = any;
316
+
317
+ export function has_i18n_marker(value: unknown): value is string {
318
+ return typeof value === "string" && value.includes(I18N_MARKER);
319
+ }
320
+
321
+ export function translate_i18n_marker(
322
+ value: string,
323
+ translate: (key: string) => string
324
+ ): string {
325
+ const start = value.indexOf(I18N_MARKER);
326
+ if (start === -1) return value;
327
+
328
+ const json_start = start + I18N_MARKER.length;
329
+ const json_end = value.indexOf("}", json_start) + 1;
330
+ if (json_end === 0) return value;
331
+
332
+ try {
333
+ const metadata = JSON.parse(value.slice(json_start, json_end));
334
+ if (metadata?.key) {
335
+ const translated = translate(metadata.key);
336
+ const result = translated !== metadata.key ? translated : metadata.key;
337
+ return value.slice(0, start) + result + value.slice(json_end);
338
+ }
339
+ } catch {}
340
+
341
+ return value;
342
+ }
343
+
344
  export class Gradio<T extends object = {}, U extends object = {}> {
345
  load_component: load_component;
346
  shared: SharedProps = $state<SharedProps>({} as SharedProps) as SharedProps;
347
  props = $state<U>({} as U) as U;
348
+ i18n: I18nFormatter = $state<any>((v: string) => v) as any;
349
+ translatable_props: Record<string, string> = {};
350
  dispatcher!: Function;
351
  last_update: ReturnType<typeof tick> | null = null;
352
  shared_props: (keyof SharedProps)[] = allowed_shared_props;
 
374
  }
375
  }
376
  // @ts-ignore same here
377
+ this.i18n = this.props.i18n ?? ((v: string) => v);
378
+
379
+ for (const key of TRANSLATABLE_PROPS) {
380
+ // @ts-ignore
381
+ this.shared[key] = this._translate_and_store(
382
+ "shared",
383
+ key,
384
+ _props.shared_props[key]
385
+ );
386
+ // @ts-ignore
387
+ this.props[key] = this._translate_and_store(
388
+ "props",
389
+ key,
390
+ _props.props[key]
391
+ );
392
+ }
393
 
394
  this.load_component = this.shared.load_component;
395
 
 
419
  // Need to update the props here
420
  // otherwise UI won't reflect latest state from render
421
  for (const key in _props.shared_props) {
422
+ if (this._is_i18n_managed(`shared.${key}`, _props.shared_props[key]))
423
+ continue;
424
  // @ts-ignore i'm not doing pointless typescript gymanstics
425
  this.shared[key] = _props.shared_props[key];
426
  }
427
  for (const key in _props.props) {
428
+ if (this._is_i18n_managed(`props.${key}`, _props.props[key])) continue;
429
  // @ts-ignore same here
430
  this.props[key] = _props.props[key];
431
  }
 
438
  this.shared.id = _props.shared_props.id;
439
  });
440
  });
441
+
442
+ // retranslate props when locale changes
443
+ if (Object.keys(this.translatable_props).length > 0) {
444
+ locale.subscribe(() => {
445
+ for (const [full_key, original] of Object.entries(
446
+ this.translatable_props
447
+ )) {
448
+ const [target, key] = full_key.split(".");
449
+ const translated = this.i18n(original);
450
+ // @ts-ignore
451
+ if (target === "shared") this.shared[key] = translated;
452
+ // @ts-ignore
453
+ else this.props[key] = translated;
454
+ }
455
+ });
456
+ }
457
+ }
458
+
459
+ // check if props are translatable
460
+ _is_i18n_managed(key: string, new_value: unknown): boolean {
461
+ const original_marker = this.translatable_props[key];
462
+ if (!original_marker) return false;
463
+ if (new_value === original_marker) return true;
464
+ // if value has changed then remove key
465
+ delete this.translatable_props[key];
466
+ return false;
467
+ }
468
+
469
+ _translate_and_store(
470
+ target: "shared" | "props",
471
+ key: string,
472
+ value: unknown
473
+ ): unknown {
474
+ if (typeof value !== "string") return value;
475
+ const translated = this.i18n(value);
476
+ if (translated !== value) {
477
+ this.translatable_props[`${target}.${key}`] = value;
478
+ }
479
+ return translated;
480
  }
481
 
482
  dispatch<E extends keyof T>(event_name: E, data?: T[E]): void {
 
493
 
494
  set_data(data: Partial<U & SharedProps>): void {
495
  for (const key in data) {
496
+ // @ts-ignore
497
+ const value = data[key];
498
+ const translated = has_i18n_marker(value)
499
+ ? this._translate_and_store(
500
+ this.shared_props.includes(key as keyof SharedProps)
501
+ ? "shared"
502
+ : "props",
503
+ key,
504
+ value
505
+ )
506
+ : value;
507
+
508
  if (this.shared_props.includes(key as keyof SharedProps)) {
509
  const _key = key as keyof SharedProps;
510
  // @ts-ignore i'm not doing pointless typescript gymanstics
511
+ this.shared[_key] = translated;
 
 
512
  continue;
 
 
 
 
 
513
  }
514
+ // @ts-ignore
515
+ this.props[key] = translated;
516
  }
517
  }
518
  }