gettext 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/ChangeLog +128 -0
  2. data/NEWS +10 -0
  3. data/README +21 -23
  4. data/Rakefile +39 -20
  5. data/bin/rgettext +1 -1
  6. data/bin/rmsgfmt +1 -1
  7. data/bin/rmsgmerge +1 -1
  8. data/data/locale/cs/LC_MESSAGES/rails.mo +0 -0
  9. data/data/locale/cs/LC_MESSAGES/rgettext.mo +0 -0
  10. data/data/locale/de/LC_MESSAGES/rails.mo +0 -0
  11. data/data/locale/de/LC_MESSAGES/rgettext.mo +0 -0
  12. data/data/locale/el/LC_MESSAGES/rails.mo +0 -0
  13. data/data/locale/el/LC_MESSAGES/rgettext.mo +0 -0
  14. data/data/locale/es/LC_MESSAGES/rails.mo +0 -0
  15. data/data/locale/es/LC_MESSAGES/rgettext.mo +0 -0
  16. data/data/locale/fr/LC_MESSAGES/rails.mo +0 -0
  17. data/data/locale/fr/LC_MESSAGES/rgettext.mo +0 -0
  18. data/data/locale/it/LC_MESSAGES/rgettext.mo +0 -0
  19. data/data/locale/ja/LC_MESSAGES/rails.mo +0 -0
  20. data/data/locale/ja/LC_MESSAGES/rgettext.mo +0 -0
  21. data/data/locale/ko/LC_MESSAGES/rails.mo +0 -0
  22. data/data/locale/ko/LC_MESSAGES/rgettext.mo +0 -0
  23. data/data/locale/nl/LC_MESSAGES/rails.mo +0 -0
  24. data/data/locale/nl/LC_MESSAGES/rgettext.mo +0 -0
  25. data/data/locale/pt_BR/LC_MESSAGES/rails.mo +0 -0
  26. data/data/locale/pt_BR/LC_MESSAGES/rgettext.mo +0 -0
  27. data/data/locale/sv/LC_MESSAGES/rgettext.mo +0 -0
  28. data/ext/gettext/extconf.rb +20 -0
  29. data/ext/gettext/{gettext/_locale.c → locale_system.c} +15 -19
  30. data/lib/gettext.rb +186 -38
  31. data/lib/gettext/cgi.rb +35 -69
  32. data/lib/gettext/container.rb +14 -5
  33. data/lib/gettext/erb.rb +30 -2
  34. data/lib/gettext/iconv.rb +19 -4
  35. data/lib/gettext/locale.rb +188 -39
  36. data/lib/gettext/locale_cgi.rb +102 -0
  37. data/lib/gettext/locale_object.rb +131 -0
  38. data/lib/gettext/locale_posix.rb +50 -0
  39. data/lib/gettext/locale_table_win32.rb +224 -182
  40. data/lib/gettext/locale_win32.rb +59 -35
  41. data/lib/gettext/mo.rb +2 -2
  42. data/lib/gettext/parser/activerecord.rb +19 -5
  43. data/lib/gettext/parser/erb.rb +6 -3
  44. data/lib/gettext/parser/glade.rb +5 -5
  45. data/lib/gettext/parser/ruby.rb +6 -6
  46. data/lib/gettext/poparser.rb +7 -6
  47. data/lib/gettext/rails.rb +122 -44
  48. data/lib/gettext/rgettext.rb +21 -8
  49. data/lib/gettext/rmsgfmt.rb +16 -8
  50. data/lib/gettext/rmsgmerge.rb +172 -50
  51. data/lib/gettext/string.rb +25 -3
  52. data/lib/gettext/textdomain.rb +89 -90
  53. data/lib/gettext/utils.rb +63 -3
  54. data/lib/gettext/version.rb +2 -2
  55. data/pkg/ruby-gettext-package-1.2.0/ext/gettext/extconf.rb +20 -0
  56. data/po/cs/rails.po +28 -28
  57. data/po/cs/rgettext.po +14 -14
  58. data/po/de/rails.po +29 -26
  59. data/po/de/rgettext.po +24 -17
  60. data/po/el/rails.po +26 -26
  61. data/po/el/rgettext.po +18 -15
  62. data/po/es/rails.po +26 -26
  63. data/po/es/rgettext.po +16 -16
  64. data/po/fr/rails.po +27 -27
  65. data/po/fr/rgettext.po +14 -14
  66. data/po/it/rgettext.po +14 -14
  67. data/po/ja/rails.po +24 -24
  68. data/po/ja/rgettext.po +14 -14
  69. data/po/ko/rails.po +24 -24
  70. data/po/ko/rgettext.po +17 -18
  71. data/po/nl/rails.po +27 -27
  72. data/po/nl/rgettext.po +18 -18
  73. data/po/pt_BR/rails.po +26 -26
  74. data/po/pt_BR/rgettext.po +14 -14
  75. data/po/rails.pot +26 -26
  76. data/po/rgettext.pot +14 -14
  77. data/po/sv/rgettext.po +14 -14
  78. data/post-setup.rb +8 -3
  79. data/pre-setup.rb +14 -1
  80. data/samples/cgi/http.rb +2 -1
  81. data/samples/makemo.rb +1 -1
  82. data/samples/rails/app/models/article.rb +15 -1
  83. data/samples/rails/locale/cs/LC_MESSAGES/blog.mo +0 -0
  84. data/samples/rails/locale/cs/LC_MESSAGES/gettext_plugin.mo +0 -0
  85. data/samples/rails/locale/de/LC_MESSAGES/blog.mo +0 -0
  86. data/samples/rails/locale/de/LC_MESSAGES/gettext_plugin.mo +0 -0
  87. data/samples/rails/locale/el/LC_MESSAGES/blog.mo +0 -0
  88. data/samples/rails/locale/el/LC_MESSAGES/gettext_plugin.mo +0 -0
  89. data/samples/rails/locale/en/LC_MESSAGES/blog.mo +0 -0
  90. data/samples/rails/locale/es/LC_MESSAGES/blog.mo +0 -0
  91. data/samples/rails/locale/es/LC_MESSAGES/gettext_plugin.mo +0 -0
  92. data/samples/rails/locale/fr/LC_MESSAGES/blog.mo +0 -0
  93. data/samples/rails/locale/fr/LC_MESSAGES/gettext_plugin.mo +0 -0
  94. data/samples/rails/locale/ja/LC_MESSAGES/blog.mo +0 -0
  95. data/samples/rails/locale/ja/LC_MESSAGES/gettext_plugin.mo +0 -0
  96. data/samples/rails/locale/ko/LC_MESSAGES/blog.mo +0 -0
  97. data/samples/rails/locale/ko/LC_MESSAGES/gettext_plugin.mo +0 -0
  98. data/samples/rails/locale/nl/LC_MESSAGES/blog.mo +0 -0
  99. data/samples/rails/locale/nl/LC_MESSAGES/gettext_plugin.mo +0 -0
  100. data/samples/rails/locale/pt_BR/LC_MESSAGES/blog.mo +0 -0
  101. data/samples/rails/locale/pt_BR/LC_MESSAGES/gettext_plugin.mo +0 -0
  102. data/samples/rails/po/blog.pot +1 -1
  103. data/samples/rails/po/cs/blog.po +1 -2
  104. data/samples/rails/po/cs/gettext_plugin.po +1 -1
  105. data/samples/rails/po/de/blog.po +1 -2
  106. data/samples/rails/po/de/gettext_plugin.po +1 -1
  107. data/samples/rails/po/el/blog.po +1 -1
  108. data/samples/rails/po/el/gettext_plugin.po +1 -1
  109. data/samples/rails/po/en/blog.po +1 -1
  110. data/samples/rails/po/es/blog.po +1 -2
  111. data/samples/rails/po/es/gettext_plugin.po +1 -1
  112. data/samples/rails/po/fr/blog.po +1 -1
  113. data/samples/rails/po/fr/gettext_plugin.po +1 -1
  114. data/samples/rails/po/gettext_plugin.pot +1 -1
  115. data/samples/rails/po/ja/blog.po +2 -1
  116. data/samples/rails/po/ja/gettext_plugin.po +1 -1
  117. data/samples/rails/po/ko/blog.po +1 -2
  118. data/samples/rails/po/ko/gettext_plugin.po +1 -1
  119. data/samples/rails/po/nl/blog.po +1 -3
  120. data/samples/rails/po/nl/gettext_plugin.po +1 -1
  121. data/samples/rails/po/pt_BR/blog.po +1 -1
  122. data/samples/rails/po/pt_BR/gettext_plugin.po +1 -1
  123. data/setup.rb +799 -574
  124. data/src/poparser.ry +6 -5
  125. data/test/gettext_runner.rb +3 -1
  126. data/test/gettext_test.rb +40 -24
  127. data/test/gettext_test_cgi.rb +60 -6
  128. data/test/gettext_test_locale.rb +136 -0
  129. data/test/gettext_test_parser.rb +12 -12
  130. data/test/locale/la/LC_MESSAGES/plural_error.mo +0 -0
  131. data/test/po/la/plural_error.po +21 -0
  132. data/test/test.bat +2 -0
  133. data/test/test.sh +2 -4
  134. data/test/{test_rubyparser_n.rb → test_rubyparser_n_.rb} +0 -0
  135. metadata +17 -12
  136. data/ext/gettext/gettext/MANIFEST +0 -3
  137. data/ext/gettext/gettext/extconf.rb +0 -20
  138. data/lib/gettext/locale_default.rb +0 -35
@@ -1,55 +1,79 @@
1
1
  =begin
2
- locale_win32.rb - Locale module for win32.
2
+ locale_win32.rb
3
3
 
4
- Copyright (C) 2002,2003 Masao Mutoh <mutoh@highway.ne.jp>
4
+ Copyright (C) 2002-2006 Masao Mutoh <mutoh@highway.ne.jp>
5
5
 
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
8
8
 
9
- $Id: locale_win32.rb,v 1.4 2005/12/29 07:02:52 mutoh Exp $
9
+ $Id: locale_win32.rb,v 1.12 2006/02/20 16:12:05 mutoh Exp $
10
10
  =end
11
11
 
12
12
  require 'gettext/locale_table_win32'
13
13
 
14
- module Locale
15
- @@locale_win32 = {}
16
- def __get(lctype = Locale::CTYPE)
17
- ret = nil
18
- if @@locale_win32[lctype]
19
- ret = @@locale_win32[lctype]
20
- else
21
- ["LC_ALL", "LC_MESSAGES", "LANG"].each do |env|
22
- ret = ENV[env]
23
- break if ret
14
+ module Locale #:nodoc:
15
+ # Locale::SystemWin32 module for win32.
16
+ module SystemWin32
17
+ extend Locale::System
18
+
19
+ @@default_locale = Locale::Object.new("en", nil, "CP1252")
20
+
21
+ module_function
22
+ # Gets the charset of the locale. ENV(LC_ALL > LC_CTYPE > LC_MESSAGES > LANG) >
23
+ # the default locale from GetUserDefaultLangID.
24
+ # * locale: Locale::Object
25
+ # * Returns the charset of the locale
26
+ def get_charset(locale)
27
+ loc = LocaleTable.find{|v| v[1] == locale.to_win}
28
+ loc ? loc[2] : "CP1252"
29
+ end
30
+
31
+ # Gets the system locale.
32
+ # * Returns the system locale (Locale::Object)
33
+ def system
34
+ lang = nil
35
+ ["LC_ALL", "LC_CTYPE", "LC_MESSAGES", "LANG"].each do |env|
36
+ lang = ENV[env]
37
+ if lang
38
+ ret = Locale::Object.new(lang)
39
+ ret.charset = get_charset(ret)
40
+ break
41
+ end
24
42
  end
25
- unless ret
26
- pri_id, sub_id = __locale_id
27
- lang = LocaleTable.assoc(pri_id)
28
- if lang.size == 3
29
- lang = lang[2].assoc(sub_id)
43
+ unless lang
44
+ lang = LocaleTable.assoc(locale_id)
45
+ if lang
46
+ ret = Locale::Object.new(lang[1], nil, lang[2])
47
+ else
48
+ ret = @@default_locale
30
49
  end
31
- ret = lang ? lang[1] : "C"
32
50
  end
33
- @@locale_win32[lctype] = ret
51
+ ret
34
52
  end
35
- ret
36
- end
37
53
 
38
- def set(lctype, loc)
39
- if lctype == ALL
40
- @@locale_win32[lctype] = loc
54
+ # Sets a default locale. en.UTF-8 is the default value if not set.
55
+ # * locale: Locale::Object object. You can't set nil.
56
+ # * Returns: self
57
+ def set_default_locale(locale)
58
+ raise "Wrong parameter: #{locale}" if locale.nil?
59
+ @@default_locale = locale
60
+ self
61
+ end
62
+
63
+ # Sets a default locale. en.UTF-8 is the default value if not set.
64
+ # * locale: Locale::Object
65
+ # * Returns: locale
66
+ def default_locale=(locale)
67
+ set_default_locale(locale)
68
+ locale
41
69
  end
42
- end
43
70
 
44
- unless defined? CTYPE
45
- CTYPE = 0
46
- NUMERIC = 1
47
- TIME = 2
48
- COLLATE = 3
49
- MONETARY = 4
50
- MESSAGES = 5
51
- ALL = 6
71
+ # Gets the default Locale::Object.
72
+ # * Returns: the default locale
73
+ def default_locale
74
+ @@default_locale
75
+ end
52
76
  end
53
- module_function :__get
77
+ @@locale_system_module = SystemWin32
54
78
  end
55
79
 
data/lib/gettext/mo.rb CHANGED
@@ -12,12 +12,12 @@
12
12
  of Ruby. License of Ruby is included with Ruby distribution in
13
13
  the file "README".
14
14
 
15
- $Id: mo.rb,v 1.2 2005/10/22 10:49:59 mutoh Exp $
15
+ $Id: mo.rb,v 1.3 2006/02/23 14:53:59 mutoh Exp $
16
16
  =end
17
17
 
18
18
  require 'gettext/iconv'
19
19
 
20
- class MOFile < Hash
20
+ class MOFile < Hash #:nodoc:
21
21
  class InvalidFormat < RuntimeError; end;
22
22
 
23
23
  Header = Struct.new(:magic,
@@ -7,7 +7,7 @@
7
7
  You may redistribute it and/or modify it under the same
8
8
  license terms as Ruby.
9
9
 
10
- $Id: activerecord.rb,v 1.5 2006/01/14 13:48:23 mutoh Exp $
10
+ $Id: activerecord.rb,v 1.6 2006/02/22 16:42:41 mutoh Exp $
11
11
  =end
12
12
 
13
13
  require 'gettext'
@@ -31,7 +31,7 @@ module GetText
31
31
  @ar_re = nil
32
32
 
33
33
  module_function
34
- def require_rails(file)
34
+ def require_rails(file) # :nodoc:
35
35
  begin
36
36
  require file
37
37
  rescue MissingSourceFile
@@ -39,6 +39,16 @@ module GetText
39
39
  end
40
40
  end
41
41
 
42
+ # Sets some preferences to parse ActiveRecord files.
43
+ #
44
+ # * config: a Hash of the config. It can takes some values below:
45
+ # * :use_classname: If true, the msgids of ActiveRecord become "ClassName|FieldName" (e.g. "Article|Title"). Otherwise the ClassName is not used (e.g. "Title"). Default is true.
46
+ # * :db_yml: the path of database.yml. Default is "config/database.yml".
47
+ # * :db_mode: the mode of the database. Default is "development"
48
+ # * :activerecord_classes: an Array of the superclass of the models. The classes should be String value. Default is ["ActiveRecord::Base"]
49
+ #
50
+ # "ClassName|FieldName" uses GetText.sgettext. So you don't need to translate the left-side of "|".
51
+ # See <Documents for Translators for more details(http://www.yotabanana.com/hiki/ruby-gettext-translate.html)>.
42
52
  def init(config)
43
53
  if config
44
54
  config.each{|k, v|
@@ -48,7 +58,7 @@ module GetText
48
58
  @ar_re = /class.*(#{@config[:activerecord_classes].join("|")})/
49
59
  end
50
60
 
51
- def parse(file, targets = [])
61
+ def parse(file, targets = []) # :nodoc:
52
62
  old_constants = constants
53
63
  begin
54
64
  eval(open(file).read)
@@ -83,7 +93,7 @@ module GetText
83
93
  targets
84
94
  end
85
95
 
86
- def target?(file)
96
+ def target?(file) # :nodoc:
87
97
  init(nil) unless @ar_re
88
98
  data = IO.readlines(file)
89
99
  data.each do |v|
@@ -91,7 +101,11 @@ module GetText
91
101
  unless @db
92
102
  require 'rubygems'
93
103
  require 'active_record'
94
- yml = YAML.load(ERB.new(IO.read(@config[:db_yml])).result)
104
+ begin
105
+ yml = YAML.load(ERB.new(IO.read(@config[:db_yml])).result)
106
+ rescue
107
+ return false
108
+ end
95
109
  ENV["RAILS_ENV"] = @config[:db_mode]
96
110
  require_rails 'config/boot.rb'
97
111
  require_rails 'config/environment.rb'
@@ -6,7 +6,7 @@
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
8
8
 
9
- $Id: erb.rb,v 1.3 2006/01/07 13:39:13 mutoh Exp $
9
+ $Id: erb.rb,v 1.4 2006/02/22 16:42:41 mutoh Exp $
10
10
  =end
11
11
 
12
12
  require 'erb'
@@ -19,18 +19,21 @@ module GetText
19
19
  }
20
20
 
21
21
  module_function
22
+ # Sets some preferences to parse ERB files.
23
+ # * config: a Hash of the config. It can takes some values below:
24
+ # * :extnames: An Array of target files extension. Default is [".rhtml"].
22
25
  def init(config)
23
26
  config.each{|k, v|
24
27
  @config[k] = v
25
28
  }
26
29
  end
27
30
 
28
- def parse(file, targets = [])
31
+ def parse(file, targets = []) # :nodoc:
29
32
  erb = ERB.new(IO.readlines(file).join).src.split(/$/)
30
33
  RubyParser.parse_lines(file, erb, targets)
31
34
  end
32
35
 
33
- def target?(file)
36
+ def target?(file) # :nodoc:
34
37
  @config[:extnames].each do |v|
35
38
  return true if File.extname(file) == v
36
39
  end
@@ -6,7 +6,7 @@
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
8
8
 
9
- $Id: glade.rb,v 1.3 2006/01/07 13:39:13 mutoh Exp $
9
+ $Id: glade.rb,v 1.4 2006/02/22 16:42:41 mutoh Exp $
10
10
  =end
11
11
 
12
12
  require 'cgi'
@@ -21,13 +21,13 @@ module GetText
21
21
  TARGET2 = /(.*)<\/property>/
22
22
 
23
23
  module_function
24
- def parse(file, targets = [])
24
+ def parse(file, targets = []) # :nodoc:
25
25
  lines = IO.readlines(file)
26
26
  parse_lines(file, lines, targets)
27
27
  end
28
28
 
29
29
  #from ary of lines.
30
- def parse_lines(file, lines, targets)
30
+ def parse_lines(file, lines, targets) # :nodoc:
31
31
  cnt = 0
32
32
  target = false
33
33
  line_no = 0
@@ -65,7 +65,7 @@ module GetText
65
65
  XML_RE = /<\?xml/
66
66
  GLADE_RE = /glade-2.0.dtd/
67
67
 
68
- def target?(file)
68
+ def target?(file) # :nodoc:
69
69
  data = IO.readlines(file)
70
70
  if XML_RE =~ data[0]
71
71
  if GLADE_RE =~ data[1]
@@ -81,7 +81,7 @@ module GetText
81
81
  end
82
82
  end
83
83
 
84
- def add_target(val, file, line_no, targets)
84
+ def add_target(val, file, line_no, targets) # :nodoc:
85
85
  return unless val.size > 0
86
86
  assoc_data = targets.assoc(val)
87
87
  val = CGI.unescapeHTML(val)
@@ -9,13 +9,13 @@
9
9
  You may redistribute it and/or modify it under the same
10
10
  license terms as Ruby.
11
11
 
12
- $Id: ruby.rb,v 1.4 2005/12/17 07:12:40 mutoh Exp $
12
+ $Id: ruby.rb,v 1.6 2006/02/22 16:42:41 mutoh Exp $
13
13
  =end
14
14
 
15
15
  require 'irb/ruby-lex.rb'
16
16
  require 'stringio'
17
17
 
18
- class RubyLexX < RubyLex
18
+ class RubyLexX < RubyLex # :nodoc: all
19
19
 
20
20
  def initialize
21
21
  super
@@ -124,15 +124,15 @@ end
124
124
  module GetText
125
125
  module RubyParser
126
126
  ID = ['gettext', '_', 'N_', 'sgettext', 's_']
127
- PLURAL_ID = ['ngettext', 'n_']
127
+ PLURAL_ID = ['ngettext', 'n_', 'Nn_']
128
128
 
129
129
  module_function
130
- def parse(file, targets = [])
130
+ def parse(file, targets = []) # :nodoc:
131
131
  lines = IO.readlines(file)
132
132
  parse_lines(file, lines, targets)
133
133
  end
134
134
 
135
- def parse_lines(file_name, lines, targets)
135
+ def parse_lines(file_name, lines, targets) # :nodoc:
136
136
  file = StringIO.new(lines.join + "\n")
137
137
  rl = RubyLexX.new
138
138
  rl.set_input(file)
@@ -188,7 +188,7 @@ module GetText
188
188
  targets
189
189
  end
190
190
 
191
- def target?(file)
191
+ def target?(file) # :nodoc:
192
192
  true # always true, as default parser.
193
193
  end
194
194
 
@@ -20,7 +20,7 @@ module GetText
20
20
 
21
21
  class PoParser < Racc::Parser
22
22
 
23
- module_eval <<'..end src/poparser.ry modeval..id827ea1cb71', 'src/poparser.ry', 90
23
+ module_eval <<'..end src/poparser.ry modeval..ida44e3c5e28', 'src/poparser.ry', 90
24
24
  include GetText
25
25
  GetText.bindtextdomain("rgettext")
26
26
 
@@ -32,10 +32,11 @@ module_eval <<'..end src/poparser.ry modeval..id827ea1cb71', 'src/poparser.ry',
32
32
  ret
33
33
  end
34
34
 
35
- def parse(str, data)
35
+ def parse(str, data, ignore_fuzzy = true)
36
36
  @comments = []
37
37
  @data = data
38
38
  @fuzzy = false
39
+ $ignore_fuzzy = ignore_fuzzy
39
40
  str.strip!
40
41
  @q = []
41
42
  until str.empty? do
@@ -92,7 +93,7 @@ module_eval <<'..end src/poparser.ry modeval..id827ea1cb71', 'src/poparser.ry',
92
93
 
93
94
  def on_message(msgid, msgstr)
94
95
  @data[msgid] = msgstr
95
-
96
+
96
97
  @data.set_comment(msgid, @comments.join("\n"))
97
98
  @comments.clear
98
99
  end
@@ -102,7 +103,7 @@ module_eval <<'..end src/poparser.ry modeval..id827ea1cb71', 'src/poparser.ry',
102
103
  @comments << comment
103
104
  end
104
105
 
105
- ..end src/poparser.ry modeval..id827ea1cb71
106
+ ..end src/poparser.ry modeval..ida44e3c5e28
106
107
 
107
108
  ##### racc 1.4.4 generates ###
108
109
 
@@ -217,7 +218,7 @@ Racc_debug_parser = true
217
218
 
218
219
  module_eval <<'.,.,', 'src/poparser.ry', 36
219
220
  def _reduce_4( val, _values, result )
220
- if @fuzzy
221
+ if @fuzzy and $ignore_fuzzy
221
222
  if val[1] != ""
222
223
  $stderr.print _("Warning: fuzzy message was ignored.\n")
223
224
  $stderr.print " msgid '#{val[1]}'\n"
@@ -235,7 +236,7 @@ module_eval <<'.,.,', 'src/poparser.ry', 36
235
236
 
236
237
  module_eval <<'.,.,', 'src/poparser.ry', 51
237
238
  def _reduce_5( val, _values, result )
238
- if @fuzzy
239
+ if @fuzzy and $ignore_fuzzy
239
240
  if val[1] != ""
240
241
  $stderr.print _("Warning: fuzzy message was ignored.\n")
241
242
  $stderr.print "msgid = '#{val[1]}\n"
data/lib/gettext/rails.rb CHANGED
@@ -1,28 +1,53 @@
1
1
  =begin
2
2
  gettext/rails.rb - GetText for "Ruby on Rails"
3
3
 
4
- Copyright (C) 2005 Masao Mutoh
4
+ Copyright (C) 2005,2006 Masao Mutoh
5
5
 
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
8
8
 
9
- $Id: rails.rb,v 1.12 2006/01/14 13:48:22 mutoh Exp $
9
+ $Id: rails.rb,v 1.17 2006/02/23 14:53:59 mutoh Exp $
10
10
  =end
11
11
 
12
- require 'gettext'
13
12
  require 'gettext/cgi'
14
13
  require 'active_record'
15
14
  require 'active_support'
16
15
 
17
- module GetText
16
+ module GetText #:nodoc:
17
+ # = GetText for Ruby on Rails
18
+ # GetText::Rails supports Ruby on Rails.
19
+ # You add only 2 lines in your controller, all of the controller/view/models are
20
+ # targeted the textdomain.
21
+ #
22
+ # See <Ruby-GetText-Package HOWTO for Ruby on Rails (http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html>.
18
23
  module Rails
19
24
  include GetText
25
+ extend GetText
20
26
 
21
- Rails = ::Rails
27
+ Rails = ::Rails #:nodoc:
22
28
 
23
- alias :_bindtextdomain :bindtextdomain
29
+ alias :_bindtextdomain :bindtextdomain #:nodoc:
24
30
 
31
+ # Bind a textdomain(#{path}/#{locale}/LC_MESSAGES/#{domainname}.mo) to your program.
32
+ # Notes the textdomain scope becomes all of the controllers/views/models in your app.
33
+ # This is different from normal GetText.bindtextomain.
34
+ #
35
+ # Usually, you don't call this directly in your rails application.
36
+ # Call init_gettext in ActionController::Base instead.
37
+ #
38
+ # On the other hand, you need to call this in helpers/plugins.
39
+ #
40
+ # * domainname: the textdomain name.
41
+ # * locale: the locale value such as "ja-JP". When the value is nil,
42
+ # * charset: the charset. Generally UTF-8 is recommanded.
43
+ # * with_model: false if you want to ignore model support.
44
+ #
45
+ # locale is searched the order by this value > "lang" value of QUERY_STRING >
46
+ # "lang" value of Cookie > HTTP_ACCEPT_LANGUAGE value > Default locale(en).
47
+ # And the charset is set order by "the argument of bindtextdomain" > HTTP_ACCEPT_CHARSET > Default charset(UTF-8).
48
+ #
25
49
  def bindtextdomain(domainname, _cgi = nil, locale = nil, charset = nil, with_model = true)
50
+ Locale.clear # IMPORTANT! current locale should be nil once(default is used).
26
51
  set_cgi(_cgi) if _cgi
27
52
  @gettext_container_domainname = domainname
28
53
  path = File.join(RAILS_ROOT, "locale")
@@ -31,20 +56,20 @@ module GetText
31
56
  bindtextdomain_to(ActiveRecord::Base, domainname) if with_model
32
57
  end
33
58
 
34
- def bindtextdomain_to(klass, domainname)
59
+ def bindtextdomain_to(klass, domainname) #:nodoc:
35
60
  klass.class_eval {
36
61
  textdomain(domainname)
37
62
 
38
- def self.human_attribute_name(attribute_key_name)
63
+ def self.human_attribute_name(attribute_key_name) #:nodoc:
39
64
  s_("#{self}|#{attribute_key_name.humanize}")
40
65
  end
41
- def self.human_attribute_table_name_for_error(table_name)
66
+ def self.human_attribute_table_name_for_error(table_name) #:nodoc:
42
67
  _(table_name.gsub(/_/, " "))
43
68
  end
44
69
  }
45
70
  end
46
71
 
47
- def callersrc
72
+ def callersrc #:nodoc:
48
73
  @gettext_container_domainname = nil unless defined? @gettext_container_domainname
49
74
  @gettext_container_domainname
50
75
  end
@@ -52,7 +77,7 @@ module GetText
52
77
  end
53
78
 
54
79
 
55
- module ActionController
80
+ module ActionController #:nodoc:
56
81
  class Base
57
82
  helper GetText::Rails
58
83
  include GetText::Rails
@@ -62,13 +87,22 @@ module ActionController
62
87
  @@gettext_content_type = nil
63
88
 
64
89
  prepend_before_filter :init_gettext
65
-
66
- def init_gettext_main
90
+ after_filter :init_content_type
91
+
92
+ def init_gettext_main #:nodoc:
67
93
  bindtextdomain(@@gettext_domainname, request.cgi) #You need to pass CGI object first.
68
- @headers["Content-Type"] = "#{@@gettext_content_type}; charset=#{GetText.output_charset}"
69
94
  end
70
95
 
71
- def init_gettext
96
+ def init_content_type #:nodoc:
97
+ if @headers["Content-Type"] and /javascript/ =~ @headers["Content-Type"]
98
+ @headers["Content-Type"] = "text/javascript; charset=#{GetText.output_charset}"
99
+ elsif ! @headers["Content-Type"]
100
+ @headers["Content-Type"] = "#{@@gettext_content_type}; charset=#{GetText.output_charset}"
101
+ end
102
+ end
103
+
104
+
105
+ def init_gettext # :nodoc:
72
106
  init_gettext_main if @@gettext_domainname
73
107
  ActiveRecord::Errors.class_eval{
74
108
  # You need to call bindtextdomain here because ActiveRecord::Errors doesn't know
@@ -77,6 +111,21 @@ module ActionController
77
111
  }
78
112
  end
79
113
 
114
+ # Bind a 'textdomain' to all of the controllers/views/models. Call this instead of GetText.bindtextdomain.
115
+ # * textdomain: the textdomain
116
+ # * charset: the output charset. Default is "UTF-8"
117
+ # * content_type: the content type. Default is "text/html"
118
+ #
119
+ # If you want to separate the textdomain each controllers, you need to call this function in the each controllers.
120
+ #
121
+ # app/controller/blog_controller.rb:
122
+ # require 'gettext/rails'
123
+ #
124
+ # class BlogController < ApplicationController
125
+ # init_gettext "blog"
126
+ # :
127
+ # :
128
+ # end
80
129
  def self.init_gettext(domainname, charset = "UTF-8", content_type = "text/html")
81
130
  GetText.output_charset = charset
82
131
  @@gettext_domainname = domainname
@@ -84,7 +133,7 @@ module ActionController
84
133
  end
85
134
  end
86
135
 
87
- class TestRequest < AbstractRequest
136
+ class TestRequest < AbstractRequest #:nodoc:
88
137
  @cgi = nil
89
138
  def cgi
90
139
  @cgi = CGI.new unless @cgi
@@ -93,18 +142,38 @@ module ActionController
93
142
  end
94
143
  end
95
144
 
96
- module ActiveRecord
145
+ module ActiveRecord #:nodoc:
97
146
  class Base
98
- include GetText::Rails
99
- extend GetText::Rails
100
- end
147
+ include GetText::Rails # Important
148
+ extend GetText::Rails # Important
149
+
150
+ @@error_message_title = nil
151
+ @@error_message_explanation = nil
152
+
153
+ # Sets a your own title of error message dialog.
154
+ def self.set_error_message_title(single_msg, plural_msg)
155
+ @@error_message_title = [single_msg, plural_msg]
156
+ end
157
+
158
+ # Sets a your own explanation of the error message dialog.
159
+ def self.set_error_message_explanation(single_msg, plural_msg)
160
+ @@error_message_explanation = [single_msg, plural_msg]
161
+ end
101
162
 
102
- class Errors
163
+ def error_message_title #:nodoc:
164
+ @@error_message_title
165
+ end
166
+ def error_message_explanation #:nodoc:
167
+ @@error_message_explanation
168
+ end
169
+ end
170
+
171
+ class Errors #:nodoc:
103
172
  include GetText
104
173
  extend GetText
105
174
 
106
- RE_FIELD_NAME = /%\{fn\}/
107
-
175
+ RE_FIELD_NAME = /%\{fn\}/ #:nodoc:
176
+
108
177
  # You need to define this here, because this values will be updated by application.
109
178
  default_error_messages.update(
110
179
  :inclusion => N_("%{fn} is not included in the list"),
@@ -120,22 +189,20 @@ module ActiveRecord
120
189
  :taken => N_("%{fn} has already been taken"),
121
190
  :not_a_number => N_("%{fn} is not a number")
122
191
  )
123
-
124
192
  @@default_error_messages_d = {
125
193
  :too_long => default_error_messages[:too_long],
126
194
  :too_short => default_error_messages[:too_short],
127
195
  :wrong_length => default_error_messages[:wrong_length]
128
196
  }
129
197
 
130
- def self.convert_validates_messages_of_validates_length_of
198
+ def self.convert_validates_messages_of_validates_length_of #:nodoc:
131
199
  default_error_messages[:too_long] = _(@@default_error_messages_d[:too_long])
132
200
  default_error_messages[:too_short] = _(@@default_error_messages_d[:too_short])
133
201
  default_error_messages[:wrong_length] = _(@@default_error_messages_d[:wrong_length])
134
202
  end
135
203
 
136
- def full_messages
204
+ def full_messages #:nodoc:
137
205
  full_messages = []
138
-
139
206
  @errors.each_key do |attr|
140
207
  @errors[attr].each do |msg|
141
208
  next if msg.nil?
@@ -152,8 +219,8 @@ module ActiveRecord
152
219
  end
153
220
  end
154
221
 
155
- module Validations
156
- module ClassMethods
222
+ module Validations #:nodoc:
223
+ module ClassMethods #:nodoc:
157
224
  alias :_validates_length_of :validates_length_of
158
225
  def validates_length_of(*attrs)
159
226
  ActiveRecord::Errors.convert_validates_messages_of_validates_length_of
@@ -164,33 +231,44 @@ module ActiveRecord
164
231
  end
165
232
 
166
233
 
167
- module ActionView
168
- module Helpers
169
- module ActiveRecordHelper
234
+ module ActionView #:nodoc:
235
+ module Helpers #:nodoc:
236
+ module ActiveRecordHelper #:nodoc: all
170
237
  module L10n
171
238
  # Separate namespace for textdomain
172
239
  include GetText
173
240
  extend GetText
241
+
242
+ @error_message_title = Nn_("%{num} error prohibited this %{record} from being saved",
243
+ "%{num} errors prohibited this %{record} from being saved")
244
+ @error_message_explanation = Nn_("There was a problem with the following field:",
245
+ "There were problems with the following fields:")
246
+
174
247
  module_function
248
+ def render_error_message(object, klass, record, count, options)
249
+ message_title = object.error_message_title || @error_message_title
250
+ message_explanation = object.error_message_explanation || @error_message_explanation
251
+
252
+ klass.content_tag("div",
253
+ klass.content_tag(
254
+ options[:header_tag] || "h2",
255
+ n_(message_title, count) % {:num => count, :record => record}
256
+ ) +
257
+ klass.content_tag("p", n_(message_explanation, count) % {:num => count}) +
258
+ klass.content_tag("ul", object.errors.full_messages.collect { |msg| klass.content_tag("li", msg) }),
259
+ "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
260
+ )
261
+ end
262
+
175
263
  def error_messages_for(object, object_name, klass, options = {})
176
264
  textdomain("rails")
177
265
 
178
266
  options = options.symbolize_keys
179
-
267
+
180
268
  unless object.errors.empty?
181
269
  count = object.errors.count
182
270
  record = ActiveRecord::Base.human_attribute_table_name_for_error(object_name)
183
- klass.content_tag("div",
184
- klass.content_tag(
185
- options[:header_tag] || "h2",
186
- n_("%{num} error prohibited this %{record} from being saved",
187
- "%{num} errors prohibited this %{record} from being saved", count) %
188
- {:num => count, :record => record}) +
189
- klass.content_tag("p", n_("There were problems with the following field:",
190
- "There were problems with the following fields:", count)) +
191
- klass.content_tag("ul", object.errors.full_messages.collect { |msg| klass.content_tag("li", msg) }),
192
- "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
193
- )
271
+ render_error_message(object, klass, record, count, options)
194
272
  end
195
273
  end
196
274
  end