locale.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*globals jQuery*/
  2. /*jslint vars: true, eqeq: true, forin: true*/
  3. /*
  4. * Localizing script for SVG-edit UI
  5. *
  6. * Licensed under the MIT License
  7. *
  8. * Copyright(c) 2010 Narendra Sisodya
  9. * Copyright(c) 2010 Alexis Deveria
  10. *
  11. */
  12. // Dependencies
  13. // 1) jQuery
  14. // 2) svgcanvas.js
  15. // 3) svg-editor.js
  16. var svgEditor = (function($, editor) {'use strict';
  17. var lang_param;
  18. function setStrings(type, obj, ids) {
  19. // Root element to look for element from
  20. var i, sel, val, $elem, elem, node, parent = $('#svg_editor').parent();
  21. for (sel in obj) {
  22. val = obj[sel];
  23. if (!val) {console.log(sel);}
  24. if (ids) {sel = '#' + sel;}
  25. $elem = parent.find(sel);
  26. if ($elem.length) {
  27. elem = parent.find(sel)[0];
  28. switch ( type ) {
  29. case 'content':
  30. for (i = 0; i < elem.childNodes.length; i++) {
  31. node = elem.childNodes[i];
  32. if (node.nodeType === 3 && node.textContent.replace(/\s/g,'')) {
  33. node.textContent = val;
  34. break;
  35. }
  36. }
  37. break;
  38. case 'title':
  39. elem.title = val;
  40. break;
  41. }
  42. } else {
  43. console.log('Missing: ' + sel);
  44. }
  45. }
  46. }
  47. editor.readLang = function(langData) {
  48. var more = editor.canvas.runExtensions("addlangData", lang_param, true);
  49. $.each(more, function(i, m) {
  50. if (m.data) {
  51. langData = $.merge(langData, m.data);
  52. }
  53. });
  54. // Old locale file, do nothing for now.
  55. if (!langData.tools) {return;}
  56. var tools = langData.tools,
  57. misc = langData.misc,
  58. properties = langData.properties,
  59. config = langData.config,
  60. layers = langData.layers,
  61. common = langData.common,
  62. ui = langData.ui;
  63. setStrings('content', {
  64. // copyrightLabel: misc.powered_by, // Currently commented out in svg-editor.html
  65. curve_segments: properties.curve_segments,
  66. fitToContent: tools.fitToContent,
  67. fit_to_all: tools.fit_to_all,
  68. fit_to_canvas: tools.fit_to_canvas,
  69. fit_to_layer_content: tools.fit_to_layer_content,
  70. fit_to_sel: tools.fit_to_sel,
  71. icon_large: config.icon_large,
  72. icon_medium: config.icon_medium,
  73. icon_small: config.icon_small,
  74. icon_xlarge: config.icon_xlarge,
  75. image_opt_embed: config.image_opt_embed,
  76. image_opt_ref: config.image_opt_ref,
  77. includedImages: config.included_images,
  78. largest_object: tools.largest_object,
  79. layersLabel: layers.layers,
  80. page: tools.page,
  81. relativeToLabel: tools.relativeTo,
  82. selLayerLabel: layers.move_elems_to,
  83. selectedPredefined: config.select_predefined,
  84. selected_objects: tools.selected_objects,
  85. smallest_object: tools.smallest_object,
  86. straight_segments: properties.straight_segments,
  87. svginfo_bg_url: config.editor_img_url + ":",
  88. svginfo_bg_note: config.editor_bg_note,
  89. svginfo_change_background: config.background,
  90. svginfo_dim: config.doc_dims,
  91. svginfo_editor_prefs: config.editor_prefs,
  92. svginfo_height: common.height,
  93. svginfo_icons: config.icon_size,
  94. svginfo_image_props: config.image_props,
  95. svginfo_lang: config.language,
  96. svginfo_title: config.doc_title,
  97. svginfo_width: common.width,
  98. tool_docprops_cancel: common.cancel,
  99. tool_docprops_save: common.ok,
  100. tool_source_cancel: common.cancel,
  101. tool_source_save: common.ok,
  102. tool_prefs_cancel: common.cancel,
  103. tool_prefs_save: common.ok,
  104. sidepanel_handle: layers.layers.split('').join(' '),
  105. tool_clear: tools.new_doc,
  106. tool_docprops: tools.docprops,
  107. tool_export: tools.export_img,
  108. tool_import: tools.import_doc,
  109. tool_imagelib: tools.imagelib,
  110. tool_open: tools.open_doc,
  111. tool_save: tools.save_doc,
  112. svginfo_units_rulers: config.units_and_rulers,
  113. svginfo_rulers_onoff: config.show_rulers,
  114. svginfo_unit: config.base_unit,
  115. svginfo_grid_settings: config.grid,
  116. svginfo_snap_onoff: config.snapping_onoff,
  117. svginfo_snap_step: config.snapping_stepsize,
  118. svginfo_grid_color: config.grid_color
  119. }, true);
  120. // Shape categories
  121. var o, cats = {};
  122. for (o in langData.shape_cats) {
  123. cats['#shape_cats [data-cat="' + o + '"]'] = langData.shape_cats[o];
  124. }
  125. // TODO: Find way to make this run after shapelib ext has loaded
  126. setTimeout(function() {
  127. setStrings('content', cats);
  128. }, 2000);
  129. // Context menus
  130. var opts = {};
  131. $.each(['cut','copy','paste', 'paste_in_place', 'delete', 'group', 'ungroup', 'move_front', 'move_up', 'move_down', 'move_back'], function() {
  132. opts['#cmenu_canvas a[href="#' + this + '"]'] = tools[this];
  133. });
  134. $.each(['dupe','merge_down', 'merge_all'], function() {
  135. opts['#cmenu_layers a[href="#' + this + '"]'] = layers[this];
  136. });
  137. opts['#cmenu_layers a[href="#delete"]'] = layers.del;
  138. setStrings('content', opts);
  139. setStrings('title', {
  140. align_relative_to: tools.align_relative_to,
  141. circle_cx: properties.circle_cx,
  142. circle_cy: properties.circle_cy,
  143. circle_r: properties.circle_r,
  144. cornerRadiusLabel: properties.corner_radius,
  145. ellipse_cx: properties.ellipse_cx,
  146. ellipse_cy: properties.ellipse_cy,
  147. ellipse_rx: properties.ellipse_rx,
  148. ellipse_ry: properties.ellipse_ry,
  149. fill_color: properties.fill_color,
  150. font_family: properties.font_family,
  151. idLabel: properties.id,
  152. image_height: properties.image_height,
  153. image_url: properties.image_url,
  154. image_width: properties.image_width,
  155. layer_delete: layers.del,
  156. layer_down: layers.move_down,
  157. layer_new: layers['new'],
  158. layer_rename: layers.rename,
  159. layer_moreopts: common.more_opts,
  160. layer_up: layers.move_up,
  161. line_x1: properties.line_x1,
  162. line_x2: properties.line_x2,
  163. line_y1: properties.line_y1,
  164. line_y2: properties.line_y2,
  165. linecap_butt: properties.linecap_butt,
  166. linecap_round: properties.linecap_round,
  167. linecap_square: properties.linecap_square,
  168. linejoin_bevel: properties.linejoin_bevel,
  169. linejoin_miter: properties.linejoin_miter,
  170. linejoin_round: properties.linejoin_round,
  171. main_icon: tools.main_menu,
  172. mode_connect: tools.mode_connect,
  173. tools_shapelib_show: tools.mode_shapelib,
  174. palette: ui.palette_info,
  175. zoom_panel: ui.zoom_level,
  176. path_node_x: properties.node_x,
  177. path_node_y: properties.node_y,
  178. rect_height_tool: properties.rect_height,
  179. rect_width_tool: properties.rect_width,
  180. seg_type: properties.seg_type,
  181. selLayerNames: layers.move_selected,
  182. selected_x: properties.pos_x,
  183. selected_y: properties.pos_y,
  184. stroke_color: properties.stroke_color,
  185. stroke_style: properties.stroke_style,
  186. stroke_width: properties.stroke_width,
  187. svginfo_title: config.doc_title,
  188. text: properties.text_contents,
  189. toggle_stroke_tools: ui.toggle_stroke_tools,
  190. tool_add_subpath: tools.add_subpath,
  191. tool_alignbottom: tools.align_bottom,
  192. tool_aligncenter: tools.align_center,
  193. tool_alignleft: tools.align_left,
  194. tool_alignmiddle: tools.align_middle,
  195. tool_alignright: tools.align_right,
  196. tool_aligntop: tools.align_top,
  197. tool_angle: properties.angle,
  198. tool_blur: properties.blur,
  199. tool_bold: properties.bold,
  200. tool_circle: tools.mode_circle,
  201. tool_clone: tools.clone,
  202. tool_clone_multi: tools.clone,
  203. tool_delete: tools.del,
  204. tool_delete_multi: tools.del,
  205. tool_ellipse: tools.mode_ellipse,
  206. tool_eyedropper: tools.mode_eyedropper,
  207. tool_fhellipse: tools.mode_fhellipse,
  208. tool_fhpath: tools.mode_fhpath,
  209. tool_fhrect: tools.mode_fhrect,
  210. tool_font_size: properties.font_size,
  211. tool_group_elements: tools.group_elements,
  212. tool_make_link: tools.make_link,
  213. tool_link_url: tools.set_link_url,
  214. tool_image: tools.mode_image,
  215. tool_italic: properties.italic,
  216. tool_line: tools.mode_line,
  217. tool_move_bottom: tools.move_bottom,
  218. tool_move_top: tools.move_top,
  219. tool_node_clone: tools.node_clone,
  220. tool_node_delete: tools.node_delete,
  221. tool_node_link: tools.node_link,
  222. tool_opacity: properties.opacity,
  223. tool_openclose_path: tools.openclose_path,
  224. tool_path: tools.mode_path,
  225. tool_position: tools.align_to_page,
  226. tool_rect: tools.mode_rect,
  227. tool_redo: tools.redo,
  228. tool_reorient: tools.reorient_path,
  229. tool_select: tools.mode_select,
  230. tool_source: tools.source_save,
  231. tool_square: tools.mode_square,
  232. tool_text: tools.mode_text,
  233. tool_topath: tools.to_path,
  234. tool_undo: tools.undo,
  235. tool_ungroup: tools.ungroup,
  236. tool_wireframe: tools.wireframe_mode,
  237. view_grid: tools.toggle_grid,
  238. tool_zoom: tools.mode_zoom,
  239. url_notice: tools.no_embed
  240. }, true);
  241. editor.setLang(lang_param, langData);
  242. };
  243. editor.putLocale = function (given_param, good_langs) {
  244. if (given_param) {
  245. lang_param = given_param;
  246. }
  247. else {
  248. lang_param = $.pref('lang');
  249. if (!lang_param) {
  250. if (navigator.userLanguage) { // Explorer
  251. lang_param = navigator.userLanguage;
  252. }
  253. else if (navigator.language) { // FF, Opera, ...
  254. lang_param = navigator.language;
  255. }
  256. if (lang_param == null) { // Todo: Would cause problems if uiStrings removed; remove this?
  257. return;
  258. }
  259. }
  260. console.log('Lang: ' + lang_param);
  261. // Set to English if language is not in list of good langs
  262. if ($.inArray(lang_param, good_langs) === -1 && lang_param !== 'test') {
  263. lang_param = "en";
  264. }
  265. // don't bother on first run if language is English
  266. // The following line prevents setLang from running
  267. // extensions which depend on updated uiStrings,
  268. // so commenting it out.
  269. // if (lang_param.indexOf("en") === 0) {return;}
  270. }
  271. var conf = editor.curConfig;
  272. var url = conf.langPath + "lang." + lang_param + ".js";
  273. $.getScript(url, function(d) {
  274. // Fails locally in Chrome 5+
  275. if (!d) {
  276. var s = document.createElement('script');
  277. s.src = url;
  278. document.querySelector('head').appendChild(s);
  279. }
  280. });
  281. };
  282. return editor;
  283. }(jQuery, svgEditor));