Class: Msg

Inherits:
String show all
Defined in:
lib/ri18n/msg.rb

Constant Summary collapse

ENTRY_REGEXP =
/(#[.,:]?\s*.*?\n)?msgid\s+(.+?)msg(id_plural|str)\s+(.*)/m
MSGSTR_PLURAL_RE =
/(.+?)(msgstr\[\d+\]\s+.+?)+/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#interpolate, #strip_q, #strip_q!, #unescape_quote

Constructor Details

#initialize(msg, comments, idp = nil, pl = nil) ⇒ Msg

Returns a new instance of Msg.



31
32
33
34
35
36
37
# File 'lib/ri18n/msg.rb', line 31

def initialize(msg, comments, idp=nil, pl=nil)
  super(msg)
  @comments = comments
  parse_comments if @comments
  @id_plural = idp
  @plurals = pl
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



3
4
5
# File 'lib/ri18n/msg.rb', line 3

def comments
  @comments
end

#flagObject (readonly)

Returns the value of attribute flag.



3
4
5
# File 'lib/ri18n/msg.rb', line 3

def flag
  @flag
end

#id_pluralObject

Returns the value of attribute id_plural.



4
5
6
# File 'lib/ri18n/msg.rb', line 4

def id_plural
  @id_plural
end

#pluralsObject

Returns the value of attribute plurals.



4
5
6
# File 'lib/ri18n/msg.rb', line 4

def plurals
  @plurals
end

#referenceObject (readonly)

Returns the value of attribute reference.



3
4
5
# File 'lib/ri18n/msg.rb', line 3

def reference
  @reference
end

#strObject

Returns the value of attribute str.



4
5
6
# File 'lib/ri18n/msg.rb', line 4

def str
  @str
end

Class Method Details

.new_untranslated(comments, id_plural, nplural) ⇒ Object

build a new untranslated msgstr



27
28
29
# File 'lib/ri18n/msg.rb', line 27

def Msg::new_untranslated(comments, id_plural, nplural)
  Msg.new("", comments, id_plural, Array.new(nplural, "") )
end

.Parse(entry) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ri18n/msg.rb', line 6

def Msg::Parse(entry)
    all, com, id, sel, str = *(ENTRY_REGEXP.match(entry))
    com = com ? com.split("\n") : nil
    id.strip_q!
    if sel == 'str'
      str.strip_q!
      msg = Msg.new(str, com)
      [id, msg]
    else 
      [id, Msg::ParsePlurals(str, com)]
    end
end

.ParsePlurals(entry, com) ⇒ Object



20
21
22
23
24
# File 'lib/ri18n/msg.rb', line 20

def Msg::ParsePlurals(entry, com)
  all, idp, pl = *(MSGSTR_PLURAL_RE.match(entry))
  plurals = pl.strip.split(/msgstr\[\d+\]/).collect!{|mp| mp.strip_q}.compact
  Msg.new(plurals[0], com, idp.strip_q, plurals)
end

Instance Method Details

#parse_commentsObject



39
40
41
42
43
44
45
46
# File 'lib/ri18n/msg.rb', line 39

def parse_comments
  @reference = if r = @comments.find{|c| c =~ /\A#:/}
                  r[2..-1].strip
               end 
  @flag = if r = @comments.find{|c| c =~ /\A#,/}
             r[2..-1].strip
          end
end

#po_format(id, nplurals = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ri18n/msg.rb', line 48

def po_format(id, nplurals=nil)
  comments = @comments ? @comments.join("\n") << "\n" : "" 
  comments << if @id_plural
    memo = ''
    if @plurals
      pl = @plurals
    else
      pl = Array.new(nplurals, "")
    end
    pl.each_with_index{|pl, i| memo << %Q'msgstr[#{i}] "#{pl}"\n' }
    %Q(msgid "#{id}"\nmsgid_plural "#{@id_plural}"\n#{memo}\n)
  else
    %Q(msgid "#{id}"\nmsgstr "#{self}"\n\n)
  end
end

#pot_format(nplurals = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ri18n/msg.rb', line 64

def pot_format(nplurals=nil)
  if @id_plural
    memo = ''
    (0...nplurals).each{|i| memo << %Q'msgstr[#{i}] ""\n' }
    %Q(msgid "#{self}"\nmsgid_plural "#{@id_plural}"\n#{memo}\n)
  else
    %Q(msgid "#{self}"\nmsgstr ""\n\n)
  end
end