locale.js 9.1 KB

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