You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a Google spreadsheet like the following example:
KEY
EN
ES
CA
TAGS
COMMENTS
// Core
core_accept
Accept
Aceptar
D'acord
android,ios,core
-
core_cancel
Cancel
Cancelar
Cancel·lar
android,ios,core
-
core_ok
OK
OK
OK
ios,core
-
// Login
login_email_hint
Enter your email
Introduce tu email
Introdueix el teu email
android,ios,login
-
login_pasword_hint
Enter your password
Intruduce tu contraseña
Introdueix la teva contrasenya
android,ios,login
-
Follow the Twine format on the language translations columns.
Create a script with the snippet code.
Open the spreadsheet.
Select the tab with the localized strings.
Select the "Localization" menu option at the left of the "Help" menu option.
Select "Copy and Export" option.
Copy and paste the content on your twine file.
If you prefer to download the file generated, go to your spreadsheet folder. On this folder, you can see the "temp" folder where you can download the Twine file generated.
Clarifications:
Sections are marked on "KEY" spreadsheet column like a comment "//".
Empty translation cells are generated empty.
You can generate inclusive and exclusive localized strings using tags with Twine (read the documentation).
Comments are not included in generated Twine file/content.
How to add a new language:
Add the new language column between the last language and "TAGS" column on the spreadsheet.
Go to the spreadsheet script.
Update "COLUMNS" variable with the same order of spreadsheet.
Update the "COLUMN_POSITION_TAGS" and "COLUMN_POSITION_COMMENTS" position.
Save the script.
You are ready!
Google Spreadsheets script:
// **************************************************************************/* * VARS TO UPDATE when a new language is added * * MANDATORY for COLUMNS var: * "KEY" always the first column and "TAGS" and "COMMENTS" the last columns */varCOLUMNS=["key","en","fr","es","pt-PT","tags","comments"];varCOLUMN_POSITION_TAGS=COLUMNS.length-2;varCOLUMN_POSITION_COMMENTS=COLUMN_POSITION_TAGS+1;// **************************************************************************varCOLUMN_POSITION_KEY=0;varTEMP_FOLDER_NAME="temp";varTWINE_FILENAME="twine.txt";varMARKER_SECTION="//"varKEYWORD_TWINE_TAGS="tags";varKEYWORD_COMMENT_TAGS="comment";varFILE_CONTENT=""functiononOpen(){varapp=SpreadsheetApp.getActiveSpreadsheet();varmenuEntries=[{name: "Copy and Export",functionName: "init"}];app.addMenu("Localization",menuEntries);}functioninit(){try{varsheetData=getData();if(checkColumns(sheetData)){varisFileParsed=parseFileContent(sheetData)if(isFileParsed){varfilename=createFile(FILE_CONTENT);copyAndExportDialog(FILE_CONTENT);}}}catch(error){Logger.log(error);showErrorDialog(error);}}functiongetData(){varactiveSpreadsheet=SpreadsheetApp.getActiveSpreadsheet();varsheet=activeSpreadsheet.getActiveSheet();varactiveRange=sheet.getDataRange();vardata=activeRange.getValues();returndata;}functioncheckColumns(fileData){if(fileData[0][0].toLowerCase()==COLUMNS[0].toLowerCase()&&fileData[0][1].toLowerCase()==COLUMNS[1].toLowerCase()&&fileData[0][2].toLowerCase()==COLUMNS[2].toLowerCase()&&fileData[0][3].toLowerCase()==COLUMNS[3].toLowerCase()&&fileData[0][4].toLowerCase()==COLUMNS[4].toLowerCase()&&fileData[0][5].toLowerCase()==COLUMNS[5].toLowerCase()){returntrue;}varerrorColumns="<p><b>Do you have selected the tab with the localized strings?</b></p>";errorColumns+="<p>If the error persist, check the columns names on spreadsheet or update the script variables.</p>";showErrorDialog(errorColumns);Logger.log(errorColumns);returnfalse;}functionparseFileContent(fileData){vari;varj;varnumberOfRows=fileData.length;varnumberOfColumns=COLUMNS.length;// We skipt postion 0 because this row has the headersfor(i=1;i<numberOfRows;i++){for(j=0;j<numberOfColumns;j++){varcolumnName=COLUMNS[j]varcell=fileData[i][j].trim();// skip empty cell if(j===0&&cell===""){break;}if(j===0&&cell.indexOf(MARKER_SECTION)==0){// sectionaddSeparator();addSection(cell.replace(MARKER_SECTION,"").trim());break;}else{if(j===COLUMN_POSITION_KEY&&cell!=""){// keyaddKey(cell)}elseif(j===COLUMN_POSITION_TAGS&&cell!=""){// tagsaddTags(cell);}elseif(j===COLUMN_POSITION_COMMENTS&&cell!=""){// commentsaddComment(cell);}else{// translationsaddTranslation(columnName,cell);}}}}Logger.log("Twine content:"+FILE_CONTENT);returntrue;}functionaddSection(sectionName){FILE_CONTENT+="[["+sectionName+"]]\n";}functionaddKey(keyName){FILE_CONTENT+="\t["+keyName+"]\n";}functionaddTranslation(language,translation){FILE_CONTENT+="\t\t"+language+" = "+translation+"\n";}functionaddTags(tags){FILE_CONTENT+="\t\t"+KEYWORD_TWINE_TAGS+" = "+tags+"\n";}functionaddComment(comment){FILE_CONTENT+="\t\t"+KEYWORD_COMMENT_TAGS+" = "+comment+"\n";}functionaddSeparator(){FILE_CONTENT+="\n";}functioncreateFile(content){varthisFileId=SpreadsheetApp.getActive().getId();varthisFile=DriveApp.getFileById(thisFileId);varparentFolder=thisFile.getParents().next();varisTempFolderCreated=getFolder(parentFolder,TEMP_FOLDER_NAME);if(isTempFolderCreated===false){parentFolder.createFolder(TEMP_FOLDER_NAME);Logger.log("Temp folder created");}else{removeFolder(parentFolder,TEMP_FOLDER_NAME);Logger.log("Temp folder removed");parentFolder.createFolder(TEMP_FOLDER_NAME);Logger.log("Temp folder created");}vartempFolder=getFolder(parentFolder,TEMP_FOLDER_NAME);varfileName=getFileName(newDate())tempFolder.createFile(fileName,content);Logger.log("File created");returnfileName}functiongetFolder(parentFolder,folderName){varfolders=parentFolder.getFolders();while(folders.hasNext()){varfolder=folders.next();if(folderName==folder.getName()){returnfolder;}}returnfalse;}functionremoveFolder(parentFolder,folderName){varfolders=parentFolder.getFolders();while(folders.hasNext()){varfolder=folders.next();if(folderName==folder.getName()){parentFolder.removeFolder(folder)returntrue;}}returnfalse;}functiongetFileName(d){varfileName=d.getFullYear();fileName+="_"if(d.getMonth()+1<10){fileName+='0'+(d.getMonth()+1)}else{fileName+=''+(d.getMonth()+1)}fileName+="_"if(d.getDate()<10){fileName+='0'+d.getDate()}else{fileName+=''+d.getDate()}fileName+="__"if(d.getHours()<10){fileName+='0'+d.getHours()}else{fileName+=''+d.getHours()}fileName+="_"if(d.getMinutes()<10){fileName+='0'+d.getMinutes()}else{fileName+=''+d.getMinutes()}fileName+="_"+TWINE_FILENAMEreturnfileName;}functioncopyAndExportDialog(content){varwidth=700;varheight=500;varmessageHeight=(height-60)+"px";vartwineLink="<a href=\"https://github.com/scelis/twine\" target=\"_blank\">Twine</a>";varmessage="<p>Use "+twineLink+" command line tool to manage your strings and their translations.</p>";message+="<textarea readonly style=\"width:100%;height:"+messageHeight+";\">"+content+"</textarea>";varhtml=HtmlService.createHtmlOutput(message).setWidth(width).setHeight(height);SpreadsheetApp.getUi().showModalDialog(html,'📝 Copy and Export');}functionshowErrorDialog(error){varerrorMessage="<p>"+error+"</p>"showDialog("ERROR",errorMessage)}functionshowDialog(title,message){varhtmlOutput=HtmlService.createHtmlOutput(message).setWidth(300).setHeight(250);SpreadsheetApp.getUi().showModalDialog(htmlOutput,title);}