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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 201: Line 201:
#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.
#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.
#Whenever we need to add more features to internationalization, in PHP we just have to add a module to the internationalization extension.
#In PHP, we have functions that are written specifically to return internationalization errors. This helps in making debugging easy in case i18n fails. In ruby we don’t have such dedicated functions.


= References =
= References =

Revision as of 00:25, 18 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.
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.

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 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.
  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 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
  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. Example http://study.es
You can implement it like this in your ApplicationController before_action
  1. 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 -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.
  2. 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


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:

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.

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.
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)

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 PHP, Java

Internationalization in PHP

In PHP, we need to install/enable extensions to support internationalization(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 provided 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 Ruby :

  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.

References

http://guides.rubyonrails.org/3_0_release_notes.html
http://rubylearning.com/blog/2012/07/24/minimal-i18n-with-rails-3-2/
http://blog.lingohub.com/developers/2013/08/internationalization-for-ruby-i18n-gem/
http://vimeo.com/12665914
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://blog.lingohub.com/developers/2013/08/i18n-gem-advanced-features-ruby-rails-internationalization/
http://en.wikipedia.org/wiki/Internationalization_and_localization
http://www.ruby-doc.org/gems/docs/s/synergy_russian-0.2.8/I18n/Backend/Simple.html
https://github.com/svenfuchs/i18n
http://guides.rubyonrails.org/v2.3.11/i18n.html
http://www.slideshare.net/Belighted/internationalization-in-rails-22-3120853
http://www.slideshare.net/graysky/rails-internationalization
http://www.slideshare.net/
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