Skip to content

Commit

Permalink
remove whitespace and backport Tarmo's fix for making simple backend …
Browse files Browse the repository at this point in the history
…able to store false values (and not confuse them with nil) http://github.com/rails/rails/commit/d69b4b7bea28fcab04f61afe381e06fa8e37b429
  • Loading branch information
Sven Fuchs committed Oct 25, 2008
1 parent 250c045 commit 39d9a47
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 210 deletions.
80 changes: 41 additions & 39 deletions lib/i18n.rb
Expand Up @@ -2,39 +2,39 @@
# Sven Fuchs (http://www.artweb-design.de),
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
# Saimon Moore (http://saimonmoore.net),
# Stephan Soller (http://www.arkanis-development.de/)
# Stephan Soller (http://www.arkanis-development.de/)
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
# License:: MIT
require 'i18n/backend/simple'
require 'i18n/exceptions'

module I18n
module I18n
@@backend = nil
@@load_path = []
@@load_path = nil
@@default_locale = :'en-US'
@@exception_handler = :default_exception_handler

class << self
# Returns the current backend. Defaults to +Backend::Simple+.
def backend
@@backend ||= Backend::Simple.new
end

# Sets the current backend. Used to set a custom backend.
def backend=(backend)
def backend=(backend)
@@backend = backend
end

# Returns the current default locale. Defaults to 'en-US'
def default_locale
@@default_locale
@@default_locale
end

# Sets the current default locale. Used to set a custom default locale.
def default_locale=(locale)
@@default_locale = locale
def default_locale=(locale)
@@default_locale = locale
end

# Returns the current locale. Defaults to I18n.default_locale.
def locale
Thread.current[:locale] ||= default_locale
Expand All @@ -44,46 +44,48 @@ def locale
def locale=(locale)
Thread.current[:locale] = locale
end

# Sets the exception handler.
def exception_handler=(exception_handler)
@@exception_handler = exception_handler
end
# Allow clients to register paths providing translation data sources. The

# Allow clients to register paths providing translation data sources. The
# backend defines acceptable sources.
#
# E.g. the provided SimpleBackend accepts a list of paths to translation
# files which are either named *.rb and contain plain Ruby Hashes or are
# named *.yml and contain YAML data. So for the SimpleBackend clients may
# named *.yml and contain YAML data. So for the SimpleBackend clients may
# register translation files like this:
# I18n.load_path << 'path/to/locale/en-US.yml'
def load_path
@@load_path
@@load_path ||= []
end

# Sets the load path instance. Custom implementations are expected to
# behave like a Ruby Array.
def load_path=(load_path)
@@load_path = load_path
end
# Translates, pluralizes and interpolates a given key using a given locale,

# Translates, pluralizes and interpolates a given key using a given locale,
# scope, and default, as well as interpolation values.
#
# *LOOKUP*
#
# Translation data is organized as a nested hash using the upper-level keys
# as namespaces. <em>E.g.</em>, ActionView ships with the translation:
# Translation data is organized as a nested hash using the upper-level keys
# as namespaces. <em>E.g.</em>, ActionView ships with the translation:
# <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
#
# Translations can be looked up at any level of this hash using the key argument
# and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
#
# Translations can be looked up at any level of this hash using the key argument
# and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
# returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
#
# Key can be either a single key or a dot-separated key (both Strings and Symbols
#
# Key can be either a single key or a dot-separated key (both Strings and Symbols
# work). <em>E.g.</em>, the short format can be looked up using both:
# I18n.t 'date.formats.short'
# I18n.t :'date.formats.short'
#
#
# Scope can be either a single key, a dot-separated key or an array of keys
# or dot-separated keys. Keys and scopes can be combined freely. So these
# examples will all look up the same short date format:
Expand All @@ -96,9 +98,9 @@ def load_path=(load_path)
#
# Translations can contain interpolation variables which will be replaced by
# values passed to #translate as part of the options hash, with the keys matching
# the interpolation variable names.
# the interpolation variable names.
#
# <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
# <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
# value for the key +bar+ will be interpolated into the translation:
# I18n.t :foo, :bar => 'baz' # => 'foo baz'
#
Expand All @@ -107,7 +109,7 @@ def load_path=(load_path)
# Translation data can contain pluralized translations. Pluralized translations
# are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
#
# Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
# Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
# pluralization rules. Other algorithms can be supported by custom backends.
#
# This returns the singular version of a pluralized translation:
Expand All @@ -116,9 +118,9 @@ def load_path=(load_path)
# These both return the plural version of a pluralized translation:
# I18n.t :foo, :count => 0 # => 'Foos'
# I18n.t :foo, :count => 2 # => 'Foos'
#
# The <tt>:count</tt> option can be used both for pluralization and interpolation.
# <em>E.g.</em>, with the translation
#
# The <tt>:count</tt> option can be used both for pluralization and interpolation.
# <em>E.g.</em>, with the translation
# <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
# be interpolated to the pluralized translation:
# I18n.t :foo, :count => 1 # => '1 foo'
Expand All @@ -128,11 +130,11 @@ def load_path=(load_path)
# This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
# I18n.t :foo, :default => 'default'
#
# This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
# This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
# translation for <tt>:foo</tt> was found:
# I18n.t :foo, :default => :bar
#
# Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
# Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
# or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
# I18n.t :foo, :default => [:bar, 'default']
#
Expand All @@ -152,17 +154,17 @@ def translate(key, options = {})
rescue I18n::ArgumentError => e
raise e if options[:raise]
send @@exception_handler, e, locale, key, options
end
end
alias :t :translate

# Localizes certain objects, such as dates and numbers to local formatting.
def localize(object, options = {})
locale = options[:locale] || I18n.locale
format = options[:format] || :default
backend.localize(locale, object, format)
end
alias :l :localize

protected
# Handles exceptions raised in the backend. All exceptions except for
# MissingTranslationData exceptions are re-raised. When a MissingTranslationData
Expand All @@ -172,7 +174,7 @@ def default_exception_handler(exception, locale, key, options)
return exception.message if MissingTranslationData === exception
raise exception
end

# Merges the given locale, key and scope into a single array of keys.
# Splits keys that contain dots into multiple keys. Makes sure all
# keys are Symbols.
Expand Down

0 comments on commit 39d9a47

Please sign in to comment.