{"id":6185,"date":"2013-06-24T00:00:00","date_gmt":"2013-06-24T00:00:00","guid":{"rendered":"https:\/\/www.globalizationpartners.com\/2013\/06\/24\/javascript-internationalization-and-localization\/"},"modified":"2021-01-05T12:38:17","modified_gmt":"2021-01-05T12:38:17","slug":"javascript-internationalization-and-localization","status":"publish","type":"post","link":"https:\/\/www.globalizationpartners.com\/2013\/06\/24\/javascript-internationalization-and-localization\/","title":{"rendered":"JavaScript Internationalization and Localization"},"content":{"rendered":"<p>Internationalization (I18n) and localization (L10n) are two of the most important phases in software&#8217;s engineering<br \/>\nlife cycle in order to create worldwide applications that function for different cultures, languages and locations.<br \/>\nI18n and L10n are usually controlled server side by managed programming languages such as Microsoft ASP.NET, where<br \/>\nresource files (*.resx) \u00a0provide the foundation of the process and by using the Microsoft <a href=\"\/localization-for-microsoft-net-web-applications\/\">.net<br \/>\nframework<\/a> the application can display the correct language and locale based on the user choice, IP<br \/>\naddresses, domains etc\u2026<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.globalizationpartners.com\/wp-content\/uploads\/2018\/07\/javascript-localization_369x295.jpg\" alt=\"Javascript-localization\" width=\"369\" height=\"295\" \/><\/p>\n<h5>JavaScript, JSON, JQuery<\/h5>\n<p>Due to the steady increase of services provided by the Internet worldwide and the new technology tools and concepts,<br \/>\nthe client side languages have become more complex. Software internationalization (I18n) and <a title=\"Software Localizatopn\" href=\"\/services\/software-localization\/\">localization<\/a> (L10n) have become very important criteria to address with the<br \/>\noverall globalization of an application. <a href=\"https:\/\/www.javascriptsource.com\/\">JavaScript<\/a>, JSON, JQuery<br \/>\nare currently playing a major role in any website user interface (UI) layer. Let&#8217;s examine a very simple case that<br \/>\nshows the necessity of internationalization and localization of client-side routines:\u00a0 A multilingual application<br \/>\nready to display and to handle any number of languages developed using ASP.NET and .NET framework. This website<br \/>\nuses JavaScript to validate users input and display warning or error messages in some cases such as &#8220;Email format<br \/>\nnot valid&#8221;, &#8220;Only digits allowed&#8221;. Usually web applications use the client-side approach to validate user inputs<br \/>\ninstead of server-side validation for performance reasons and to avoid non-necessary rounds between client and<br \/>\nserver-sides.<\/p>\n<p>In the above case, if the website UI displays Arabic, normally all client-side alerts should be displayed using the<br \/>\nsame language. Based on this, the L18n\/l10n becomes a must in order to ensure the application overall integrity.<br \/>\nThe following approach describes one of the ways suggested to internationalize client-side languages:<\/p>\n<p>First step is to create a JavaScript file that will be used to store the strings values in source language (English<br \/>\nfor example). The internal key \/ value structure of the JavaScript file will be as following:<\/p>\n[code]var key=&#8221;value&#8221; ; \u00a0(i.e.\u00a0 var EmailError= &#8220;Invalid Email Format!&#8221;)[\/code]\n<p>Once all messages are added to the English JavaScript file, the file can be multiplied per language. For example if<br \/>\nthe English file name is Resources.js, we can have Resource.ar-EG.js for the Arabic language with Egyptian locale,<br \/>\nResources.fr-FR for French France, etc\u2026 And then we can change the values of the keys to the corresponding message<br \/>\nbut in the new target language. The above Email error message will be displayed in Arabic as following:<\/p>\n[code]var EmailError=&#8221;\u0628\u0631\u064a\u062f\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a \u062e\u0637\u0623!&#8221;;[\/code]\n<p>By now we have JavaScript resource files for each target language ready to be used. How can we select the correct<br \/>\nresource file based on the currently displayed language for the application?<\/p>\n<p>For demo purpose, we can create a simple asp.net page with a language selector to switch between English and Arabic<br \/>\nto monitor the JavaScript language change based on the current language.<\/p>\n<p>Also we can add a simple input field and a submit button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.globalizationpartners.com\/wp-content\/uploads\/2018\/07\/javascript-localization01.jpg\" alt=\"software localization\" width=\"628\" height=\"86\" \/><\/p>\n<p>The expected behavior should be receiving client-side messages in same language as the webpage overall UI : For<br \/>\nEnglish UI Wrong email address will raise the error warning in English<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.globalizationpartners.com\/wp-content\/uploads\/2018\/07\/javascript-localization02_402x206.jpg\" alt=\"localization\" width=\"402\" height=\"206\" \/><\/p>\n<p>For Arabic<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.globalizationpartners.com\/wp-content\/uploads\/2018\/07\/javascript-localization03_469x231.jpg\" alt=\"JavaScript Arabic\" width=\"469\" height=\"231\" \/><\/p>\n<p>Behind the scene, there are a few easy and quick steps to enable the above localization approach:<\/p>\n<p>First, add a script manager control to the page and enable script localization attribute:<\/p>\n[code]&lt;asp:ScriptManager<br \/>\nID=&#8221;spManager&#8221;<br \/>\nrunat=&#8221;server&#8221;<br \/>\nEnableScriptLocalization=&#8221;true&#8221;&gt;[\/code]\n<p>Add a reference to each JavaScript resource file created per language along with each resource UI Culture:<\/p>\n[code]&lt;asp:ScriptReference Path=&#8221;\/Scripts\/StringResources.js&#8221; ResourceUICultures=&#8221;ar-EG&#8221; \/&gt;[\/code]\n<p>To retrieve the correct localized Message based on the user input a simple routine can be coded to validate the data<br \/>\nentered and raise the correct error or warning message accordingly<\/p>\n[code][JavaScript routine to validate Email Format]\nAlert([key]) ; Example: alert(&#8220;wrongFormat&#8221;);<br \/>\n[JavaScript routine to validate empty string for mandatory field]\nAlert([key]) ; Example: alert(&#8220;required&#8221;);[\/code]\n<p>The above method calls the appropriate JavaScript resource file (i.e. Resources.ar-EG.js),\u00a0 selects the key inserted<br \/>\nin the alert and displays the key value. As mentioned above the application was able to select the correct resource<br \/>\nfile by the help of Script Manager where we have a reference to all resources files registered by language \/<br \/>\nlocale. For demo purposes the list box used above is used to select between different languages just as en-US,<br \/>\nar-EG etc..<\/p>\n<p>The above simple demo shows one of the ways to internationalize and localize client-side programming languages and<br \/>\nhighlights the importance of this process for multilingual applications.<\/p>\n<p><strong>SIDENOTE:<\/strong> GPI has developed a very strong Globalization Tool Suite &#8211; &#8220;<a title=\"Globalization Tool\" href=\"\/tools\/globalization-toolkit\" target=\"_blank\" rel=\"noopener noreferrer\">G11Tool<\/a>&#8221; which is able to identify areas inside server or<br \/>\nclient-side programming languages that require localization and also externalizes the content from almost any file<br \/>\nformat or programming language and exports them in ready to translate format such as ttx, *.resx and others. This<br \/>\napplication saves our engineers and yours a lot of time, effort and money when globalizing your websites and<br \/>\nweb-applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Internationalization (I18n) and localization (L10n) are two of the most important phases in software&#8217;s engineering life cycle in order to create worldwide applications that function for different cultures, languages and locations. I18n and L10n are usually controlled server side by managed programming languages such as Microsoft ASP.NET, where resource files (*.resx) \u00a0provide the foundation of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":6186,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[1425,1444],"_links":{"self":[{"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/posts\/6185"}],"collection":[{"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/comments?post=6185"}],"version-history":[{"count":8,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/posts\/6185\/revisions"}],"predecessor-version":[{"id":29493,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/posts\/6185\/revisions\/29493"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/media\/6186"}],"wp:attachment":[{"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/media?parent=6185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/categories?post=6185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globalizationpartners.com\/wp-json\/wp\/v2\/tags?post=6185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}