├── .idea └── vcs.xml ├── Code.gs └── README.md /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Code.gs: -------------------------------------------------------------------------------- 1 | /* 2 | localizable-sheet-script 3 | A Google Sheets script that will take a sheet in a specific format and return iOS and Android localization files. 4 | https://github.com/cobeisfresh/localizable-sheet-script 5 | Created by COBE http://cobeisfresh.com/ Copyright 2017 COBE 6 | License: MIT 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | */ 11 | 12 | // Configurable properties 13 | 14 | /* 15 | The number of languages you support. Please check the README.md for more 16 | information on column positions. 17 | */ 18 | var NUMBER_OF_LANGUAGES = 1; 19 | 20 | /* 21 | The script expects two columns for iOS and Android identifiers, respectively, 22 | and a column after that with all of the string values. This is the position of 23 | the iOS column. 24 | */ 25 | var FIRST_COLUMN_POSITION = 1; 26 | 27 | /* 28 | The position of the header containing the strings "Identifier iOS" and "Identifier Android" 29 | */ 30 | var HEADER_ROW_POSITION = 1; 31 | 32 | /* 33 | True if iOS output should contain a `Localizable` `enum` that contains all of 34 | the keys as string constants. 35 | */ 36 | var IOS_INCLUDES_LOCALIZABLE_ENUM = true; 37 | 38 | 39 | // Constants 40 | 41 | var LANGUAGE_IOS = 'iOS'; 42 | var LANGUAGE_ANDROID = 'Android'; 43 | var DEFAULT_LANGUAGE = LANGUAGE_IOS; 44 | 45 | 46 | // Export 47 | 48 | function onOpen() { 49 | var ui = SpreadsheetApp.getUi(); 50 | ui.createMenu('Custom Export') 51 | .addItem('iOS', 'exportForIos') 52 | .addItem('Android', 'exportForAndroid') 53 | .addToUi(); 54 | } 55 | 56 | function exportForIos() { 57 | var e = { 58 | parameter: { 59 | language: LANGUAGE_IOS 60 | } 61 | }; 62 | exportSheet(e); 63 | } 64 | 65 | function exportForAndroid() { 66 | var e = { 67 | parameter: { 68 | language: LANGUAGE_ANDROID 69 | } 70 | }; 71 | exportSheet(e); 72 | } 73 | 74 | /* 75 | Fetches the active sheet, gets all of the data and displays the 76 | result strings. 77 | */ 78 | function exportSheet(e) { 79 | 80 | var ss = SpreadsheetApp.getActiveSpreadsheet(); 81 | var sheet = ss.getActiveSheet(); 82 | var rowsData = getRowsData_(sheet, getExportOptions(e)); 83 | 84 | var strings = []; 85 | for (var i = 0; i < NUMBER_OF_LANGUAGES; i++) { 86 | strings.push(makeString(rowsData, i, getExportOptions(e))); 87 | } 88 | return displayTexts_(strings); 89 | } 90 | 91 | function getExportOptions(e) { 92 | 93 | var options = {}; 94 | options.language = e && e.parameter.language || DEFAULT_LANGUAGE; 95 | return options; 96 | } 97 | 98 | 99 | // UI Elements 100 | 101 | function makeLabel(app, text, id) { 102 | var lb = app.createLabel(text); 103 | if (id) lb.setId(id); 104 | return lb; 105 | } 106 | 107 | function makeListBox(app, name, items) { 108 | var listBox = app.createListBox().setId(name).setName(name); 109 | listBox.setVisibleItemCount(1); 110 | 111 | var cache = CacheService.getPublicCache(); 112 | var selectedValue = cache.get(name); 113 | Logger.log(selectedValue); 114 | for (var i = 0; i < items.length; i++) { 115 | listBox.addItem(items[i]); 116 | if (items[1] == selectedValue) { 117 | listBox.setSelectedIndex(i); 118 | } 119 | } 120 | return listBox; 121 | } 122 | 123 | function makeButton(app, parent, name, callback) { 124 | var button = app.createButton(name); 125 | app.add(button); 126 | var handler = app.createServerClickHandler(callback).addCallbackElement(parent);; 127 | button.addClickHandler(handler); 128 | return button; 129 | } 130 | 131 | function makeTextBox(id, content) { 132 | var textArea = ''; 133 | return textArea; 134 | } 135 | 136 | function displayTexts_(texts) { 137 | 138 | var app = HtmlService.createHtmlOutput().setWidth(800).setHeight(600); 139 | 140 | for (var i = 0; i < texts.length; i++) { 141 | app.append(makeTextBox("export_" + i, texts[i])) 142 | } 143 | 144 | SpreadsheetApp.getUi().showModalDialog(app, "Translations"); 145 | 146 | return app; 147 | } 148 | 149 | 150 | // Creating iOS and Android strings 151 | 152 | function makeString(object, textIndex, options) { 153 | switch (options.language) { 154 | case LANGUAGE_ANDROID: 155 | return makeAndroidString(object, textIndex, options); 156 | break; 157 | case LANGUAGE_IOS: 158 | return makeIosString(object, textIndex, options); 159 | break; 160 | default: 161 | break; 162 | } 163 | } 164 | 165 | /* 166 | Creates the strings.xml file for Android. 167 | */ 168 | function makeAndroidString(object, textIndex, options) { 169 | 170 | var exportString = ""; 171 | var prevIdentifier = ""; 172 | 173 | exportString = '' + "\n"; 174 | exportString += "\n"; 175 | 176 | for(var i=0; i' + "\n"; 193 | prevIdentifier = ""; 194 | } 195 | 196 | if(identifier.indexOf("[]")>0) { 197 | 198 | if(identifier != prevIdentifier) { 199 | exportString += "\t" + '' + "\n"; 200 | } 201 | 202 | exportString += "\t\t"+''+o.text+'' + "\n"; 203 | prevIdentifier = identifier; 204 | 205 | } else { 206 | exportString += "\t"+''+text+'' + "\n"; 207 | } 208 | } 209 | 210 | exportString += ""; 211 | 212 | return exportString; 213 | } 214 | 215 | /* 216 | Creates the Localizable.strings file and a Localizable enum for iOS. 217 | */ 218 | function makeIosString(object, textIndex, options) { 219 | 220 | var exportString = ""; 221 | 222 | if (IOS_INCLUDES_LOCALIZABLE_ENUM) { 223 | 224 | exportString += "// MARK: - Localizable enum\n\n" 225 | 226 | exportString += "enum Localizable {\n\n" 227 | 228 | for(var i=0; i 0) { 292 | keys.push(key); 293 | } 294 | } 295 | return keys; 296 | } 297 | 298 | /* 299 | Converts a header string into a camelCase string. 300 | - returns a string in camelCase 301 | */ 302 | function normalizeHeader(header) { 303 | var key = ""; 304 | var upperCase = false; 305 | for (var i = 0; i < header.length; ++i) { 306 | var letter = header[i]; 307 | if (letter == " " && key.length > 0) { 308 | upperCase = true; 309 | continue; 310 | } 311 | if (!isAlnum_(letter)) { 312 | continue; 313 | } 314 | if (key.length == 0 && isDigit_(letter)) { 315 | continue; // first character must be a letter 316 | } 317 | if (upperCase) { 318 | upperCase = false; 319 | key += letter.toUpperCase(); 320 | } else { 321 | key += letter.toLowerCase(); 322 | } 323 | } 324 | return key; 325 | } 326 | 327 | /* 328 | Gets all of the data from the sheet. 329 | - returns an array of objects containing all the necessary data for display. 330 | */ 331 | function getRowsData_(sheet, options) { 332 | 333 | var dataRange = sheet.getRange(HEADER_ROW_POSITION + 1, FIRST_COLUMN_POSITION, sheet.getMaxRows(), sheet.getMaxColumns()); 334 | var headers = getNormalizedHeaders(sheet, options); 335 | var objects = getObjects(dataRange.getValues(), headers); 336 | 337 | return objects; 338 | } 339 | 340 | /* 341 | Gets the objects for the cell data. For each cell, the keys are the headers and the value is the 342 | data inside the cell. 343 | - returns: an array of objects with data for displaying the final string 344 | */ 345 | function getObjects(data, keys) { 346 | 347 | var objects = []; 348 | 349 | for (var i = 0; i < data.length; ++i) { 350 | 351 | var object = { 352 | "texts": [] 353 | }; 354 | 355 | var hasData = false; 356 | 357 | for (var j = 0; j < data[i].length; ++j) { 358 | 359 | var cellData = data[i][j]; 360 | if (isCellEmpty_(cellData)) { 361 | cellData = ""; 362 | } 363 | 364 | if (keys[j] != "identifierIos" && keys[j] != "identifierAndroid") { 365 | object["texts"].push(cellData); 366 | } else { 367 | object[keys[j]] = cellData; 368 | } 369 | hasData = true; 370 | } 371 | if (hasData) { 372 | objects.push(object); 373 | } 374 | } 375 | return objects; 376 | } 377 | 378 | 379 | // Utils 380 | 381 | // Returns true if the cell where cellData was read from is empty. 382 | // Arguments: 383 | // - cellData: string 384 | function isCellEmpty_(cellData) { 385 | return typeof(cellData) == "string" && cellData == ""; 386 | } 387 | 388 | // Returns true if the character char is alphabetical, false otherwise. 389 | function isAlnum_(char) { 390 | return char >= 'A' && char <= 'Z' || 391 | char >= 'a' && char <= 'z' || 392 | isDigit_(char); 393 | } 394 | 395 | // Returns true if the character char is a digit, false otherwise. 396 | function isDigit_(char) { 397 | return char >= '0' && char <= '9'; 398 | } 399 | 400 | // Given a JavaScript 2d Array, this function returns the transposed table. 401 | // Arguments: 402 | // - data: JavaScript 2d Array 403 | // Returns a JavaScript 2d Array 404 | // Example: arrayTranspose([[1,2,3],[4,5,6]]) returns [[1,4],[2,5],[3,6]]. 405 | function arrayTranspose_(data) { 406 | if (data.length == 0 || data[0].length == 0) { 407 | return null; 408 | } 409 | 410 | var ret = []; 411 | for (var i = 0; i < data[0].length; ++i) { 412 | ret.push([]); 413 | } 414 | 415 | for (var i = 0; i < data.length; ++i) { 416 | for (var j = 0; j < data[i].length; ++j) { 417 | ret[j][i] = data[i][j]; 418 | } 419 | } 420 | 421 | return ret; 422 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # localizable-sheet-script 2 | A Google Sheets script that will take a sheet in a specific format and return iOS and Android localization files. 3 | 4 | ## What it does 5 | 6 | For Android it creates an XML resources file with all of the strings. For iOS it creates a Localizable enum with String constants, and a .strings file. 7 | 8 | ## Installing 9 | 10 | 1. Open your sheet. 11 | 2. Go to **Tools -> Script Editor** 12 | 3. Copy `Code.js`, make your edits if needed, and **Save**. 13 | 14 | ## Usage 15 | 16 | 1. Open your sheet. 17 | 2. Go to **Custom Export** and select your **iOS** or **Android**. 18 | 19 | ## Sheet format 20 | 21 | The script expects the sheet to be formatted in a specific way. 22 | 23 | | ... arbitrary number of columns before iOS keys | **Identifier iOS** | **Identifier Android** | English text | German text | ... | 24 | |-------------------------------------------------|--------------------|------------------------|--------------|-------------|-----| 25 | | place whatever you want in here | login_button_title | login_button_title | Login | Einloggen | | 26 | | | ... | ... | ... | ... | | 27 | 28 | **The texts in bold cannot be changed!** The script depends on them to know which identifier is which. The other texts don't matter. 29 | 30 | The **first row** must **always** contain headers, and not the actual strings. 31 | 32 | The number of languages depends on the `NUMBER_OF_LANGUAGES` variable in the script, and new languages can be added by adding a new column on the right and incrementing that number in the script. 33 | 34 | The position of the first (iOS) column that is relevant to the script is changed with the `FIRST_COLUMN_POSITION` variable in the script. By default it's `1` (the first column). 35 | 36 | ## Configuring 37 | 38 | - `NUMBER_OF_LANGUAGES`: The number of language columns to use. 39 | - `FIRST_COLUMN_POSITION`: The position of the iOS identifiers (the first column relevant to the script). Starting from 1. 40 | - `IOS_INCLUDES_LOCALIZABLE_ENUM`: Whether or not to create an `Localizable` `enum` containing all of the keys as `static let` constants. 41 | 42 | ## Exported files 43 | 44 | The exported files are the standard format (`strings.xml` or `Localizable.strings`) for the specific platforms. iOS also includes a `Localizable` `enum` which contains all of the keys as `static let` properties for code-completion and less typos.  45 | 46 | ## License: MIT 47 | 48 | Created by COBE http://cobeisfresh.com/ 49 | Copyright 2017 COBE 50 | 51 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 52 | 53 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 54 | 55 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 56 | --------------------------------------------------------------------------------