ea07d0526eaf812d1f029b5d87689d6257fe6390
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / gettext / helpers.rb
1 require 'i18n/gettext'
2
3 module I18n
4   module Gettext
5     # Implements classical Gettext style accessors. To use this include the
6     # module to the global namespace or wherever you want to use it.
7     #
8     #   include I18n::Gettext::Helpers
9     module Helpers
10       def gettext(msgid, options = {})
11         I18n.t(msgid, { :default => msgid, :separator => '|' }.merge(options))
12       end
13       alias _ gettext
14
15       def sgettext(msgid, separator = '|')
16         scope, msgid = I18n::Gettext.extract_scope(msgid, separator)
17         I18n.t(msgid, :scope => scope, :default => msgid, :separator => separator)
18       end
19       alias s_ sgettext
20
21       def pgettext(msgctxt, msgid)
22         separator = I18n::Gettext::CONTEXT_SEPARATOR
23         sgettext([msgctxt, msgid].join(separator), separator)
24       end
25       alias p_ pgettext
26
27       def ngettext(msgid, msgid_plural, n = 1)
28         nsgettext(msgid, msgid_plural, n)
29       end
30       alias n_ ngettext
31
32       # Method signatures:
33       #   nsgettext('Fruits|apple', 'apples', 2)
34       #   nsgettext(['Fruits|apple', 'apples'], 2)
35       def nsgettext(msgid, msgid_plural, n = 1, separator = '|')
36         if msgid.is_a?(Array)
37           msgid, msgid_plural, n, separator = msgid[0], msgid[1], msgid_plural, n
38           separator = '|' unless separator.is_a?(::String)
39         end
40
41         scope, msgid = I18n::Gettext.extract_scope(msgid, separator)
42         default = { :one => msgid, :other => msgid_plural }
43         I18n.t(msgid, :default => default, :count => n, :scope => scope, :separator => separator)
44       end
45       alias ns_ nsgettext
46
47       # Method signatures:
48       #   npgettext('Fruits', 'apple', 'apples', 2)
49       #   npgettext('Fruits', ['apple', 'apples'], 2)
50       def npgettext(msgctxt, msgid, msgid_plural, n = 1)
51         separator = I18n::Gettext::CONTEXT_SEPARATOR
52
53         if msgid.is_a?(Array)
54           msgid_plural, msgid, n = msgid[1], [msgctxt, msgid[0]].join(separator), msgid_plural
55         else
56           msgid = [msgctxt, msgid].join(separator)
57         end
58
59         nsgettext(msgid, msgid_plural, n, separator)
60       end
61       alias np_ npgettext
62     end
63   end
64 end