struct Gettext::Catalogue

Overview

Gettext message catalogue. Contains methods for handling translations

You should not be manually creating an instance of this class! Instead let the Gettext backends do it for you! See Gettext::MOBackend and Gettext::POBackend

Defined in:

backend/gettext/gettext.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(contents : Hash(String, Hash(Int8, String))) #

Creates a message catalogue from parsed Gettext data


[View source]

Instance Method Detail

def contents : Hash(String, Hash(Int8, String)) #

Returns all messages within the catalogue

You should never have to deal with this method under normal circumstances. Please use the #gettext family of methods to translate your application instead.

catalogue = Gettext::MOBackend.new("examples").create["en-US"]
catalogue.contents # => {...}

[View source]
def gettext(id : String) #

Fetches the translated message for the specific ID. If none can be found the given ID is returned.

catalogue = Gettext::MOBackend.new("examples").create["en_US"]
catalogue.gettext("A message")     # => "Translated message"
catalogue.gettext("I don't exist") # => "I don't exist"

[View source]
def headers : Hash(String, String) #

Returns a hash of the headers

catalogue = Gettext::MOBackend.new("examples").create["en_US"]
catalogue.headers["Plural-Forms"] # => "nplurals=2; plural=(n != 1);"

[View source]
def ngettext(id : String, plural_id, n) #

Fetches the translated message for the specific ID with the correct plural form. Returns either the singular or plural id if none can be found.

catalogue = Gettext::MOBackend.new("examples").create["en_US"]
catalogue.ngettext("I have %d apple", "I have %d apples", 0) # => "Translated message with plural-form 1"
catalogue.ngettext("I have %d apple", "I have %d apples", 1) # => "Translated message with plural-form 0"

# Not found:
catalogue.ngettext("I have %d pear", "I have %d pears", 0) # => "I have %d pears"
catalogue.ngettext("I have %d pear", "I have %d pears", 1) # => "I have %d pear"

[View source]
def npgettext(context, id, plural_id, n) #

Fetches the translated message for the specific ID that is bound by context with the correct plural form.

catalogue = Gettext::MOBackend.new("examples").create["en_US"]
catalogue.npgettext("CopyPasteMenu", "Export %d file", "Export %d files", 0) # => "Translated message with plural-form 1"
catalogue.npgettext("CopyPasteMenu", "Export %d file", "Export %d files", 1) # => "Translated message with plural-form 0"

# Not found:
catalogue.npgettext("CopyPasteMenu", "None", "NonePlural", 0) # => "NonePlural"
catalogue.npgettext("CopyPasteMenu", "None", "NonePlural", 1) # => "None"

[View source]
def pgettext(context, id) #

Fetches the translated message for the specific ID that is bound by context. If none can be found the given ID is returned.

catalogue = Gettext::MOBackend.new("examples").create["en_US"]

catalogue.pgettext("CopyPasteMenu", "copy")          # => "Translated copy"
catalogue.pgettext("CopyPasteMenu", "I don't exist") # => "I don't exist"

[View source]