svenfuchs-i18n 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/i18n.rb +181 -0
  2. metadata +66 -0
data/lib/i18n.rb ADDED
@@ -0,0 +1,181 @@
1
+ # Authors:: Matt Aimonetti (http://railsontherun.com/),
2
+ # Sven Fuchs (http://www.artweb-design.de),
3
+ # Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
4
+ # Saimon Moore (http://saimonmoore.net),
5
+ # Stephan Soller (http://www.arkanis-development.de/)
6
+ # Copyright:: Copyright (c) 2008 The Ruby i18n Team
7
+ # License:: MIT
8
+ require 'i18n/backend/simple'
9
+ require 'i18n/exceptions'
10
+
11
+ module I18n
12
+ @@backend = nil
13
+ @@load_paths = []
14
+ @@default_locale = :'en-US'
15
+ @@exception_handler = :default_exception_handler
16
+
17
+ class << self
18
+ # Returns the current backend. Defaults to +Backend::Simple+.
19
+ def backend
20
+ @@backend ||= Backend::Simple.new
21
+ end
22
+
23
+ # Sets the current backend. Used to set a custom backend.
24
+ def backend=(backend)
25
+ @@backend = backend
26
+ end
27
+
28
+ # Returns the current default locale. Defaults to 'en-US'
29
+ def default_locale
30
+ @@default_locale
31
+ end
32
+
33
+ # Sets the current default locale. Used to set a custom default locale.
34
+ def default_locale=(locale)
35
+ @@default_locale = locale
36
+ end
37
+
38
+ # Returns the current locale. Defaults to I18n.default_locale.
39
+ def locale
40
+ Thread.current[:locale] ||= default_locale
41
+ end
42
+
43
+ # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
44
+ def locale=(locale)
45
+ Thread.current[:locale] = locale
46
+ end
47
+
48
+ # Sets the exception handler.
49
+ def exception_handler=(exception_handler)
50
+ @@exception_handler = exception_handler
51
+ end
52
+
53
+ # Allow clients to register paths providing translation data sources. The
54
+ # backend defines acceptable sources.
55
+ #
56
+ # E.g. the provided SimpleBackend accepts a list of paths to translation
57
+ # files which are either named *.rb and contain plain Ruby Hashes or are
58
+ # named *.yml and contain YAML data. So for the SimpleBackend clients may
59
+ # register translation files like this:
60
+ # I18n.load_paths << 'path/to/locale/en-US.yml'
61
+ def load_paths
62
+ @@load_paths
63
+ end
64
+
65
+ # Translates, pluralizes and interpolates a given key using a given locale,
66
+ # scope, and default, as well as interpolation values.
67
+ #
68
+ # *LOOKUP*
69
+ #
70
+ # Translation data is organized as a nested hash using the upper-level keys
71
+ # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
72
+ # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
73
+ #
74
+ # Translations can be looked up at any level of this hash using the key argument
75
+ # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
76
+ # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
77
+ #
78
+ # Key can be either a single key or a dot-separated key (both Strings and Symbols
79
+ # work). <em>E.g.</em>, the short format can be looked up using both:
80
+ # I18n.t 'date.formats.short'
81
+ # I18n.t :'date.formats.short'
82
+ #
83
+ # Scope can be either a single key, a dot-separated key or an array of keys
84
+ # or dot-separated keys. Keys and scopes can be combined freely. So these
85
+ # examples will all look up the same short date format:
86
+ # I18n.t 'date.formats.short'
87
+ # I18n.t 'formats.short', :scope => 'date'
88
+ # I18n.t 'short', :scope => 'date.formats'
89
+ # I18n.t 'short', :scope => %w(date formats)
90
+ #
91
+ # *INTERPOLATION*
92
+ #
93
+ # Translations can contain interpolation variables which will be replaced by
94
+ # values passed to #translate as part of the options hash, with the keys matching
95
+ # the interpolation variable names.
96
+ #
97
+ # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
98
+ # value for the key +bar+ will be interpolated into the translation:
99
+ # I18n.t :foo, :bar => 'baz' # => 'foo baz'
100
+ #
101
+ # *PLURALIZATION*
102
+ #
103
+ # Translation data can contain pluralized translations. Pluralized translations
104
+ # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
105
+ #
106
+ # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
107
+ # pluralization rules. Other algorithms can be supported by custom backends.
108
+ #
109
+ # This returns the singular version of a pluralized translation:
110
+ # I18n.t :foo, :count => 1 # => 'Foo'
111
+ #
112
+ # These both return the plural version of a pluralized translation:
113
+ # I18n.t :foo, :count => 0 # => 'Foos'
114
+ # I18n.t :foo, :count => 2 # => 'Foos'
115
+ #
116
+ # The <tt>:count</tt> option can be used both for pluralization and interpolation.
117
+ # <em>E.g.</em>, with the translation
118
+ # <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
119
+ # be interpolated to the pluralized translation:
120
+ # I18n.t :foo, :count => 1 # => '1 foo'
121
+ #
122
+ # *DEFAULTS*
123
+ #
124
+ # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
125
+ # I18n.t :foo, :default => 'default'
126
+ #
127
+ # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
128
+ # translation for <tt>:foo</tt> was found:
129
+ # I18n.t :foo, :default => :bar
130
+ #
131
+ # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
132
+ # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
133
+ # I18n.t :foo, :default => [:bar, 'default']
134
+ #
135
+ # <b>BULK LOOKUP</b>
136
+ #
137
+ # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
138
+ # I18n.t [:foo, :bar]
139
+ #
140
+ # Can be used with dot-separated nested keys:
141
+ # I18n.t [:'baz.foo', :'baz.bar']
142
+ #
143
+ # Which is the same as using a scope option:
144
+ # I18n.t [:foo, :bar], :scope => :baz
145
+ def translate(key, options = {})
146
+ locale = options.delete(:locale) || I18n.locale
147
+ backend.translate locale, key, options
148
+ rescue I18n::ArgumentError => e
149
+ raise e if options[:raise]
150
+ send @@exception_handler, e, locale, key, options
151
+ end
152
+ alias :t :translate
153
+
154
+ # Localizes certain objects, such as dates and numbers to local formatting.
155
+ def localize(object, options = {})
156
+ locale = options[:locale] || I18n.locale
157
+ format = options[:format] || :default
158
+ backend.localize(locale, object, format)
159
+ end
160
+ alias :l :localize
161
+
162
+ protected
163
+ # Handles exceptions raised in the backend. All exceptions except for
164
+ # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
165
+ # was caught and the option :raise is not set the handler returns an error
166
+ # message string containing the key/scope.
167
+ def default_exception_handler(exception, locale, key, options)
168
+ return exception.message if MissingTranslationData === exception
169
+ raise exception
170
+ end
171
+
172
+ # Merges the given locale, key and scope into a single array of keys.
173
+ # Splits keys that contain dots into multiple keys. Makes sure all
174
+ # keys are Symbols.
175
+ def normalize_translation_keys(locale, key, scope)
176
+ keys = [locale] + Array(scope) + [key]
177
+ keys = keys.map{|k| k.to_s.split(/\./) }
178
+ keys.flatten.map{|k| k.to_sym}
179
+ end
180
+ end
181
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svenfuchs-i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sven Fuchs
8
+ - Matt Aimonetti
9
+ - Stephan Soller
10
+ - Saimon Moore
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2008-06-13 00:00:00 -07:00
16
+ default_executable:
17
+ dependencies: []
18
+
19
+ description: Add Internationalization to your Ruby application.
20
+ email: rails-patch-i18n@googlegroups.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files: []
26
+
27
+ files:
28
+ - lib/i18n/backend/minimal.rb
29
+ - lib/i18n/core_ext.rb
30
+ - lib/i18n/localization.rb
31
+ - lib/i18n/translation.rb
32
+ - lib/i18n.rb
33
+ - LICENSE
34
+ - README
35
+ - spec/core_ext_spec.rb
36
+ - spec/i18n_spec.rb
37
+ - spec/spec.opts
38
+ - spec/spec/helper.rb
39
+ has_rdoc: false
40
+ homepage: http://groups.google.com/group/rails-patch-i18n
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Internationalization for Ruby
65
+ test_files: []
66
+