CSC/ECE 517 Fall 2013/ch1 1w18 bs: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(40 intermediate revisions by 2 users not shown)
Line 7: Line 7:
:In general when we start a conversation, the first default language of communication we use is English, but if the other person is not familiar with English we try and translate our dialogue in a language that the person is familiar with. Similarly whenever we write a program or a document we write it in English by default. But what if someone who is not familiar with English wants to read your program or code? The simple solution is that we translate program or code into a language that the person is familiar with. And for this, we use a term called internationalization.
:In general when we start a conversation, the first default language of communication we use is English, but if the other person is not familiar with English we try and translate our dialogue in a language that the person is familiar with. Similarly whenever we write a program or a document we write it in English by default. But what if someone who is not familiar with English wants to read your program or code? The simple solution is that we translate program or code into a language that the person is familiar with. And for this, we use a term called internationalization.


:Internationalization is a way by which we can view an application in various languages without actually making changes to the code of the application. This saves us a lot of time and effort, as we don’t have to write or edit the same code or document in all the copies.
:Internationalization is a way by which we can view an application in various languages without actually making changes to the code of the application. This saves us a lot of time and effort, as we don’t have to write or edit the same code or document in all the copies.<ref>http://en.wikipedia.org/wiki/Internationalization_and_localization </ref>


:Internationalization has an acronym, rather more of a numeronym ,which is i18n where the number 18 represents the number of letters between the first and last letters in the word internationalization.
:Internationalization has an [http://en.wikipedia.org/wiki/Acronym | acronym], rather more of a [http://en.wikipedia.org/wiki/Numeronym | numeronym] ,which is i18n where the number 18 represents the number of letters between the first and last letters in the word internationalization.


== Aspects to consider in Internationalization ==
== Aspects to consider in Internationalization ==
Line 42: Line 42:


:Similarly there are many other factors that we need to take care of when we support internationalization for a particular language in a particular region.
:Similarly there are many other factors that we need to take care of when we support internationalization for a particular language in a particular region.
<ref>http://en.wikipedia.org/wiki/Internationalization_and_localization </ref>


= Internationalization in Rails =
= Internationalization in Rails =


:Internationalization support was extended from Rails version 2.2. So in order to internationalize a rails application, it has to be migrated :to Rails 2.2 or above. Every static string in the Rails framework e.g. Active Record validation messages, time and date formats have :been internationalized.
:Internationalization support was extended from Rails version 2.2. So in order to internationalize a rails application, it has to be migrated to Rails 2.2 or above. Every static string in the Rails framework e.g. Active Record validation messages, time and date formats, have been internationalized.


:Following needs to be done in order to internationalize a rails application:
:Following needs to be done in order to internationalize a rails application:
Line 58: Line 59:
:{| class="wikitable" style="width: 100%;"
:{| class="wikitable" style="width: 100%;"
|-
|-
|width="100pt"| Ruby 2.2    || Internationalization support was extended from this version of Rails.
! Ruby version !! Features added
|-
|-
| Ruby 2.3   || Additional feature for Localized Views was added in this version. The view files were rendered with an extension of the locale name.I18n#available_locales and I18n::SimpleBackend#available_locales is available to retrieve an array of available locales.
|width="100pt"| [http://guides.rubyonrails.org/2_2_release_notes.html Ruby 2.2]   || Internationalization support was extended from this version of Rails.
|-
|-
| Ruby 3.0   || Some additional features were added to the I18n gem for speed improvements like default translations for attributes,automatic pull for translations on form submit, etc.
| [http://guides.rubyonrails.org/2_3_release_notes.html Ruby 2.3]   || Additional feature for Localized Views was added in this version. The view files were rendered with an extension of the locale name.I18n#available_locales and I18n::SimpleBackend#available_locales is available to retrieve an array of available locales.
|-
|-
| Ruby 3.1    || I18n namespace lookup support removed.
| [http://guides.rubyonrails.org/3_0_release_notes.html Ruby 3.0]    || Some additional features were added to the I18n gem for speed improvements like default translations for attributes,automatic pull for translations on form submit, etc.
|-
| [http://guides.rubyonrails.org/3_1_release_notes.html Ruby 3.1]   || I18n namespace lookup support removed.
|}
|}


Line 71: Line 74:
:We can set up a rails application to support internationalization in a number of ways. Below are the various ways to configure our rails application for internationalization.
:We can set up a rails application to support internationalization in a number of ways. Below are the various ways to configure our rails application for internationalization.


#'''Configure the I18n module:'''<br>Rails adds all .rb and .yml files from the config/locales directory to your translations load path, automatically. This is the default setting provided by the [https://github.com/svenfuchs/i18n I18n gem]. In order to override this setting we have to make changes in the application.rb files that have instructions on how to add locales from another directory and how to set a different default locale.
#'''Configure the I18n module:'''<br>Rails adds all .rb and .yml files from the config/locales directory to your translations load path, automatically. This is the default setting provided by the [https://github.com/svenfuchs/i18n I18n gem]. In order to override this setting we have to make changes in the application.rb files that have instructions on how to add locales from another directory and how to set a different default locale.<br>config.i18n.load_path += Dir[Rails.root.join('mydir', 'locales', '*.{rb,yml}').to_s]<br>config.i18n.default_locale = :es
#'''Custom I18n Configuration Setup:'''<br>Optionally, the above code can be placed anywhere in the application preferably in initializers.
#'''Custom I18n Configuration Setup:'''<br>Optionally, the above code can be placed anywhere in the application preferably in initializers.
#'''Setting and passing locale:'''<br>The above two steps help setup a default locale throughout the application. But in case we have to provide our application in different languages, we can setup the application default locale in the application.rb and then set the locale using the application-controller.rb before_action. We can then pass the locale we want as a query param for example http://study.com/books?locale=es
#'''Setting and passing locale:'''<br>The above two steps help setup a default locale throughout the application. But in case we have to provide our application in different languages, we can setup the application default locale in the application.rb and then set the locale using the applicationController before_action. We can then pass the locale we want as a query param for examplestudy.com/books?locale=es
#'''Setting the locale from the domain name:'''<br>Similar to option 3 but instead of passing the locale as a query param, the locale is a part of the domain name itself. Example http://study.es
#'''Setting the locale from the domain name:'''<br>Similar to option 3 but instead of passing the locale as a query param, the locale is a part of the domain name itself. For example- study.es. You can implement it like this in your [http://api.rubyonrails.org/classes/ActionController/Base.html ApplicationController] before_action
::You can implement it like this in your ApplicationController before_action
#'''Setting the locale from the URL params:'''<br>
#'''Setting the locale from the URL params:'''<br>
##This can be a tedious task as we have to pass the locale on each request.  
##This can be a tedious task as we have to pass the locale on each request.  
##Rails provides us an alternative for "centralizing dynamic decisions about the URLs" in its ApplicationController#default_url_options and helper methods are dependent on it (by implementing/overriding this method). This will now automatically include the locale param in the query string
##Rails provides us an alternative for "centralizing dynamic decisions about the URLs" in its ApplicationController#default_url_options and helper methods are dependent on it (by implementing/overriding this method). This will now automatically include the locale param in the query string
##We can also set the locale in the URL route. For example -http://study.com/es/books .This is achievable by using over-riding default_url_options. You just have to set up your routes with scoping option in routes.rb.  
##We can also set the locale in the URL route. For example- study.com/es/books. This is achievable by using over-riding default_url_options. You just have to set up your routes with scoping option in routes.rb.  
#'''Setting the locale from client supplied information:'''<br>This can be done in 3 ways
#'''Setting the locale from client supplied information:'''<br>This can be done in 3 ways
##Using the default locale of the client browser
##Using the default locale of the client browser
##Using client location to select the locale
##Using client location to select the locale
##Saving the users choice of locale as a part of user profile
##Saving the users choice of locale as a part of user profile
 
<ref> http://blog.lingohub.com/developers/2013/08/internationalization-for-ruby-i18n-gem/</ref>


== Internationalizing Rails application ==
== Internationalizing Rails application ==


:After your application is prepared for internationalization, we need to use the feature in our application.  
:After your application is prepared for internationalization, we need to use the feature in our application.  
:This can be done in the following ways:
:This can be done in the following ways: <ref> http://blog.lingohub.com/developers/2013/08/internationalization-for-ruby-i18n-gem/ </ref>
 
1. '''Adding Translations:'''
:In order to internationalize rails code,  replace the strings with calls to  the ‘t’ helper.<br>For example:
class HelloWorld
  def sayHello
    puts t(:hello_world)
  end
end
 
or
 
<%=t :hello_world %>
 
:The helper method t also catches missing translations and displays appropriate error message.
<nowiki /># config/locales/en.yml
en: hello_world: Hello world!
 
<nowiki /># config/locales/es.yml
es: hello_world: hola mundo
 
2. '''Passing variables to translation:'''
:You can use variables in the translation messages and pass their values from the view.
en:
portal: "Mypack"
  university:
    ncsu:
    description: ! 'NCSU student information is accessed on %{portal_name}’
 
t('.description', portal_name: t(‘portal’))
 
3. '''Adding date/ time formats:'''
:OK! Now let's add a timestamp to the view, so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use ‘l’ helper. You can pick a format by passing the :format option — by default the :default format is used.
:In the view file:
<%= l Time.now, format: :short %>
:In the yml file:
  es:
  time:
    formats:
      short: " Son las %H"
 
4. '''Inflection rules for locales other than english:'''
:Rails 4.0 provides a feature to define inflection rules (singularization and pluralization)  in config/initializers/inflections.rb.
 
5. '''Localized views:'''
:For static websites available in different languages or for large static content, localized views is a useful feature. This feature enables having separate view file for different locales. For example for default locale we can have show.html.erb file while for Spanish we can have show.es.html.erb. Thus we don’t need to maintain long .yml files for each supported locale for long static pages. <ref>http://guides.rubyonrails.org/v2.3.11/i18n.html</ref>
 
== Internationalization Features ==
 
1. '''Using Different Backends:'''
:The i18n provides a [http://www.ruby-doc.org/gems/docs/s/synergy_russian-0.2.8/I18n/Backend/Simple.html simple backend] by default. We can change the backend to an active records backend or chain multiple backends as per the requirements of our application. I18n gem also provides cache, fallback and pluralization feature for the backends.
 
2. '''Pluralization:'''
:Different languages have different rules for pluralization. Thus, the I18n API provides a flexible pluralization feature.
 
3. '''Fallbacks:'''
:In case translation for a certain  locale is missing we can define fallback chain for the locales.
:For example we can define a fallback chain of the form
:'en-CA' => :'en-US', :'en-US' => :en, :de => :en
 
:if the translation for en-CA is missing rails looks for en-US , if en-US is missing it then looks for en and so on.
 
4. '''Cascading lookups:'''
:If a given key is not found, it is broken and a lookup is performed for the remaining portion of the key before finally falling back to the defaults.
 
5. '''Translation symlinks:'''
:A key can be a value for another key. Valid use for this would be situation where the key-value pairs that share values are already structured (for example localized libraries or gems). <ref> http://blog.lingohub.com/developers/2013/08/i18n-gem-advanced-features-ruby-rails-internationalization/ </ref>
:Example:
person: ‘ash’
student: :person
employee: :person
 
:Ash can be an employee or a student. Each key represents the same value.
 
6. '''Translation procs:'''
:I18n gem allows writing translation logic in the translation lookup process.
:For example:
:en
  :addressing => lambda { |values|
person=values[:person]
address=person.male? ?  “Sir” : “Madam”
“Hello, #{person.address}”
t(:addressing , :person => mary)
<ref>http://vimeo.com/12665914 </ref>
= Advantages of Internationalization =
 
#Internationalization opens up a whole new market for applications. For example, consider a website which has support for internationalization and because of that it can be displayed in a number of languages. This opens a lot of new markets, business opportunities and a large number of potential customers.
#With the help of the internationalization gem, the application code has been simplified.
#The maintenance of the code is also made easy.  Also, the steps to add support to a new language are quite simple.
#Internationalization in rails(i18n gem) provides a variety of ways to incorporate internationalization in our application. This makes it easier for the developer to adopt a method that suits his application. For example, for a static application we can we one configuration technique and say for a dynamic application we can follow another configuration method for internationalization.
 
= Internationalization in other languages =
== PHP ==
=== Introduction ===
:We need to install/enable extensions to support [http://devzone.zend.com/1500/internationalization-in-php-53/ internationalization in PHP](not required with PHP 5.3.0 version). For building the internationalization tool we must have  ICU library (v 3.6 or more).
:The internationalization in PHP is provided by modules which are built with the internationalization extension. PHP applies i18n in one of the following ways:
#'''Using Object-Oriented API :''' <br>This makes of the object-oriented API provided by the modules. This method represents the modules in the form of classes.
#'''Using Procedural API :''' <br>This makes use of the procedural API provided by the modules. This method represents the modules in the form of a group of functions.
:Note: Each module provides both of the above API’s.
:Each of these modules provide different functionality of internationalization. For example PHP has modules like:
 
#'''Locale''' —  This mainly deals with breaking and assembling of strings from components and displaying them in a specified locale.
#'''Collator''' —  This maily deals with comparison and sorting of strings according to the rules of the specified locale.
#'''Number formatter''' — This mainly deals with formatting number in a specific way according to a locale and also parses textual representations of numbers.
#'''Date formatter''' —  This formats dates in accordance to the specified locale.
:and many more…
 
=== Comparison with Rails ===
 
#Internationalization in PHP is a bit easy as compared to ruby. Because the way to implement internationalization is very similar to creating an object of a class and then using one of the functions of the classes. And hence the programmer is familiar with the syntax.
#Whenever we need to add more features to internationalization, in PHP we just have to add a module to the internationalization extension.
 
== Java ==
=== Introduction ===
There is a built-in support for [http://docs.oracle.com/javase/tutorial/i18n/ internationalization in Java].
 
* In java, we do not set a global variable for locale. We set a default locale and assign a locale separately to each object as and when needed.
* Lookup of translations is made in an efficient way with support for fallbacks, interpolation etc. The translations are stored in the [http://blog.lingohub.com/developers/2013/03/resource-file-formats-properties-files-comments/ .properties] file which contains key-value pairs.
* The ResourceBundle class is responsible for loading locale specific objects.
* It also supports formatting of numbers, currencies, date and time and messages according to the locale.
* The Java programming language provides support to handle text in a locale-independent manner.
* Java also provides support for Internationalization for domain name.
 
=== Comparison with Rails ===
 
# Java code for internationalization may seem complex as compared to incorporating internationalization in ruby.
# Java does not extend any special support for pluralisation But we can pluralize our strings by breaking them and storing them with separate keys. Java internationalization is much more advanced then ruby internationalization.
# Java internationalization provides a lot of features as opposed to ruby. Ruby at the most basic level cares only about looking up translations,formating date/time and currency. Java supports the following internationalization aspects looking up translations,formating date/time and currency, time zones, calendar systems, collation, character encoding, etc.


:1.'''Adding Translations:'''<br>In order to internationalize rails code, replace the strings with calls to  the ‘t’ helper.<br>For example:
== Python ==
:::class HelloWorld <br>def sayHello<br>  puts t(:hello_world)<br>end<br>end<br>or<br>  <%=t :hello_world %><br>The helper method t also catches missing translations and displays appropriate error message.<br><br><nowiki /># config/locales/en.yml<br>en:<br> hello_world: Hello world!<br><br><nowiki /># config/locales/es.yml<br>es:<br>hello_world: hola mundo
===Introduction===
[http://www.python.org/doc//current/library/gettext.html Internationalization in python] is achieved using the gettext module.


:2.'''Passing variables to translation:'''<br>You can use variables in the translation messages and pass their values from the view.<br>
# In python, the strings that are to be translated are first marked as translatable using the _() function.
:: en:<br>portal: "Mypack"<br>university:<br>ncsu:<br>description: ! 'NCSU student information is accessed on %{portal_name}’<br><br> t('.description', portal_name: t(‘portal’))
# The pygettext program is then run on the source code to gather all the translatable strings.
# The pygettext generates a human readable .pot file which is given to the translators for translation.
# The translators provide a .po which is converted to a .mo file using msgfmt.py program.The .mo files are then used for translations at runtime.


:3.'''Adding date/ time formats:'''
=== Comparison with Rails ===
::OK! Now let's add a timestamp to the view, so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use ‘l’ helper. You can pick a format by passing the :format option — by default the :default format is used.
# In Rails, we can directly specify key value pairs in the .yml or .rb file whereas in python, we have to go through the above process in order to get the translation resources
::In the view file:
# Strong internationalization and UNICODE support in python makes it a strong contender for internationalization.
<p><%= l Time.now, format: :short %></p>
In the yml file:
es:
  time:
    formats:
      short: " Son las %H"


4. Inflection rules for locales other than english
= See Also =
Rails 4.0 provides a feature to define inflection rules (singularization and pluralization) in config/initializers/inflections.rb.  
http://rubylearning.com/blog/2012/07/24/minimal-i18n-with-rails-3-2/ <br>
http://www.artweb-design.de/2009/7/19/experimental-extensions-in-i18n-pluralization-fallbacks-gettext-cache-and-chain <br>
http://asciicasts.com/episodes/256-i18n-backends <br>
http://devzone.zend.com/1500/internationalization-in-php-53/ <br>
http://www.php.net/manual/en/intro.intl.php <br>
http://www.php.net/manual/en/intl.requirements.php <br>
http://www.php.net/manual/en/intl.installation.php <br>
http://www.php.net/manual/en/intl.configuration.php <br>
http://www.php.net/manual/en/intl.examples.basic.php <br>
http://www.python.org/doc//current/library/gettext.html#internationalizing-your-programs-and-modules <br>


5. Localized views
= '''References''' =
For static websites available in different languages or for large static content, localized views is a useful feature. This feature enables having separate view file for different locales. For example for default locale we can have show.html.erb file while for Spanish we can have show.es.html.erb. Thus we don’t need to maintain long .yml files for each supported locale for long static pages.
----
<references/>

Latest revision as of 00:21, 25 September 2013

Internationalization in Rails

Introduction

In general when we start a conversation, the first default language of communication we use is English, but if the other person is not familiar with English we try and translate our dialogue in a language that the person is familiar with. Similarly whenever we write a program or a document we write it in English by default. But what if someone who is not familiar with English wants to read your program or code? The simple solution is that we translate program or code into a language that the person is familiar with. And for this, we use a term called internationalization.
Internationalization is a way by which we can view an application in various languages without actually making changes to the code of the application. This saves us a lot of time and effort, as we don’t have to write or edit the same code or document in all the copies.<ref>http://en.wikipedia.org/wiki/Internationalization_and_localization </ref>
Internationalization has an | acronym, rather more of a | numeronym ,which is i18n where the number 18 represents the number of letters between the first and last letters in the word internationalization.

Aspects to consider in Internationalization

Now when we are to change our application according to a particular language or a region , we need to keep in mind numerous points that are important, implicit, particular or sensitive to that language or region. Like for example when we take a language into consideration for which our application needs to be translated, we need to consider the spoken and written aspects of that language. Also, there might be a case when we have the same language but it differs from one region to other. For this we need to keep in mind the typical change in the use of words, grammar and symbol for each region that uses the same language. Our application must also provide support for the varied writing conventions that differ from a language to another language or from one region to another.
Below are the examples of the points we need to keep in mind while we provide internationalization to our application :
  • Language :
Here the i18n support to an application needs to provide the character encoding keeping in mind the following points:
  1. The written language differs. For example most of the languages like English, French, German follow a script that can have inputs from a standard keyboard. But there are other languages like Hindi, Marathi, Sanskrit that follow a script called Devnagri and this cannot be directly typed in from a standard keyboard. So if we have to provide support for these languages we need to support the translation of the characters used in these scripts.
  2. The way we write also differs and needs to included in our i18n support. For example most of the languages follow a left-right direction while writing but some languages like Urdu tend to follow a right-left direction.
  3. The numbering symbols also vary from language to language. So when we display or accept any text with numbers, we must have i18n to support this mapping.
  4. The languages also differ in the use of grammar and structure of the sentences.For example the grammar rules and structure formation in English is way different than the one followed by another language like Sanskrit.


  • Culture and Writing Conventions:
Even if the language with the same characters and grammar is used, the use of the language , the symbols , the way of writing and many other things change with change in the region.
Following are some of the examples:
  1. There are some cultures that have some regulations about having a middle name as father’s name but other cultures don’t necessary include the middle name.
  2. There might be cases when there are different names or terminologies used in different cultures for the same purpose. For example, to identify a citizen uniquely US has the system of SSN while India is in the process of implementing this by using the term as Aadhar number.
  3. Another example of difference in the writing convention can be taken as that of a postal code. Some countries have 6 digit postal codes while others have 5.
  4. There is a difference in the use of many other things like the currency(like INR,dollars,Euro) used, the symbols(€,$,₹) used , the units in which the weights , temperature are measured. Another example could be the use of lakhs , crores for mentioning any financial figures in some countries while others use millions, billions.
  5. There could also be a difference in the formats used for a same thing. For example the format of writing dates and times vary from one region to another.


  • We also have to be careful that we cover all the sensitive topics and issues particular to a language or region.
Similarly there are many other factors that we need to take care of when we support internationalization for a particular language in a particular region.

<ref>http://en.wikipedia.org/wiki/Internationalization_and_localization </ref>

Internationalization in Rails

Internationalization support was extended from Rails version 2.2. So in order to internationalize a rails application, it has to be migrated to Rails 2.2 or above. Every static string in the Rails framework e.g. Active Record validation messages, time and date formats, have been internationalized.
Following needs to be done in order to internationalize a rails application:
  1. Ensure support for internationalization by migrating to appropriate version of rails if needed.
  2. Tell rails where to find the appropriate translations files
  3. Tell rails how to switch locales
YAML (.yml) or plain Ruby (.rb) files are used for storing translations.

History

Ruby version Features added
Ruby 2.2 Internationalization support was extended from this version of Rails.
Ruby 2.3 Additional feature for Localized Views was added in this version. The view files were rendered with an extension of the locale name.I18n#available_locales and I18n::SimpleBackend#available_locales is available to retrieve an array of available locales.
Ruby 3.0 Some additional features were added to the I18n gem for speed improvements like default translations for attributes,automatic pull for translations on form submit, etc.
Ruby 3.1 I18n namespace lookup support removed.

Ways to configure Internationalization in Rails

We can set up a rails application to support internationalization in a number of ways. Below are the various ways to configure our rails application for internationalization.
  1. Configure the I18n module:
    Rails adds all .rb and .yml files from the config/locales directory to your translations load path, automatically. This is the default setting provided by the I18n gem. In order to override this setting we have to make changes in the application.rb files that have instructions on how to add locales from another directory and how to set a different default locale.
    config.i18n.load_path += Dir[Rails.root.join('mydir', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :es
  2. Custom I18n Configuration Setup:
    Optionally, the above code can be placed anywhere in the application preferably in initializers.
  3. Setting and passing locale:
    The above two steps help setup a default locale throughout the application. But in case we have to provide our application in different languages, we can setup the application default locale in the application.rb and then set the locale using the applicationController before_action. We can then pass the locale we want as a query param for example- study.com/books?locale=es
  4. Setting the locale from the domain name:
    Similar to option 3 but instead of passing the locale as a query param, the locale is a part of the domain name itself. For example- study.es. You can implement it like this in your ApplicationController before_action
  5. Setting the locale from the URL params:
    1. This can be a tedious task as we have to pass the locale on each request.
    2. Rails provides us an alternative for "centralizing dynamic decisions about the URLs" in its ApplicationController#default_url_options and helper methods are dependent on it (by implementing/overriding this method). This will now automatically include the locale param in the query string
    3. We can also set the locale in the URL route. For example- study.com/es/books. This is achievable by using over-riding default_url_options. You just have to set up your routes with scoping option in routes.rb.
  6. Setting the locale from client supplied information:
    This can be done in 3 ways
    1. Using the default locale of the client browser
    2. Using client location to select the locale
    3. Saving the users choice of locale as a part of user profile

<ref> http://blog.lingohub.com/developers/2013/08/internationalization-for-ruby-i18n-gem/</ref>

Internationalizing Rails application

After your application is prepared for internationalization, we need to use the feature in our application.
This can be done in the following ways: <ref> http://blog.lingohub.com/developers/2013/08/internationalization-for-ruby-i18n-gem/ </ref>

1. Adding Translations:

In order to internationalize rails code, replace the strings with calls to the ‘t’ helper.
For example:
class HelloWorld 
 def sayHello
   puts t(:hello_world)
 end
end

or

<%=t :hello_world %>
The helper method t also catches missing translations and displays appropriate error message.
# config/locales/en.yml
en: hello_world: Hello world!
# config/locales/es.yml
es: hello_world: hola mundo

2. Passing variables to translation:

You can use variables in the translation messages and pass their values from the view.
en:
portal: "Mypack"
  university:
   ncsu:
   description: ! 'NCSU student information is accessed on %{portal_name}’ 
t('.description', portal_name: t(‘portal’))

3. Adding date/ time formats:

OK! Now let's add a timestamp to the view, so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use ‘l’ helper. You can pick a format by passing the :format option — by default the :default format is used.
In the view file:
<%= l Time.now, format: :short %>
In the yml file:
 es:
  time:
    formats:
      short: " Son las %H"

4. Inflection rules for locales other than english:

Rails 4.0 provides a feature to define inflection rules (singularization and pluralization) in config/initializers/inflections.rb.

5. Localized views:

For static websites available in different languages or for large static content, localized views is a useful feature. This feature enables having separate view file for different locales. For example for default locale we can have show.html.erb file while for Spanish we can have show.es.html.erb. Thus we don’t need to maintain long .yml files for each supported locale for long static pages. <ref>http://guides.rubyonrails.org/v2.3.11/i18n.html</ref>

Internationalization Features

1. Using Different Backends:

The i18n provides a simple backend by default. We can change the backend to an active records backend or chain multiple backends as per the requirements of our application. I18n gem also provides cache, fallback and pluralization feature for the backends.

2. Pluralization:

Different languages have different rules for pluralization. Thus, the I18n API provides a flexible pluralization feature.

3. Fallbacks:

In case translation for a certain locale is missing we can define fallback chain for the locales.
For example we can define a fallback chain of the form
:'en-CA' => :'en-US', :'en-US' => :en, :de => :en 
if the translation for en-CA is missing rails looks for en-US , if en-US is missing it then looks for en and so on.

4. Cascading lookups:

If a given key is not found, it is broken and a lookup is performed for the remaining portion of the key before finally falling back to the defaults.

5. Translation symlinks:

A key can be a value for another key. Valid use for this would be situation where the key-value pairs that share values are already structured (for example localized libraries or gems). <ref> http://blog.lingohub.com/developers/2013/08/i18n-gem-advanced-features-ruby-rails-internationalization/ </ref>
Example:
person: ‘ash’
student: :person
employee: :person 
Ash can be an employee or a student. Each key represents the same value.

6. Translation procs:

I18n gem allows writing translation logic in the translation lookup process.
For example:
:en
 	:addressing => lambda { |values|
		person=values[:person]
		address=person.male? ?  “Sir” : “Madam”
	“Hello, #{person.address}”

t(:addressing , :person => mary)

<ref>http://vimeo.com/12665914 </ref>

Advantages of Internationalization

  1. Internationalization opens up a whole new market for applications. For example, consider a website which has support for internationalization and because of that it can be displayed in a number of languages. This opens a lot of new markets, business opportunities and a large number of potential customers.
  2. With the help of the internationalization gem, the application code has been simplified.
  3. The maintenance of the code is also made easy. Also, the steps to add support to a new language are quite simple.
  4. Internationalization in rails(i18n gem) provides a variety of ways to incorporate internationalization in our application. This makes it easier for the developer to adopt a method that suits his application. For example, for a static application we can we one configuration technique and say for a dynamic application we can follow another configuration method for internationalization.

Internationalization in other languages

PHP

Introduction

We need to install/enable extensions to support internationalization in PHP(not required with PHP 5.3.0 version). For building the internationalization tool we must have ICU library (v 3.6 or more).
The internationalization in PHP is provided by modules which are built with the internationalization extension. PHP applies i18n in one of the following ways:
  1. Using Object-Oriented API :
    This makes of the object-oriented API provided by the modules. This method represents the modules in the form of classes.
  2. Using Procedural API :
    This makes use of the procedural API provided by the modules. This method represents the modules in the form of a group of functions.
Note: Each module provides both of the above API’s.
Each of these modules provide different functionality of internationalization. For example PHP has modules like:
  1. Locale — This mainly deals with breaking and assembling of strings from components and displaying them in a specified locale.
  2. Collator — This maily deals with comparison and sorting of strings according to the rules of the specified locale.
  3. Number formatter — This mainly deals with formatting number in a specific way according to a locale and also parses textual representations of numbers.
  4. Date formatter — This formats dates in accordance to the specified locale.
and many more…

Comparison with Rails

  1. Internationalization in PHP is a bit easy as compared to ruby. Because the way to implement internationalization is very similar to creating an object of a class and then using one of the functions of the classes. And hence the programmer is familiar with the syntax.
  2. Whenever we need to add more features to internationalization, in PHP we just have to add a module to the internationalization extension.

Java

Introduction

There is a built-in support for internationalization in Java.

  • In java, we do not set a global variable for locale. We set a default locale and assign a locale separately to each object as and when needed.
  • Lookup of translations is made in an efficient way with support for fallbacks, interpolation etc. The translations are stored in the .properties file which contains key-value pairs.
  • The ResourceBundle class is responsible for loading locale specific objects.
  • It also supports formatting of numbers, currencies, date and time and messages according to the locale.
  • The Java programming language provides support to handle text in a locale-independent manner.
  • Java also provides support for Internationalization for domain name.

Comparison with Rails

  1. Java code for internationalization may seem complex as compared to incorporating internationalization in ruby.
  2. Java does not extend any special support for pluralisation But we can pluralize our strings by breaking them and storing them with separate keys. Java internationalization is much more advanced then ruby internationalization.
  3. Java internationalization provides a lot of features as opposed to ruby. Ruby at the most basic level cares only about looking up translations,formating date/time and currency. Java supports the following internationalization aspects looking up translations,formating date/time and currency, time zones, calendar systems, collation, character encoding, etc.

Python

Introduction

Internationalization in python is achieved using the gettext module.

  1. In python, the strings that are to be translated are first marked as translatable using the _() function.
  2. The pygettext program is then run on the source code to gather all the translatable strings.
  3. The pygettext generates a human readable .pot file which is given to the translators for translation.
  4. The translators provide a .po which is converted to a .mo file using msgfmt.py program.The .mo files are then used for translations at runtime.

Comparison with Rails

  1. In Rails, we can directly specify key value pairs in the .yml or .rb file whereas in python, we have to go through the above process in order to get the translation resources
  2. Strong internationalization and UNICODE support in python makes it a strong contender for internationalization.

See Also

http://rubylearning.com/blog/2012/07/24/minimal-i18n-with-rails-3-2/
http://www.artweb-design.de/2009/7/19/experimental-extensions-in-i18n-pluralization-fallbacks-gettext-cache-and-chain
http://asciicasts.com/episodes/256-i18n-backends
http://devzone.zend.com/1500/internationalization-in-php-53/
http://www.php.net/manual/en/intro.intl.php
http://www.php.net/manual/en/intl.requirements.php
http://www.php.net/manual/en/intl.installation.php
http://www.php.net/manual/en/intl.configuration.php
http://www.php.net/manual/en/intl.examples.basic.php
http://www.python.org/doc//current/library/gettext.html#internationalizing-your-programs-and-modules

References


<references/>