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:

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Locale) initialize(name)

Creates a locale for name locale.

Parameters:

  • name (String)

    the locale name.

Since:

  • 0.8.2



33
34
35
36
# File 'lib/yard/i18n/locale.rb', line 33

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

Class Attribute Details

+ (String?) default

Returns the default locale name.

Returns:

  • (String, nil)

    the default locale name.

Since:

  • 0.8.4



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

def default
  @default
end

Instance Attribute Details

- (String) name (readonly)

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

Returns:

See Also:

Since:

  • 0.8.2



28
29
30
# File 'lib/yard/i18n/locale.rb', line 28

def name
  @name
end

Instance Method Details

- (Boolean) load(locale_directory)

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yard/i18n/locale.rb', line 43

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

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

  require "yard/i18n/po_parser"
  return false unless POParser.available?

  po_parser = POParser.new
  @messages.merge!(po_parser.parse(po_file))

  true
end

- (String) translate(message)

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



61
62
63
# File 'lib/yard/i18n/locale.rb', line 61

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