Module: Merb::Global

Included in:
Controller
Defined in:
lib/merb_global/base.rb,
lib/merb_global/config.rb,
lib/merb_global/locale.rb,
lib/merb_global/plural.rb,
lib/merb_global/providers.rb,
lib/merb_global/date_providers.rb,
lib/merb_global/message_providers.rb,
lib/merb_global/numeric_providers.rb,
lib/merb_global/date_providers/fork.rb,
lib/merb_global/message_providers/mock.rb,
lib/merb_global/message_providers/yaml.rb,
lib/merb_global/numeric_providers/fork.rb,
lib/merb_global/numeric_providers/java.rb,
lib/merb_global/message_providers/sequel.rb,
lib/merb_global/message_providers/gettext.rb,
lib/merb_global/message_providers/data_mapper.rb,
lib/merb_global/message_providers/active_record.rb

Defined Under Namespace

Modules: DateProviders, MessageProviders, NumericProviders, Plural, Providers Classes: Locale

Constant Summary collapse

@@config =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config(keys, default = nil) ⇒ Object

call-seq:

config(key)                        => value
config([key1, key2, ...])          => value
config(key, default)               => value
config([key1, key2, ...], default) => value

Lookup the configuration

Params

key<Symbol>

A key

keys<Array>

Keys

default<Object>

A default value

Returns

value<Object>

Object read from configuration or default

Examples

Merb::Global.config [:gettext, :domain], 'merbapp'



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/merb_global/config.rb', line 21

def self.config(keys, default = nil)
  keys = [keys] unless keys.is_a? Array
  #if @@config.nil?
    @@config = Merb::Plugins.config[:merb_global].dup
    unless Merb::Plugins.config[:merb_global][Merb.env].nil?
      @@config.merge! Merb::Plugins.config[:merb_global][Merb.env]
    end
  #end
  current = @@config
  while current.respond_to?(:[]) and not keys.empty?
    current = current[keys.shift]
  end
  (keys.empty? and not current.nil?) ? current : default
end

.DateProvider(provider_name) ⇒ Object

Perform the registration

Parameters

name<~to_sym>

Name under which it is registered



36
37
38
39
40
41
42
43
44
45
# File 'lib/merb_global/date_providers.rb', line 36

def self.DateProvider(provider_name)
  Module.new do
    @@rb_date_provider_name = provider_name
    include Merb::Global::DateProviders::Base
    
    def self.included(klass)
      Merb::Global::DateProviders.register @@rb_date_provider_name, klass
    end
  end
end

.MessageProvider(provider_name, *opts) ⇒ Object

Perform the registration

Parameters

provider_name<~to_sym>

Name under which it is registred

opts<Array>

Additional imformations

Options

importer

Can perform import

exporter

Can perform export



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/merb_global/message_providers.rb', line 127

def self.MessageProvider(provider_name, *opts)
  Module.new do
    @@mg_message_provider_name = provider_name
    
    include Merb::Global::MessageProviders::Base
    if opts.include? :importer
      include Merb::Global::MessageProviders::Base::Importer
    end
    if opts.include? :exporter
      include Merb::Global::MessageProviders::Base::Exporter
    end
    
    def self.included(klass)
      Merb::Global::MessageProviders.register @@mg_message_provider_name,
                                              klass
    end
  end
end

.NumericProvider(provider_name) ⇒ Object

Perform the registration

Parameters

name<~to_sym>

Name under which it is registred



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/merb_global/numeric_providers.rb', line 36

def self.NumericProvider(provider_name)
  Module.new do
    @@rb_numeric_provider_name = provider_name
    include Merb::Global::NumericProviders::Base
    
    def self.included(klass)
      Merb::Global::NumericProviders.register @@rb_numeric_provider_name,
                                              klass
    end
  end
end

Instance Method Details

#_(*args) ⇒ Object

call-seq:

_(singular, opts)          => translated message
_(singlular, plural, opts) => translated message
_(date, format)            => localized date
_(number)                  => localized number

Translate a string.

Parameters

singular<String>

A string to translate

plural<String>

A plural form of string

opts<Hash>

An options hash (see below)

date<~strftime>

A date to localize

format<String>

A format of string (should be compatibile with strftime)

number<Numeric>

A numeber to localize

Options (opts)

:locale<Locale>

A language to translate on

:n<Fixnum>

A number of objects (for messages)

Returns

translated<String>

A translated string

Example

render _('%d file deleted', '%d files deleted', :n => del) % del



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/merb_global/base.rb', line 75

def _(*args)
  opts = {:locale => Merb::Global::Locale.current, :n => 1}
  opts.merge! args.pop if args.last.is_a? Hash
  if args.first.respond_to? :strftime
    if args.size == 2
      Merb::Global::DateProviders.provider.localize opts[:locale], args[0], args[1]
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
    end
  elsif args.first.is_a? Numeric
    if args.size == 1
      Merb::Global::NumericProviders.provider.localize opts[:locale], args.first
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 1)"
    end
  elsif args.first.is_a? String
    if args.size == 1
      Merb::Global::MessageProviders.provider.localize args[0], nil, opts[:n], opts[:locale]
    elsif args.size == 2
      Merb::Global::MessageProviders.provider.localize args[0], args[1], opts[:n], opts[:locale]
    else
      raise ArgumentError,
            "wrong number of arguments (#{args.size} for 1-2)"
    end
  else
    raise ArgumentError,
          "wrong type of arguments - see documentation for details"
  end
end