-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranslations.js
More file actions
256 lines (206 loc) · 7.07 KB
/
translations.js
File metadata and controls
256 lines (206 loc) · 7.07 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Reads settings_schema.json and all section setting JSON files
* Outputs en.default.schema.json
* Replaces relevant fields with their "t:" translation path in all setting JSON files
*/
const glob = require('glob');
const fs = require('fs');
const translateableProps = [
'info',
'content',
'label',
'name',
'group',
'placeholder',
'category',
'unit'
];
const ignoreStrings = [
'Behance',
'Clubhouse',
'Discord',
'Dribbble',
'Facebook',
'Flickr',
'Houzz',
'Instagram',
'Kickstarter',
'LinkedIn',
'Messenger',
'OpenSea',
'Pinterest',
'Reddit',
'RSS',
'Snapchat',
'Spotify',
'TikTok',
'Tumblr',
'Twitch',
'Twitter',
'Vimeo',
'WhatsApp',
'YouTube',
'https://behance.net/',
'https://www.clubhouse.com/',
'https://discord.com/',
'https://dribbble.com/shopify',
'https://facebook.com/shopify',
'https://www.flickr.com/',
'https://www.houzz.com/',
'https://instagram.com/shopify',
'https://www.kickstarter.com/',
'https://linkedin.com/company/shopify',
'https://medium.com/',
'https://www.messenger.com/',
'https://opensea.io/',
'https://pinterest.com/shopify',
'https://www.reddit.com/r/shopify/',
'https://www.shopify.com/content-services/feeds/ecommerce.atom',
'https://www.snapchat.com/',
'https://www.spotify.com/',
'https://tiktok.com/@shopify',
'https://shopify.tumblr.com/',
'https://www.twitch.tv/',
'https://twitter.com/shopify',
'https://vimeo.com/',
'https://www.whatsapp.com/',
'https://youtube.com/user/shopify',
];
const enSchema = {
sections: {},
settings_schema: {},
};
const sectionSettingsFiles = glob.sync('./source/sections/**/*.json');
const settingsSchemaFile = './source/config/settings_schema.json';
function processSettings(content, translationPath, sectionKey, blockName = null) {
let headerCount = 0;
let paragraphCount = 0;
const updatedSettings = content.map(setting => {
let settingId;
if (setting.id) {
settingId = setting.id;
} else if (setting.type === 'header') {
// if the setting does not have an ID, it is a header or a paragraph setting
headerCount += 1;
settingId = `header_${headerCount}`;
} else if (setting.type === 'paragraph') {
paragraphCount += 1;
settingId = `paragraph_${paragraphCount}`;
}
let localSchema = enSchema.sections;
if (translationPath.includes('settings_schema')) {
localSchema = enSchema.settings_schema;
}
if (blockName) {
localSchema[sectionKey].blocks[blockName][settingId] = {};
} else {
localSchema[sectionKey][settingId] = {};
}
Object.keys(setting).forEach(settingProp => {
const settingValue = setting[settingProp];
if (translateableProps.includes(settingProp) && !ignoreStrings.includes(settingValue) && settingValue.substring(0, 2) !== 't:') {
if (blockName) {
localSchema[sectionKey].blocks[blockName][settingId][settingProp] = settingValue;
} else {
localSchema[sectionKey][settingId][settingProp] = settingValue;
}
setting[settingProp] = `${translationPath}.${settingId}.${settingProp}`;
}
});
if (setting.options) {
let optionCount = 0;
setting.options.forEach(option => {
Object.keys(option).forEach(settingProp => {
const settingValue = option[settingProp];
if (translateableProps.includes(settingProp) && !ignoreStrings.includes(option[settingValue]) && settingValue.substring(0, 2) !== 't:') {
optionCount += 1;
const optionSettingProp = `option_${optionCount}`;
if (blockName) {
localSchema[sectionKey].blocks[blockName][settingId][optionSettingProp] = settingValue;
} else {
localSchema[sectionKey][settingId][optionSettingProp] = settingValue;
}
option[settingProp] = `${translationPath}.${settingId}.${optionSettingProp}`;
}
});
});
}
if (translationPath.includes('settings_schema')) {
enSchema.settings_schema = localSchema;
} else {
enSchema.sections = localSchema;
}
return setting;
});
return updatedSettings;
}
sectionSettingsFiles.forEach(filePath => {
const fileContent = JSON.parse(fs.readFileSync(filePath));
const sectionKey = fileContent.name.replace(/\s+/g, '_').toLowerCase();
enSchema.sections[sectionKey] = {
name: fileContent.name,
};
const translationPath = `t:sections.${sectionKey}`;
fileContent.name = `${translationPath}.name`;
if (fileContent.settings.length) {
fileContent.settings = processSettings(fileContent.settings, translationPath, sectionKey, null);
// TODO: Account for blocks, presets
if (fileContent.blocks) {
enSchema.sections[sectionKey].blocks = {};
const tempBlocks = fileContent.blocks;
const blockTranslationPath = `${translationPath}.blocks`
const updatedBlocks = tempBlocks.map(block => {
if (block.name) {
const blockName = block.name.replace(/ /g, '_').toLowerCase();
enSchema.sections[sectionKey].blocks[blockName] = {
name: block.name
};
block.name = `${blockTranslationPath}.${blockName}.name`;
if (block.settings) {
const blockSettingTranslationPath = `${blockTranslationPath}.${blockName}`;
const blockSettings = processSettings(block.settings, blockSettingTranslationPath, sectionKey, blockName);
block.settings = blockSettings;
}
}
return block;
})
fileContent.blocks = updatedBlocks;
}
if (fileContent.presets) {
const presets = fileContent.presets;
enSchema.sections[sectionKey].presets = {};
updatedPresets = presets.map(preset => {
const presetName = preset.name.replace(/ /g, '_').toLowerCase();
enSchema.sections[sectionKey].presets[presetName] = {
name: preset.name,
category: preset.category,
};
preset.name = `${translationPath}.presets.${presetName}.name`;
preset.category = `${translationPath}.presets.${presetName}.category`;
return preset;
});
fileContent.presets = updatedPresets;
}
}
const fileContentString = JSON.stringify(fileContent, null, 2);
fs.writeFileSync(filePath, fileContentString);
});
// config/settings_schema
fs.readFileSync(settingsSchemaFile)
const configFileContent = JSON.parse(fs.readFileSync(settingsSchemaFile));
const updatedConfigFile = configFileContent.map(group => {
if (group.settings) {
const groupKey = group.name.replace(/ /g, '_').toLowerCase();
const schemaTranslationPath =`t:settings_schema.${groupKey}`;
enSchema.settings_schema[groupKey] = {
name: group.name
};
const updatedSettings = processSettings(group.settings, schemaTranslationPath, groupKey);
group.settings = updatedSettings;
}
return group;
});
const configFileString = JSON.stringify(updatedConfigFile, null, 2);
fs.writeFileSync(settingsSchemaFile, configFileString);
const schemaString = JSON.stringify(enSchema, null, 2);
fs.writeFileSync('./en.default.schema.json', schemaString);