Class: Catalog

Inherits:
Hash show all
Includes:
LangTools, PoHelper
Defined in:
lib/ri18n/catalog.rb

Constant Summary

Constants included from LangTools

LangTools::NPLURAL, LangTools::PLURAL_FAMILIES

Instance Attribute Summary

Attributes included from LangTools

#lang

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LangTools

#country_code, #find_family, #lang_encoding, #language_code, #language_country, #nplural, #plural_family

Methods included from PoHelper

#encoding, #parse_content_type

Constructor Details

#initialize(lang) ⇒ Catalog

Returns a new instance of Catalog.



9
10
11
12
# File 'lib/ri18n/catalog.rb', line 9

def initialize(lang)
  super()
  @lang=lang
end

Class Method Details

.new_from_msgidlist(l, new_msg) ⇒ Object

creates a catalog with untranslated msgstr lang is l, needed for plural forms (nplural depends on lang)



16
17
18
19
20
# File 'lib/ri18n/catalog.rb', line 16

def Catalog.new_from_msgidlist(l, new_msg)
  c = Catalog.new(l)
  new_msg.each{|m| c[m] = Msg.new_untranslated(nil, m.id_plural, c.nplural)}
  c
end

Instance Method Details

#headerObject

in PO, header is defined as empty msgid



23
24
25
# File 'lib/ri18n/catalog.rb', line 23

def header
  self[""]
end

#header_commentsObject



32
33
34
# File 'lib/ri18n/catalog.rb', line 32

def header_comments
  header[:comments]
end

#header_entriesObject

ordered header entries (keys)



28
29
30
# File 'lib/ri18n/catalog.rb', line 28

def header_entries
  header[:ordered_entries]
end

#po_format(app_enc = 'utf-8') ⇒ Object

TODO: test ordering of entries (also with plurals))



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ri18n/catalog.rb', line 70

def po_format(app_enc='utf-8')
  ret = po_header(app_enc)
  sort.each{|id, str| 
    next if id == ""
    if str.respond_to? :po_format
      ret << str.po_format(id, nplural)
    else
      ret << Msg.new(str.to_s, nil).po_format(id, nplural)
    end
    }
  if encoding == app_enc.downcase
    ret
  else
    Iconv.new(encoding, app_enc).iconv(ret)
  end
end

#po_header(app_enc) ⇒ Object

returns the formated PO header



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ri18n/catalog.rb', line 37

def po_header(app_enc)
  if header
    ret = header_comments ? header_comments.dup.strip + "\n" : ''
    unless  header_entries.empty?
      ret << PoSource::HEADER_SPLIT
      header_entries.each{|key| ret << "\"#{key}: #{header[key]}\\n\"\n"}
      ret << "\n"
    end
  else
    "# SOME DESCRIPTIVE TITLE.\n# Copyright (C) YEAR Free Software Foundation, Inc.\n# This file is distributed under the same license as the PACKAGE package.\n# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n#\n#, fuzzy\nmsgid \"\"\nmsgstr \"\"\n\"Project-Id-Version: PACKAGE VERSION\\\\n\"\n\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\\\n\"\n\"POT-Creation-Date: 2002-12-10 22:11+0100\\\\n\"\n\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\\\n\"\n\"Language-Team: LANGUAGE <[email protected]>\\\\n\"\n\"MIME-Version: 1.0\\\\n\"\n\"Content-Type: text/plain; charset=\#{app_enc}\\\\n\"\n\"Content-Transfer-Encoding: 8bit\\\\n\"\n\"Plural-Forms: nplurals=\#{nplural}; plural=n == 0;\\\\n\"\n\n    EOS\n  end\nend\n"

#update(new_msg) ⇒ Object

update the catalog with the new_msg messages



88
89
90
91
# File 'lib/ri18n/catalog.rb', line 88

def update(new_msg)
  new_ids = Catalog.new_from_msgidlist(@lang, new_msg)
  new_msg.each{|m| self[m] = new_ids[m] unless self.has_key?(m)}
end