Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

localization.js 4.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. var re_num = /^[.\d]+$/;
  2. var original_lines = {};
  3. var translated_lines = {};
  4. function hasLocalization() {
  5. return window.localization && Object.keys(window.localization).length > 0;
  6. }
  7. function textNodesUnder(el) {
  8. var n, a = [], walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);
  9. while ((n = walk.nextNode())) a.push(n);
  10. return a;
  11. }
  12. function canBeTranslated(node, text) {
  13. if (!text) return false;
  14. if (!node.parentElement) return false;
  15. var parentType = node.parentElement.nodeName;
  16. if (parentType == 'SCRIPT' || parentType == 'STYLE' || parentType == 'TEXTAREA') return false;
  17. if (re_num.test(text)) return false;
  18. return true;
  19. }
  20. function getTranslation(text) {
  21. if (!text) return undefined;
  22. if (translated_lines[text] === undefined) {
  23. original_lines[text] = 1;
  24. }
  25. var tl = localization[text];
  26. if (tl !== undefined) {
  27. translated_lines[tl] = 1;
  28. }
  29. return tl;
  30. }
  31. function processTextNode(node) {
  32. var text = node.textContent.trim();
  33. if (!canBeTranslated(node, text)) return;
  34. var tl = getTranslation(text);
  35. if (tl !== undefined) {
  36. node.textContent = tl;
  37. if (text && node.parentElement) {
  38. node.parentElement.setAttribute("data-original-text", text);
  39. }
  40. }
  41. }
  42. function processNode(node) {
  43. if (node.nodeType == 3) {
  44. processTextNode(node);
  45. return;
  46. }
  47. if (node.title) {
  48. let tl = getTranslation(node.title);
  49. if (tl !== undefined) {
  50. node.title = tl;
  51. }
  52. }
  53. if (node.placeholder) {
  54. let tl = getTranslation(node.placeholder);
  55. if (tl !== undefined) {
  56. node.placeholder = tl;
  57. }
  58. }
  59. textNodesUnder(node).forEach(function(node) {
  60. processTextNode(node);
  61. });
  62. }
  63. function refresh_style_localization() {
  64. processNode(document.querySelector('.style_selections'));
  65. }
  66. function refresh_aspect_ratios_label(value) {
  67. label = document.querySelector('#aspect_ratios_accordion div span');
  68. translation = getTranslation("Aspect Ratios");
  69. if (typeof translation == "undefined") {
  70. translation = "Aspect Ratios";
  71. }
  72. label.textContent = translation + " " + htmlDecode(value);
  73. }
  74. function localizeWholePage() {
  75. processNode(gradioApp());
  76. function elem(comp) {
  77. var elem_id = comp.props.elem_id ? comp.props.elem_id : "component-" + comp.id;
  78. return gradioApp().getElementById(elem_id);
  79. }
  80. for (var comp of window.gradio_config.components) {
  81. if (comp.props.webui_tooltip) {
  82. let e = elem(comp);
  83. let tl = e ? getTranslation(e.title) : undefined;
  84. if (tl !== undefined) {
  85. e.title = tl;
  86. }
  87. }
  88. if (comp.props.placeholder) {
  89. let e = elem(comp);
  90. let textbox = e ? e.querySelector('[placeholder]') : null;
  91. let tl = textbox ? getTranslation(textbox.placeholder) : undefined;
  92. if (tl !== undefined) {
  93. textbox.placeholder = tl;
  94. }
  95. }
  96. }
  97. }
  98. document.addEventListener("DOMContentLoaded", function() {
  99. if (!hasLocalization()) {
  100. return;
  101. }
  102. onUiUpdate(function(m) {
  103. m.forEach(function(mutation) {
  104. mutation.addedNodes.forEach(function(node) {
  105. processNode(node);
  106. });
  107. });
  108. });
  109. localizeWholePage();
  110. if (localization.rtl) { // if the language is from right to left,
  111. (new MutationObserver((mutations, observer) => { // wait for the style to load
  112. mutations.forEach(mutation => {
  113. mutation.addedNodes.forEach(node => {
  114. if (node.tagName === 'STYLE') {
  115. observer.disconnect();
  116. for (const x of node.sheet.rules) { // find all rtl media rules
  117. if (Array.from(x.media || []).includes('rtl')) {
  118. x.media.appendMedium('all'); // enable them
  119. }
  120. }
  121. }
  122. });
  123. });
  124. })).observe(gradioApp(), {childList: true});
  125. }
  126. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...