Giter Club home page Giter Club logo

webingart / rails-i18n-translation-inheritance-helper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kipcole9/rails-i18n-translation-inheritance-helper

0.0 1.0 0.0 68 KB

If I18n.translate() fails in a locale, try a broader one. ie. If it fails in en-UK, try in 'en', or 'en-US', 'en-AU', 'en-CA' or other 'en' locales you have installed. This allows simple inheritance to cater for small but important differences between regions of the same language.

License: MIT License

rails-i18n-translation-inheritance-helper's Introduction

I18N Translation Helper
=======================

Installation
============

As a plugin:
	script/plugin install git://github.com/kipcole9/rails-i18n-translation-inheritance-helper.git
	
Or from git directly as a plugin to your project:
	git submodule add git://github.com/kipcole9/rails-i18n-translation-inheritance-helper.git vendor/plugins/i18n_translation_helper
	
	
Description
===========

Adds a fallback chain to translation lookup in Rails i18n.
When installed (plugin, or otherwise mixed in) the method 'translate'
will now attempt to find a translation in a broader scope locale
or 'sister' locals if there are any.

This means that it is more practical to have regional variations of
locales where there are only minor differences.

This updated version defines a lookup_chain as follows:

1. lookup in supplied or default locale (ie. "en-UK")
2. then in the broader locale ("en")
3. then any 'sister' locales ('en-GB', 'en-US', 'en-AU', ...). 

NOTE
===
To lookup the 'sister' locales requires that the version of I18n 
installed supports the #available_locales call.

This call is supported in the I18n gem version: svenfuchs-i18n (0.1.2) or in sven's
github repository.  You can also add the required functions by adding the following
to an initializer in your rails project:

module I18n
  class << self
    def available_locales
      backend.available_locales
    end
  end

  module Backend
    class Simple
      def available_locales
        translations.keys
      end
    end
  end
end

Performance
===========

Note that for the simple cases of a skinny narrow scope locale (ie 'en-GB') where
most keys are stored in the broader locale (ie. 'en') then performance will be twice
as slow on average compared with the standard I18n#translate function.

More experimentation to come yet.....

How this fits in an application
===============================

For example, the only practical difference between en-US and en-UK
would be the currency symbol ($ or £).

By default, the full set of rails 'en' rails localisation files would need to be
copied and maintained.

With this approach, an 'en-UK' localisation file for rails just has to maintain
the differences from the 'en' version.

An example would be:

en-UK:
  support:
    number:
      # Used in number_to_currency()
      currency:
          unit: "£"

Any lookup in the 'en-UK' local that fails will be redirected to the 'en' locale
for translation.

This approach can also be used for regional language variations in other
languages as well (pt-PT vs. pt-BR; fr-FR vs. fr-CA; es-ES vs. es-MX) - for both application
localisation files and for rails' own localisations.

This would be better (and would execute faster) if implemented in the I18n
core module directly.

Example
=======

Using the en-UK localisation file above:

	I18n.locale = "en-UK"
	I18n.translate('support.number.currency.unit')	=> "£"	# Resolved from 'en-UK'
	I18n.translate('support.array.words_connector')	=> ", " # Resolved from default 'en'

Also added is a new function #locate which can be used as a debugging tool
to tell you in which locale a translation was found.

	I18n.locate('support.array.words_connector')	=> "en: ', '"
	

Contact
=======
kip cole 9 at g mail dot com
(remote the spaces etc etc)



Copyright (c) 2009 Kip Cole, released under the MIT license

rails-i18n-translation-inheritance-helper's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.