-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
138 lines (131 loc) · 5.74 KB
/
code.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
figma.showUI(__html__, { visible: true });
figma.ui.onmessage = (translationDict) => __awaiter(this, void 0, void 0, function* () {
console.log('translation dict from file', translationDict);
let nameToTranslation = function (key, locale) {
let adaptedLocale = locale
if (adaptedLocale === 'universal') {
return null;
}
const languageCleaner = {'no': 'nb'}
adaptedLocale = languageCleaner[adaptedLocale] || adaptedLocale
// console.log(name, translationId, locale)
if (!(adaptedLocale in translationDict)) {
// console.info('Warning: locale not found', adaptedLocale);
let splitLocale = adaptedLocale.split('-');
if (splitLocale.length > 1) {
adaptedLocale = splitLocale[0];
if (!(adaptedLocale in translationDict)) {
return null;
} else {
console.log('adapted locale: ' + adaptedLocale);
}
} else {
console.warn(`This locale ${adaptedLocale} is not available in the dict of all locales`)
return null;
}
}
let allTranslations = translationDict[adaptedLocale];
if (!(key in allTranslations)) {
console.log('ERROR: translation not found', adaptedLocale, key);
return "";
}
return allTranslations[key];
};
function setTextContent(textNode, text, locale) {
var font = null;
var alertOnce = false;
const specialFonts = ['zh-Hans','zh-Hant','he','ar','ja','ko','thai']
if (typeof textNode.fontName != 'symbol') {
console.log('locale', locale)
if (specialFonts.includes(locale)) {
let textStyles = figma.getLocalTextStyles()
console.log('textStyles', textStyles)
for (const textStyle of textStyles) {
console.log('style', 'screenshot_title_' + locale);
if (textStyle.name === 'screenshot_title_' + locale){
console.log('loaded style', textStyle);
figma.loadFontAsync(textStyle.fontName).then(() => {
textNode.textStyleId = textStyle.id
if (text != null) {
textNode.characters = text;
}
});
}
}
} else {
font = textNode.fontName;
console.log('font', font)
figma.loadFontAsync(font).then(() => {
if (text != null) {
textNode.characters = text;
}
});
}
// font = textNode.fontName;
}
else {
if (!alertOnce) {
// alertOnce = true
console.log('Plugin cannot modify text layers with mixed font properties for ' + textNode.characters);
}
}
};
const isIterable = (value) => {
return Symbol.iterator in Object(value);
}
let page = figma.currentPage;
let locale = ''
function traverse(node) {
console.log('traverse: ' + node.type);
// console.log("At node ", node.name)
if (node.name === 'Info') {
return
}
if (node.type === "FRAME") {
let paths = node.name.split('/');
if (paths.length > 2) {
locale = paths[paths.length-2];
//android frame names are longer
if (paths.length > 5) {
locale = paths[paths.length-5];
}
console.log('locale: ' + locale);
const textNodes = node.findAll(nodeInFrame => nodeInFrame.type === "TEXT");
console.log("found text nodes", textNodes.length)
for (const textNode of textNodes) {
let translationKey = textNode.name;
console.log('characters: ' + translationKey);
if (translationKey.charAt(0) === '#') {
let key = translationKey.toLowerCase()
console.log('key: ' + key + ' locale:' + locale);
let translation = nameToTranslation(key, locale);
console.log("after nameToTranslation",)
if (translation != null) {
console.log("Setting autoRename to false", textNode);
textNode.autoRename = false
setTextContent(textNode, translation, locale);
console.log('translation set: ' + translation);
}
}
}
}
}
if (node.type !== "INSTANCE") {
if (isIterable(node.children)) {
for (const child of node.children) {
traverse(child);
}
}
}
}
traverse(page); // start the traversal at the PAGE
});