Class: MOFile
Defined Under Namespace
Classes: Header, InvalidFormat
Constant Summary collapse
- MAGIC_BIG_ENDIAN =
"\x95\x04\x12\xde"
- MAGIC_LITTLE_ENDIAN =
"\xde\x12\x04\x95"
Instance Attribute Summary collapse
-
#charset ⇒ Object
readonly
Returns the value of attribute charset.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#last_modified ⇒ Object
Returns the value of attribute last_modified.
-
#little_endian ⇒ Object
Returns the value of attribute little_endian.
-
#nplurals ⇒ Object
readonly
Returns the value of attribute nplurals.
-
#path ⇒ Object
Returns the value of attribute path.
-
#plural ⇒ Object
readonly
Returns the value of attribute plural.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(output_charset = nil) ⇒ MOFile
constructor
A new instance of MOFile.
- #load(arg) ⇒ Object
- #load_from_file(filename) ⇒ Object
- #load_from_stream(io) ⇒ Object
- #set_comment(msgid_or_sym, comment) ⇒ Object
- #update! ⇒ Object
Constructor Details
#initialize(output_charset = nil) ⇒ MOFile
Returns a new instance of MOFile.
41 42 43 44 45 46 47 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 41 def initialize(output_charset = nil) @filename = nil @last_modified = nil @little_endian = true @output_charset = output_charset super end |
Instance Attribute Details
#charset ⇒ Object (readonly)
Returns the value of attribute charset.
154 155 156 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 154 def charset @charset end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
23 24 25 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 23 def filename @filename end |
#last_modified ⇒ Object
Returns the value of attribute last_modified.
153 154 155 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 153 def last_modified @last_modified end |
#little_endian ⇒ Object
Returns the value of attribute little_endian.
153 154 155 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 153 def little_endian @little_endian end |
#nplurals ⇒ Object (readonly)
Returns the value of attribute nplurals.
154 155 156 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 154 def nplurals @nplurals end |
#path ⇒ Object
Returns the value of attribute path.
153 154 155 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 153 def path @path end |
#plural ⇒ Object (readonly)
Returns the value of attribute plural.
154 155 156 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 154 def plural @plural end |
Class Method Details
.open(arg = nil, output_charset = nil) ⇒ Object
36 37 38 39 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 36 def self.open(arg = nil, output_charset = nil) result = self.new(output_charset) result.load(arg) end |
Instance Method Details
#load(arg) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 60 def load(arg) case arg when String begin st = File.stat(arg) @last_modified = [st.ctime, st.mtime] rescue Exception end load_from_file(arg) when IO load_from_stream(arg) end @filename = arg self end |
#load_from_file(filename) ⇒ Object
143 144 145 146 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 143 def load_from_file(filename) @filename = filename File.open(filename, 'rb'){|f| load_from_stream(f)} end |
#load_from_stream(io) ⇒ Object
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 76 def load_from_stream(io) magic = io.read(4) case magic when MAGIC_BIG_ENDIAN @little_endian = false when MAGIC_LITTLE_ENDIAN @little_endian = true else raise InvalidFormat.new("Unknown signature %s" % magic.dump) end header = Header.new(magic, *(io.read(4 * 6).unpack(@little_endian ? 'V6' : 'N6'))) raise InvalidFormat.new(sprintf("file format revision %d isn't supported", header.revision)) if header.revision > 0 io.pos = header.orig_table_offset orig_table_data = io.read((4 * 2) * header.nstrings).unpack(@little_endian ? 'V*' : 'N*') io.pos = header.translated_table_offset trans_table_data = io.read((4 * 2) * header.nstrings).unpack(@little_endian ? 'V*' : 'N*') original_strings = Array.new(header.nstrings) for i in 0...header.nstrings io.pos = orig_table_data[i * 2 + 1] original_strings[i] = io.read(orig_table_data[i * 2 + 0]) end clear for i in 0...header.nstrings io.pos = trans_table_data[i * 2 + 1] str = io.read(trans_table_data[i * 2 + 0]) if original_strings[i] == "" if str @charset = nil @nplurals = nil @plural = nil str.each_line{|line| if /^Content-Type:/i =~ line and /charset=((?:\w|-)+)/i =~ line @charset = $1 elsif /^Plural-Forms:\s*nplurals\s*\=\s*(\d*);\s*plural\s*\=\s*([^;]*)\n?/ =~ line @nplurals = $1 @plural = $2 end break if @charset and @nplurals } @nplurals = "1" unless @nplurals @plural = "0" unless @plural end else if @output_charset begin str = Iconv.iconv(@output_charset, @charset, str).join if @charset rescue Iconv::Failure if $DEBUG $stderr.print "@charset = ", @charset, "\n" $stderr.print "@output_charset = ", @output_charset, "\n" $stderr.print "msgid = ", original_strings[i], "\n" $stderr.print "msgstr = ", str, "\n" end end end end self[original_strings[i]] = str end self end |
#set_comment(msgid_or_sym, comment) ⇒ Object
148 149 150 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 148 def set_comment(msgid_or_sym, comment) #Do nothing end |
#update! ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ramaze/contrib/gettext/mo.rb', line 49 def update! if FileTest.exist?(@filename) st = File.stat(@filename) load(@filename) unless (@last_modified == [st.ctime, st.mtime]) else puts "#{@filename} was lost." if $DEBUG clear end self end |