Class: YARD::I18n::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/i18n/locale.rb

Overview

Locale is a unit of translation. It has #name and a set of messages.

Since:

  • 0.8.2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Locale

Creates a locale for name locale.

Parameters:

  • name (String)

    the locale name.

Since:

  • 0.8.2



17
18
19
20
# File 'lib/yard/i18n/locale.rb', line 17

def initialize(name)
  @name = name
  @messages = {}
end

Instance Attribute Details

#nameString (readonly)

Returns the name of the locale. It used IETF language tag format [language[.codeset]].

Returns:

See Also:

Since:

  • 0.8.2



12
13
14
# File 'lib/yard/i18n/locale.rb', line 12

def name
  @name
end

Instance Method Details

#load(locale_directory) ⇒ Boolean

Loads translation messages from locale_directory/#name.po.

Parameters:

  • locale_directory (String)

    the directory path that has #name.po.

Returns:

  • (Boolean)

    true if PO file exists, false otherwise.

Since:

  • 0.8.2



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yard/i18n/locale.rb', line 27

def load(locale_directory)
  return false if @name.nil?

  po_file = File.join(locale_directory, "#{@name}.po")
  return false unless File.exist?(po_file)

  begin
    require "gettext/tools/poparser"
    require "gettext/runtime/mofile"
  rescue LoadError
    log.warn "Need gettext gem for i18n feature:"
    log.warn "  gem install gettext"
    return false
  end

  parser = GetText::PoParser.new
  parser.report_warning = false
  data = GetText::MoFile.new
  parser.parse_file(po_file, data)
  @messages.merge!(data)
  true
end

#translate(message) ⇒ String

Returns translated message. If tarnslation isn’t registered, the message is returned.

Parameters:

  • message (String)

    the translation target message.

Returns:

  • (String)

    translated message. If tarnslation isn’t registered, the message is returned.

Since:

  • 0.8.2



53
54
55
# File 'lib/yard/i18n/locale.rb', line 53

def translate(message)
  @messages[message] || message
end