-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.gs
120 lines (93 loc) · 3.46 KB
/
code.gs
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
/**
*
* Created by Abdullah Alhaider.
* Mon, 10/05/2021.
*
*/
/**
* Translate the input to given language code.
*
* @param {text} input The value to translate.
* @param {targetLanguage} like ar, en
* @return The input translated to given langauge.
* @customfunction
*/
function translate(text, targetLanguage) {
return Array(text).length > 0 ? LanguageApp.translate(text, 'en', targetLanguage) : '';
}
/**
* iOSLocalization.
*
* @param {localizationKey} input The value for the key.
* @param {localizedValue} the localized value.
* @return raw value for the localization.
* @customfunction
*/
function iOSLocalization(localizationKey, localizedValue) {
if (localizationKey) {
var raw = '\n' + '\"' + localizationKey + '\" = \"' + localizedValue + '\";';
return raw;
}
return null;
}
/**
* Create UUID string.
*
* @return raw value for UUID object.
* @customfunction
*/
function createUUID() {
var uuid = Utilities.getUuid();
return uuid
}
/**
* Fetch the the sheet and start creating root folder and sub folders for each lang with Localization.strings file
*
*/
function exportLocalizationsFiles() {
var startDate = new Date();
Logger.log('🗂 Starting exporting localization files, time: ' + startDate.toString());
var app = SpreadsheetApp;
var spreadsheet = app.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Localizable');
var columnsCount = sheet.getRange('A:AF').getNumColumns() ;
var locations = [...Array(columnsCount).keys()].map(column => columnToLetter(column + 1));
var languages = sheet.getRange(locations[0] + '1:' + locations[locations.length - 1] + '1').getValues()[0];
var languageCodes = languages.map(value => value.substring(value.lastIndexOf("(") + 1, value.lastIndexOf(")")))
Logger.log("languageCodes == locations => " + (languageCodes.length == locations.length));
var rootFolderName = 'Localization Files - ' + startDate.toString();
var rootFolder = DriveApp.createFolder(rootFolderName);
var rootFolderURL = rootFolder.getUrl();
Logger.log('👉 Created root folder with name: ' + rootFolderName + '\n👉 Root folder URL => ' + rootFolderURL);
for(index = 0; index < locations.length; index++) {
var range = locations[index] + '2:' + locations[index] + '150';
var fileContent = sheet.getRange(range).getValues().join('\n\n');
var subFolder = rootFolder.createFolder(languageCodes[index] + '.lproj');
Logger.log('Created folder for ' + languageCodes[index].toUpperCase());
subFolder.createFile('Localizable.strings', fileContent);
Logger.log('Remaining is ' + ((locations.length - 1) - index) + '...');
}
var endDate = new Date();
var timeDiff = startDate.getSeconds() + endDate.getSeconds();
Logger.log('✅ DONE in : ' + timeDiff.toString() + ' second' + ', folder URL ==> ' + rootFolderURL);
}
function columnToLetter(column) {
var temp, letter = '';
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
function letterToColumn(letter) {
var column = 0, length = letter.length;
for (var i = 0; i < length; i++) {
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
// ----------------------------------------------------------------------------------------------------------------- //
String.prototype.isEmpty = function() {
return (this.length === 0 || !this.trim());
};