localization.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // localization = {} -- the dict with translations is created by the backend
  2. ignore_ids_for_localization={
  3. setting_sd_hypernetwork: 'OPTION',
  4. setting_sd_model_checkpoint: 'OPTION',
  5. setting_realesrgan_enabled_models: 'OPTION',
  6. modelmerger_primary_model_name: 'OPTION',
  7. modelmerger_secondary_model_name: 'OPTION',
  8. modelmerger_tertiary_model_name: 'OPTION',
  9. train_embedding: 'OPTION',
  10. train_hypernetwork: 'OPTION',
  11. txt2img_style_index: 'OPTION',
  12. txt2img_style2_index: 'OPTION',
  13. img2img_style_index: 'OPTION',
  14. img2img_style2_index: 'OPTION',
  15. setting_random_artist_categories: 'SPAN',
  16. setting_face_restoration_model: 'SPAN',
  17. setting_realesrgan_enabled_models: 'SPAN',
  18. extras_upscaler_1: 'SPAN',
  19. extras_upscaler_2: 'SPAN',
  20. }
  21. re_num = /^[\.\d]+$/
  22. re_emoji = /[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u
  23. original_lines = {}
  24. translated_lines = {}
  25. function textNodesUnder(el){
  26. var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
  27. while(n=walk.nextNode()) a.push(n);
  28. return a;
  29. }
  30. function canBeTranslated(node, text){
  31. if(! text) return false;
  32. if(! node.parentElement) return false;
  33. parentType = node.parentElement.nodeName
  34. if(parentType=='SCRIPT' || parentType=='STYLE' || parentType=='TEXTAREA') return false;
  35. if (parentType=='OPTION' || parentType=='SPAN'){
  36. pnode = node
  37. for(var level=0; level<4; level++){
  38. pnode = pnode.parentElement
  39. if(! pnode) break;
  40. if(ignore_ids_for_localization[pnode.id] == parentType) return false;
  41. }
  42. }
  43. if(re_num.test(text)) return false;
  44. if(re_emoji.test(text)) return false;
  45. return true
  46. }
  47. function getTranslation(text){
  48. if(! text) return undefined
  49. if(translated_lines[text] === undefined){
  50. original_lines[text] = 1
  51. }
  52. tl = localization[text]
  53. if(tl !== undefined){
  54. translated_lines[tl] = 1
  55. }
  56. return tl
  57. }
  58. function processTextNode(node){
  59. text = node.textContent.trim()
  60. if(! canBeTranslated(node, text)) return
  61. tl = getTranslation(text)
  62. if(tl !== undefined){
  63. node.textContent = tl
  64. }
  65. }
  66. function processNode(node){
  67. if(node.nodeType == 3){
  68. processTextNode(node)
  69. return
  70. }
  71. if(node.title){
  72. tl = getTranslation(node.title)
  73. if(tl !== undefined){
  74. node.title = tl
  75. }
  76. }
  77. if(node.placeholder){
  78. tl = getTranslation(node.placeholder)
  79. if(tl !== undefined){
  80. node.placeholder = tl
  81. }
  82. }
  83. textNodesUnder(node).forEach(function(node){
  84. processTextNode(node)
  85. })
  86. }
  87. function dumpTranslations(){
  88. dumped = {}
  89. Object.keys(original_lines).forEach(function(text){
  90. if(dumped[text] !== undefined) return
  91. dumped[text] = localization[text] || text
  92. })
  93. return dumped
  94. }
  95. onUiUpdate(function(m){
  96. m.forEach(function(mutation){
  97. mutation.addedNodes.forEach(function(node){
  98. processNode(node)
  99. })
  100. });
  101. })
  102. document.addEventListener("DOMContentLoaded", function() {
  103. processNode(gradioApp())
  104. })
  105. function download_localization() {
  106. text = JSON.stringify(dumpTranslations(), null, 4)
  107. var element = document.createElement('a');
  108. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  109. element.setAttribute('download', "localization.json");
  110. element.style.display = 'none';
  111. document.body.appendChild(element);
  112. element.click();
  113. document.body.removeChild(element);
  114. }