Demo: Localizzazione in Rails

Ultimamente molto lavoro è stato fatto da Sven Fuchs e outros per facilitare l’internazionalizzazione e la localizzazione di Rails.

Questa applicazione di esempio prova a mostrare come sia possibile utilizzare le funzionalità che sono state fino ad ora implementate per localizzare buona parte della tua applicazione Rails.

Articolo Originale

Setup

Freezing di Rails edge e installazione plugin delle date

Per utilizzare tutte le funzionalità implementate è necessario aggiornare per il progetto Ruby on Rails all’ultima versione di sviluppo.

    rails i18n_test
    rake rails:freeze:edge

Per i formati di data e ora, è inoltre necessario installare il plugin localized_dates:

    ./script/plugin install git://github.com/clemens/localized_dates.git

Procediamo inoltre all’installazione del plugin in_place_editing

    ./script/plugin install git://github.com/dcrec1/in_place_editing.git

Configurazione del locale

Il posto migliore in cui inserire la configurazione del locale all’interno della propria applicazione Ruby on Rails è, secondo la mia opinione, in config/locale. Il plugin localized_dates procederà alla copia delle configurazioni per due locale in questa directory, en-US e de-AT.

Questa è la configurazione del locale utilizzata per l’applicazione dimostrativa:

    0: I18n.backend.store_translations :'en-US', {
    1:   :date => {
    2:     :formats => {
    3:       :default      => "%Y-%m-%d",
    4:       :short        => "%e %b",
    5:       :long         => "%B %e, %Y",
    6:       :long_ordinal => lambda { |date| "%B #{date.day.ordinalize}, %Y" }
    7:     },
    8:     :day_names => Date::DAYNAMES,
    9:     :abbr_day_names => Date::ABBR_DAYNAMES,
    10:     :month_names => Date::MONTHNAMES,
    11:     :abbr_month_names => Date::ABBR_MONTHNAMES,
    12:     :order => [:year, :month, :day]
    13:   },
    14:   :time => {
    15:     :formats => {
    16:       :default      => "%a %b %d %H:%M:%S %Z %Y",
    17:       :time         => "%H:%M",
    18:       :short        => "%d %b %H:%M",
    19:       :long         => "%B %d, %Y %H:%M",
    20:       :long_ordinal => lambda { |time| "%B #{time.day.ordinalize}, %Y %H:%M" }
    21:     },
    22:     :datetime => {
    23:       :formats => {
    24:         :default => "%Y-%m-%dT%H:%M:%S%Z"
    25:       }
    26:     },
    27:     :time_with_zone => {
    28:       :formats => {
    29:         :default => lambda { |time| "%Y-%m-%d %H:%M:%S #{time.formatted_offset(false, 'UTC')}" }
    30:       }
    31:     },
    32:     :am => 'am',
    33:     :pm => 'pm'
    34:   }  ,
    35:     :txt => {
    36:       :main_title => "Localizing Rails",
    37:       :app_name => "Demo Application",
    38:       :sub_title => "how to localize your app with Rails' new i18n features",
    39:       :contents => "Contents",
    40:       :menu => {
    41:         :introduction => "Introduction",
    42:         :about => "About",
    43:         :setup => "Setup",
    44:         :date_formats => "Date formats",
    45:         :time_formats => "Time formats"
    46:       },
    47:       :about => {
    48:         :title => "About this demo app",
    49:         :author => "This demo app was written by {{mail_1}}.",
    50:         :feedback => "If you have any feedback, please feel free to drop me a line. Also visit {{blog_href}} where I regularly blog about Rails and other stuff.",
    51:         :licence => "This demo app and all its contents are licensed under the {{licence_href}}. If you want to use it in ways prohibited by this license, please contact me and ask my permission."
    52:       },
    53:       :active_record => {
    54:         :too_lazy => "No examples here since I'm too lazy to think of attributes to show all custom error messages. ;-)",
    55:         :easy_to_understand => "It's quite easy to understand, though."
    56:       },
    57:       :date_formats => {
    58:         :rails_standards_work => "Rails standard formats (Date::DATE_FORMATS) still work:"
    59:       },
    60:       :date_helper => {
    61:         :date_time_title => "Date/Time distance",
    62:         :forms_title => "Forms"
    63:       },
    64:       :index => {
    65:         :introduction => "Lately, a lot of work has been done by {{sven_blog}} and {{sven_github}} to facilitate future internationalization and localization of Rails.",
    66:         :story_so_far => "This demo app tries to show you how you can use the features that have been implemented so far to localize big parts of your Rails application."
    67:       },
    68:       :number_helper => {
    69:         :note_one => "Note: number_to_phone hasn't been localized yet and probably never will be - at least not in core. Look out for new internationalization/localization plugins like a new version of {{globalize}} as they will probably support stuff like that.",
    70:         :note_two => "Another note: number_to_currency, number_to_percentage and number_to_human_size all use number_with_precision internally and number_with_precision uses number_with_delimiter internally."
    71:       },
    72:       :setup => {
    73:         :freezing_edge_and_adding => "Freezing Edge and installing the localized_dates plugin",
    74:         :you_need_to_be_on_edge => "You need to be on Edge Rails in order to use the Rails i18n features:",
    75:         :date_time_formats => "For date and time formats, you also need to install the {{localized_dates_link}}:",
    76:         :config_locale => "Configuring the locale",
    77:         :best_place => "The best place to put your locale configuration, in my opinion, is config/locales. The localized_dates plugin will copy two locales, en-US and de-AT, in this directory. You can extend or modify them and also create new locales.",
    78:         :locale => "Here's the demo locale that was used for this demo application:",
    79:         :defaults => "You also need to set up the default locale and/or locale in your environment.rb or an initializer.",
    80:         :locale_structure_title => "A word on the structure of locales",
    81:         :locale_structure_number => "You may have noticed that inside the :number part of the locale, we defined :format and :currency. In general, locales are structured hierarchically - i.e. a currencies are numbers, percentages are numbers, etc. :currency can either override the basic :format settings (in our case, we set :precision to 2 instead of 3) or extend them (we add two new options, :unit and :format).",
    82:         :locale_structure_date_time => "The same holds true for dates and times: If needed, :datetime and :time_with_zone can be used to specifically address formatting of their respective types instead of just relying on the settings for :time. Note, however, that usually you want to use the same formats as :time."
    83:       },
    84:       :time_formats => {
    85:         :rails_standards_work => "Rails standard formats (Time::DATE_FORMATS) still work:"
    86:       },
    87:         :ipe => {
    88:           :click => "click here!"
    89:         }
    90:     }
    91: }

Inoltre è necessario procedere all’impostazione del locale di default nel file environment.rb oppure in un inizializzatore:

I18n.default_locale = 'en-US'
I18n.locale         = 'en-US'

Alcune note sulla struttura dei locale

Avrete notato che nella sezione del locale definita da :number abbiamo definito :format e :currency. In generale i locale sono strutturati gerarchicamente – ad esempio una :currency è un numero, una percentuale è un numero -. :currency può effettuare un override del :format di :number (nel nostro caso abbiamo impostato :precision a 2 al posto di 3) oppure estenderlo (nel nostro caso abbiamo aggiunto due nuove opzioni, :unit e :format).

Lo stesso vale per le date e l’ora: se necessario, :datetime e :time_with_zone possono essere utilizzati per specificare il formato dei rispettivi tipi invece che semplicemente affidarsi all’impostazione specificata per :time.

Formati della data

Date.today.to_s 2008-08-27
Date.today.to_s(:default) 2008-08-27
Date.today.to_s(:short) Aug 27
Date.today.to_s(:long) August 27, 2008
Date.today.to_s(:long_ordinal) August 27th, 2008
Date.today.to_s(:only_day) 2008-08-27

I formati standard di Rails (Date::DATE_FORMATS) continuano comunque a funzionare correttamente.

Date.today.to_s(:db) 2008-08-27
Date.today.to_s(:number) 20080827
Date.today.to_s(:rfc822) 27 Aug 2008

27 Agosto 2008
Categorie: Ruby on Rails 2.0, Ruby

AddThis Social Bookmark Button

Articoli simili

Commenti

Comments are closed.