{"id":370,"date":"2009-04-11T22:09:44","date_gmt":"2009-04-11T21:09:44","guid":{"rendered":"http:\/\/meier-online.com\/?p=370"},"modified":"2009-10-07T00:10:10","modified_gmt":"2009-10-06T23:10:10","slug":"localize-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/meier-online.com\/en\/2009\/04\/localize-ruby-on-rails\/","title":{"rendered":"Multilingual Websites with Ruby on Rails"},"content":{"rendered":"<p><\/p>\n<h3>Part 1 &#8211; Overview<\/h3>\n<p>Writing software for several languages is an ever recurrent challenge. Ruby on Rails provides  solutions to most of the standard problems in web development. But it needed time until the version 2.2. for the &#8220;Internationalization API &#8221; to become an official part of Ruby on Rails. .<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-374\" title=\"The Ruby-On-Rails Internationalization module\" src=\"http:\/\/meier-online.com\/blog\/uploads\/rails-i18n.png\" alt=\"The Ruby-On-Rails Internationalization module\" width=\"480\" height=\"300\" srcset=\"https:\/\/meier-online.com\/blog\/uploads\/rails-i18n.png 480w, https:\/\/meier-online.com\/blog\/uploads\/rails-i18n-300x187.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/p>\n<p><!--more--><\/p>\n<h3>Structure of the framework<\/h3>\n<p>The framework consists of a &#8220;public&#8221; and a &#8220;private&#8221; part. The I18N module forms the public part with the methods that we call in our code. The private backend is interchangeable. The standard implementation reads the texts from files in Yaml format or from normal ruby files.<\/p>\n<h3>Calling<\/h3>\n<p>In our code, we replace all the text to be translated with the <em>I18N.translate()<\/em> method. Thanks to an alias definition, we can simply write <em>t()<\/em>. We give a key as parameter, and the backend uses this key to look up the correct translation.<\/p>\n<p>The key is a string or a symbol. Normally, look up by symbol is slightly more efficient. But since the key gets decomposed into its components during the processing, I expect that this advantage disappears.<\/p>\n<p>In a typical web application, many of the texts to be translated are found in the views, ie. Erb templates. As an abbreviation for pure HTML text, there is the following tag:<\/p>\n<pre>&lt;%=t 'key' %&gt;<\/pre>\n<h3>Organization of keys<\/h3>\n<p>The keys have a hierarchical structure. The suggested organization is:<\/p>\n<p>&#8220;Controllername.viewname.key&#8221;, for example &#8216;companies.show.header&#8217;<\/p>\n<h3>The language files<\/h3>\n<p>The language files can either be Ruby code files or text files in Yaml format.  The person who translates is often a person without Ruby knowledge. That is why the Yaml file format is recommended.<\/p>\n<p>The format looks like this:<\/p>\n<pre lang=\"text\">de:\r\n  users:\r\n    show:\r\n      header: \"Company Data\"\r\n      intro: \"For this company, we have stored the following data:\"<\/pre>\n<p>The first key part the language code. This has the disadvantage that it separates the text entries of different languages. To help the translators, you can insert comments by marking a line with a beginning &#8216;#&#8217; character . And of course, you should name the keys as clear as possible.<\/p>\n<p>But even then, when developer and translator are not the same person, I think the Yaml file format is not optimal.<\/p>\n<p>Part 2 of this article will soon be translated to English.<\/p>","protected":false},"excerpt":{"rendered":"<p>Part 1 &#8211; Overview Writing software for several languages is an ever recurrent challenge. Ruby on Rails provides solutions to most of the standard problems in web development. But it needed time until the version 2.2. for the &#8220;Internationalization API &#8221; to become an official part of Ruby on Rails. .<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[126,114,146],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-develop","tag-mehrsprachigkeit","tag-ruby","tag-translation","entry"],"_links":{"self":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts\/370"}],"collection":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/comments?post=370"}],"version-history":[{"count":19,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/posts\/370\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meier-online.com\/en\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}