|
| 1 | +<!-- |
| 2 | +Copyright 2018 Google LLC |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +--> |
| 13 | +<html> |
| 14 | +<head> |
| 15 | + <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> |
| 16 | + <style> |
| 17 | + .logo { vertical-align: middle; } |
| 18 | + ul { list-style-type: none; padding: 0; } |
| 19 | + h4 { margin: 10px 0 5px 0; } |
| 20 | + .error { color: #d32f2f; } |
| 21 | + .success { color: #388e3c; } |
| 22 | + #button-bar button { margin: 5px 0; width: 100%; } |
| 23 | + </style> |
| 24 | +</head> |
| 25 | +<body> |
| 26 | +<form class="sidebar branding-below"> |
| 27 | + <h4>Source language:</h4> |
| 28 | + <ul id="source-languages"></ul> |
| 29 | + |
| 30 | + <h4>Target language:</h4> |
| 31 | + <ul id="target-languages"></ul> |
| 32 | + |
| 33 | + <div class="block" id="button-bar"> |
| 34 | + <button class="blue" id="run-translation">Translate Selected</button> |
| 35 | + <button class="blue" id="run-all-translation">Translate All Slides</button> |
| 36 | + </div> |
| 37 | + <h5 class="error" id="error"></h5> |
| 38 | + <h5 class="success" id="success"></h5> |
| 39 | +</form> |
| 40 | +<div class="sidebar bottom"> |
| 41 | + <img alt="Add-on logo" class="logo" |
| 42 | + src="https://www.gstatic.com/images/branding/product/1x/translate_48dp.png" width="27" height="27"> |
| 43 | + <span class="gray branding-text">Translate sample by Google</span> |
| 44 | +</div> |
| 45 | + |
| 46 | +<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> |
| 47 | +<script> |
| 48 | + $(function() { |
| 49 | + // Expanded list of languages with auto-detect option for source |
| 50 | + const languages = { |
| 51 | + '': 'Auto-detect', |
| 52 | + ar: 'Arabic', |
| 53 | + bg: 'Bulgarian', |
| 54 | + zh: 'Chinese (Simplified)', |
| 55 | + 'zh-TW': 'Chinese (Traditional)', |
| 56 | + hr: 'Croatian', |
| 57 | + cs: 'Czech', |
| 58 | + da: 'Danish', |
| 59 | + nl: 'Dutch', |
| 60 | + en: 'English', |
| 61 | + fi: 'Finnish', |
| 62 | + fr: 'French', |
| 63 | + de: 'German', |
| 64 | + el: 'Greek', |
| 65 | + he: 'Hebrew', |
| 66 | + hi: 'Hindi', |
| 67 | + hu: 'Hungarian', |
| 68 | + id: 'Indonesian', |
| 69 | + it: 'Italian', |
| 70 | + ja: 'Japanese', |
| 71 | + ko: 'Korean', |
| 72 | + no: 'Norwegian', |
| 73 | + pl: 'Polish', |
| 74 | + pt: 'Portuguese', |
| 75 | + ro: 'Romanian', |
| 76 | + ru: 'Russian', |
| 77 | + es: 'Spanish', |
| 78 | + sv: 'Swedish', |
| 79 | + th: 'Thai', |
| 80 | + tr: 'Turkish', |
| 81 | + uk: 'Ukrainian', |
| 82 | + vi: 'Vietnamese' |
| 83 | + }; |
| 84 | + |
| 85 | + // Create radio buttons for source languages (including auto-detect) |
| 86 | + const sourceLanguageList = Object.keys(languages).map((id) => { |
| 87 | + return $('<li>').html([ |
| 88 | + $('<input>') |
| 89 | + .attr('type', 'radio') |
| 90 | + .attr('name', 'source') |
| 91 | + .attr('id', 'radio-source-' + id) |
| 92 | + .attr('value', id) |
| 93 | + .prop('checked', id === ''), // Auto-detect selected by default |
| 94 | + $('<label>') |
| 95 | + .attr('for', 'radio-source-' + id) |
| 96 | + .html(languages[id]) |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + // Create radio buttons for target languages (excluding auto-detect) |
| 101 | + const targetLanguageList = Object.keys(languages).filter(id => id !== '').map((id) => { |
| 102 | + return $('<li>').html([ |
| 103 | + $('<input>') |
| 104 | + .attr('type', 'radio') |
| 105 | + .attr('name', 'dest') |
| 106 | + .attr('id', 'radio-dest-' + id) |
| 107 | + .attr('value', id) |
| 108 | + .prop('checked', id === 'en'), // English selected by default |
| 109 | + $('<label>') |
| 110 | + .attr('for', 'radio-dest-' + id) |
| 111 | + .html(languages[id]) |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + $('#run-translation').click(runTranslation); |
| 116 | + $('#run-all-translation').click(runAllTranslation); |
| 117 | + $('#source-languages').html(sourceLanguageList); |
| 118 | + $('#target-languages').html(targetLanguageList); |
| 119 | + }); |
| 120 | + |
| 121 | + /** |
| 122 | + * Runs a server-side function to translate the text on selected slides/elements. |
| 123 | + */ |
| 124 | + function runTranslation() { |
| 125 | + this.disabled = true; |
| 126 | + $('#error').text(''); |
| 127 | + $('#success').text(''); |
| 128 | + google.script.run |
| 129 | + .withSuccessHandler((numTranslatedElements, element) =>{ |
| 130 | + element.disabled = false; |
| 131 | + if (numTranslatedElements === 0) { |
| 132 | + $('#error').empty() |
| 133 | + .append('Did you select elements to translate?') |
| 134 | + .append('<br/>') |
| 135 | + .append('Please select slides or individual elements.'); |
| 136 | + } else { |
| 137 | + $('#success').text('Successfully translated ' + numTranslatedElements + ' element(s)!'); |
| 138 | + } |
| 139 | + return false; |
| 140 | + }) |
| 141 | + .withFailureHandler((msg, element)=> { |
| 142 | + element.disabled = false; |
| 143 | + $('#error').text('Something went wrong. Please check the add-on logs.'); |
| 144 | + return false; |
| 145 | + }) |
| 146 | + .withUserObject(this) |
| 147 | + .translateSelectedElements($('input[name=dest]:checked').val()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Runs a server-side function to translate all text on all slides. |
| 152 | + */ |
| 153 | + function runAllTranslation() { |
| 154 | + this.disabled = true; |
| 155 | + $('#error').text(''); |
| 156 | + $('#success').text(''); |
| 157 | + const sourceLanguage = $('input[name=source]:checked').val(); |
| 158 | + const targetLanguage = $('input[name=dest]:checked').val(); |
| 159 | + |
| 160 | + google.script.run |
| 161 | + .withSuccessHandler((numTranslatedElements, element) => { |
| 162 | + element.disabled = false; |
| 163 | + if (numTranslatedElements === 0) { |
| 164 | + $('#error').text('No text elements found to translate.'); |
| 165 | + } else { |
| 166 | + $('#success').text('Successfully translated ' + numTranslatedElements + ' element(s) across all slides!'); |
| 167 | + } |
| 168 | + return false; |
| 169 | + }) |
| 170 | + .withFailureHandler((msg, element) => { |
| 171 | + element.disabled = false; |
| 172 | + $('#error').text('Something went wrong: ' + msg); |
| 173 | + return false; |
| 174 | + }) |
| 175 | + .withUserObject(this) |
| 176 | + .translateAllSlides(sourceLanguage, targetLanguage); |
| 177 | + } |
| 178 | +</script> |
| 179 | +</body> |
| 180 | +</html> |
0 commit comments