4y ago
Loading comments...
Translate your sketch files automagically
If you are someone like me who uses sketch on a daily basis be it for prototypes or designing for products for Bharat use case like swasth.app, then ability to demo apps/prototypes in local languages helps cut through a lot of noise and questions.
It also helps in ensuring you design for content that's not just in English but other Indian languages as well. I wrote a simple sketch plugin that will fetch translations of your current prototype and helps you visualize without having to manually copy paste and format etc.
sketch-plugin-translate.js
sketch-fetch-google-translate.js
1 export default function () {
2 const doc = sketch.getSelectedDocument()
3 const selectedLayers = doc.selectedLayers
4 const selectedCount = selectedLayers.length
5 let values;
6 if (selectedCount === 0) {
7 sketch.UI.message('No layers are selected.')
8 } else {
9 UI.getInputFromUser(
10 "What language should we translate to?",
11 {
12 type: UI.INPUT_TYPE.selection,
13 possibleValues: ['Arabic,ar',
14 'Bengali,bn',
15 'English,en',
16 'Gujarati,gu',
17 'Hindi,hi',
18 'Japanese,ja',
19 'Korean,ko',
20 'Malayalam,ml',
21 'Marathi,mr',
22 'Odia (Oriya),or',
23 'Pashto,ps',
24 'Punjabi,pa',
25 'Sindhi,sd',
26 'Tamil,ta',
27 'Telugu,te',
28 'Urdu,ur'],
29 description: "Full list of languages https://cloud.google.com/translate/docs/languages"
30 },
31 (err, value) => {
32 if (err) {
33 // most likely the user canceled the input
34 return
35 }
36 selectedLayers.forEach(function (layer) {
37 // let source_text = layer.text
38 let translated_text = fetch_lang_prefs(layer, locale); // fetch translated strings from google translate API
39 UI.message(`Updated ${selectedLayers.length} text layers with ${value.split(",")[0]} language`)
40 }
41 )
42 }
43 )
44
45 }
46 }
47
1 const google_translate_fetch_translation_endpoint = "https://translation.googleapis.com/language/translate/v2?key=$YOUR_API_KEY";
2 export function fetch_translated_text(layer, locale) {
3
4 let data = {
5 "q": layer.text, //current value in layer
6 "target": locale, // target locale code
7 "source": "en" // I'm defaulting to english as source, you can always add a new UI.Selector and capture source lg as well
8 }
9
10 //construct body
11 let options = {
12 method: "POST",
13 body: JSON.stringify(data),
14 headers: {
15 "Content-Type": "application/json"
16 }
17 }
18
19 fetch(google_translate_fetch_translation_endpoint, options).then(function (text) {
20 let res = (text.json())
21 let translated_text = parse_gapi_response_json(res) //API contract for google translate
22 layer.text = (translated_text)
23 })
24 .catch(e => console.error(e));
25
26
27 }
28
605 views
No comments yet.