-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRANSLATE.gs
29 lines (29 loc) · 939 Bytes
/
TRANSLATE.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
/**
* Translate from one language to another.
*
* @param {"Hello World"} str The string to translate.
* @param {"fr"} to The target language.
* @param {"en"} from Optional. The source language.
*/
function TRANSLATE(str,to,from){
var locale = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetLocale().substring(0,SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetLocale().indexOf("_")).toLowerCase();
to = to ? to : locale;
if(from==undefined || from==null){
from = "";
}
var key = from + "-" + to + "-" + encodeURIComponent(str);
if(str?true:false){
var cache = CacheService.getDocumentCache();
var cached = cache.get(key);
if(cached?true:false){
return cached;
}else{
var value = LanguageApp.translate(str, from, to);
cache.put(key, value,21600);
return value;
}
}else{
var msg = "'str' is required.";
throw new Error(LanguageApp.translate(msg,locale=="en"?"":"en",locale));
}
}