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