{"id":1601,"date":"2016-02-21T12:20:21","date_gmt":"2016-02-21T10:20:21","guid":{"rendered":"https:\/\/blog.zitnik.si\/?p=1601"},"modified":"2017-09-04T15:38:32","modified_gmt":"2017-09-04T13:38:32","slug":"a-simple-custom-solution-to-multi-language-support-using-js","status":"publish","type":"post","link":"https:\/\/blog.zitnik.si\/?p=1601","title":{"rendered":"A simple custom solution to multi-language support using JS"},"content":{"rendered":"<p>Recently, I needed to add a multi-language support to a website that was run using PHP. Sure, I could use some i18n PHP framework to deliver translated web-page directly from a server, but I decided to design a simple approach based on Javascript.<\/p>\n<p>Here it goes &#8230;<\/p>\n<p>The default language is supposed to be slovene, so visiting a page by url http:\/\/mywebpage would show a slovene version. To get a webpage in a specific language, you need to explicitly define the language using a parameter, like so: http:\/\/mywebpage?lang={sl, hr, en}. Of course, this is used in a header of a web page for a languages selection menu:<\/p>\n<p>[codesyntax lang=&#8221;html4strict&#8221;]<\/p>\n<pre>&lt;a id=\"lang-sl\" href=\"\/?lang=sl\"&gt;Slovenski&lt;\/a&gt;|\r\n&lt;a id=\"lang-hr\" href=\"\/?lang=hr\"&gt;Hrvatski&lt;\/a&gt;|\r\n&lt;a id=\"lang-en\" href=\"\/?lang=en\"&gt;English&lt;\/a&gt;<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>In the webpage, I introduced a &#8220;lang&#8221; HTML tag, which acts as a placeholder for a language-specific text:<\/p>\n<p>[codesyntax lang=&#8221;html4strict&#8221;]<\/p>\n<pre>&lt;lang id=\"home\" \/&gt;<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>For each language I create a separate JS file that includes a map variable, whose keys\u00a0are ids used in HTML placeholders and values are translated texts. For example, a file &#8220;lang.sl.js&#8221; includes slovene translations:<\/p>\n<p>[codesyntax lang=&#8221;javascript&#8221;]<\/p>\n<pre>var lang = {\r\n    home: \"Domov\",\r\n    about: \"O programu\",\r\n    functionalities: \"Funkcionalnosti\",\r\n    ....\r\n};<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>Now, as we have everything prepared, we are going to write a JS script that (A) automatically detects a selected language from query string, (B) \u00a0includes an appropriate translation file and (C) automatically populates prepared HTML placeholders. I do this as follows:<\/p>\n<p>[codesyntax lang=&#8221;javascript&#8221;]<\/p>\n<pre>$(document).ready(function() {\r\n\r\n\t$.urlParam = function(name){\r\n\t\tvar results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);\r\n\t\tif (results==null){\r\n\t\t   return null;\r\n\t\t}\r\n\t\telse{\r\n\t\t   return results[1] || 0;\r\n\t\t}\r\n\t}\r\n\t\r\n\tfunction loadJS(file) {\r\n\t\tvar jsElm = document.createElement(\"script\");\r\n\t\tjsElm.type = \"application\/javascript\";\r\n\t\tjsElm.src = file;\r\n\t\t\r\n\t\t$(\"body\").prepend(jsElm);\r\n\t}\r\n\r\n\tif ($.urlParam(\"lang\") == \"en\") {\r\n\t\tloadJS(\"js\/lang.en.js\");\r\n\t\t$(\"#lang-en\").css(\"font-weight\", \"bolder\");\r\n\t} else if ($.urlParam(\"lang\") == \"hr\") {\r\n\t\tloadJS(\"js\/lang.hr.js\");\r\n\t\t$(\"#lang-hr\").css(\"font-weight\", \"bolder\");\r\n\t} else {\r\n\t\tloadJS(\"js\/lang.sl.js\");\r\n\t\t$(\"#lang-sl\").css(\"font-weight\", \"bolder\");\t\r\n\t}\r\n\t\r\n\tfunction fillIn(key, value) {\r\n\t\tdocument.getElementById(key).innerHTML = value;\t \t\r\n\t}\r\n\r\n\r\n\t\/\/Fill in translations\t\r\n\tfor(var key in lang) {\r\n\t  fillIn(key, lang[key]);\r\n\t}\r\n\t\r\n});<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>Additionally to that basic example I added some exceptions to insert language-specific values into some HTML tag attributes.<\/p>\n<p>Such approach works like a charm for about 200 language keys &#8211; I have not tested for more. The whole site is served by an RPi and JS loading of language-specific texts is not noticeable on a client.<\/p>\n<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div>","protected":false},"excerpt":{"rendered":"<p>Recently, I needed to add a multi-language support to a website that was run using PHP. Sure, I could use some i18n PHP framework to deliver translated web-page directly from a server, but I decided to design a simple approach based on Javascript. Here it goes &#8230; The default language&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/blog.zitnik.si\/?p=1601\">Continue reading<span class=\"screen-reader-text\">A simple custom solution to multi-language support using JS<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":1603,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1601","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-engineering","entry"],"_links":{"self":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1601"}],"version-history":[{"count":2,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1601\/revisions"}],"predecessor-version":[{"id":1604,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1601\/revisions\/1604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/media\/1603"}],"wp:attachment":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}