{"id":6026,"date":"2014-04-12T00:28:49","date_gmt":"2014-04-12T00:28:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/how-to-make-localization-on-months-days-for-d3js-collection-of-common-programming-errors-2\/"},"modified":"2014-04-12T00:28:49","modified_gmt":"2014-04-12T00:28:49","slug":"how-to-make-localization-on-months-days-for-d3js-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/how-to-make-localization-on-months-days-for-d3js-collection-of-common-programming-errors-2\/","title":{"rendered":"How to make localization on months \/ days for D3js?-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5d3b7f746ad94a31fe8026782b5fe1be?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNeon<\/p>\n<p>I am looking for a way to do localization on D3<\/p>\n<p>I have found the values<\/p>\n<pre><code>d3_time_days = [ \"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\" ],\nd3_time_dayAbbreviations = [ \"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\" ], \nd3_time_months = [ \"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\" ], \nd3_time_monthAbbreviations = [ \"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\" ];\n<\/code><\/pre>\n<p>inside D3 but as they are local\/privat to D3 i (obviously) cant access them.<\/p>\n<p>Any hints would be great, thanks \ud83d\ude42<\/p>\n<h3>Update 1:<\/h3>\n<p>Seems only a recompile can do the magic &#8211; but can&#8217;t be boughtered &#8211; so ended up hard coding the edits in the minified version &#8211; shame on me&#8230;<\/p>\n<h3>Update 2:<\/h3>\n<p>Will try to look into how to make D3 accept an &#8220;on the fly&#8221; localization setting like fx moment.js does it:<\/p>\n<pre><code>moment.lang('da', {\n    months : \"Januar_Februar_Marts_April_Maj_Juni_Juli_August_September_Oktober_November_December\".split(\"_\"),\n    monthsShort : \"Jan_Feb_Mar_Apr_Maj_Jun_Jul_Aug_Sep_Okt_Nov_Dec\".split(\"_\"),\n    weekdays : \"S\u00f8ndag_Mandag_Tirsdag_Onsdag_Torsdag_Fredag_L\u00f8rdag\".split(\"_\"),\n    weekdaysShort : \"S\u00f8n_Man_Tir_Ons_Tor_Fre_L\u00f8r\".split(\"_\"),\n    weekdaysMin : \"S\u00f8_Ma_Ti_On_To_Fr_L\u00f8\".split(\"_\"),\n    ordinal : '%d.',\n    week : {\n        dow : 1, \/\/ Monday is the first day of the week.\n        doy : 4  \/\/ The week that contains Jan 4th is the first week of the year.\n    }\n});\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/51dc6c879dfd7d737dd541afd8d9219f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nLars Kotthoff<\/p>\n<p>From the documentation:<\/p>\n<blockquote>\n<p>D3&#8217;s implementation is fixed to a locale at compile time based on the $LOCALE environment variable.<\/p>\n<\/blockquote>\n<p>In order to localise it to your environment, you need to recompile d3.js (and d3.min.js) on a machine with the locale settings you want. This will replace the strings you&#8217;re seeing in the source.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/09c0161c6d7df52090eeb0a79b1c0aca?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ndsuess<\/p>\n<p><strong>Multiple Files:<\/strong> Recompiling the Lib and providing multiple files of 10.000 lines of codes, differing only at 5 lines&#8230; is this best practice?<\/p>\n<p><strong>Patch for on the fly changes:<\/strong> As locale-string are private you can (or rather need to) patch D3.js. Unfortunately any patched library complicates update processes. An example of on-the-fly localization is shown at internationalization and localization at runtime<\/p>\n<p><strong>Official Commit:<\/strong> Mike Bostock reacted quickly an there is a commit on locale-branch by him. It seems to be a first draft, but maybe it is helpful for you.<\/p>\n<p>dsuess<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/2da4e6ef9e8d179a011d92c37043e7f2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuperman<\/p>\n<p>You can create custom d3 time formatters and use moment.js for localization. For example, to create an axis with localized dates:<\/p>\n<pre><code>var x = d3.time.scale();\n\nvar myTimeFormatter = function(date) {\n    return moment(date).format(\"LL\");\n};\n\nvar xAxis = d3.svg.axis()\n    .scale(x)\n    .orient(\"bottom\")\n    .tickFormat(myTimeFormatter);\n<\/code><\/pre>\n<p>Then use <code>moment.lang<\/code> to add support for additional languages and to toggle current language. Refer to http:\/\/momentjs.com\/docs\/#\/i18n for specifics.<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Neon I am looking for a way to do localization on D3 I have found the values d3_time_days = [ &#8220;Sunday&#8221;, &#8220;Monday&#8221;, &#8220;Tuesday&#8221;, &#8220;Wednesday&#8221;, &#8220;Thursday&#8221;, &#8220;Friday&#8221;, &#8220;Saturday&#8221; ], d3_time_dayAbbreviations = [ &#8220;Sun&#8221;, &#8220;Mon&#8221;, &#8220;Tue&#8221;, &#8220;Wed&#8221;, &#8220;Thu&#8221;, &#8220;Fri&#8221;, &#8220;Sat&#8221; ], d3_time_months = [ &#8220;January&#8221;, &#8220;February&#8221;, &#8220;March&#8221;, &#8220;April&#8221;, &#8220;May&#8221;, &#8220;June&#8221;, &#8220;July&#8221;, &#8220;August&#8221;, &#8220;September&#8221;, &#8220;October&#8221;, &#8220;November&#8221;, &#8220;December&#8221; ], [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6026","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6026"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6026\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}