Class: GetText::LocalePath

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/locale_path.rb

Overview

Treats locale-path for mo-files.

Constant Summary collapse

CONFIG_PREFIX =

The default locale paths.

RbConfig::CONFIG['prefix'].gsub(/\/local/, "")
DEFAULT_RULES =
default_rules_candidates.uniq

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, topdir = nil) ⇒ LocalePath

Creates a new GetText::TextDomain.

  • name: the textdomain name.
  • topdir: the locale path ("%topdir/%lang/LC_MESSAGES/%name.mo") or nil.


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gettext/locale_path.rb', line 78

def initialize(name, topdir = nil)
  @name = name

  if topdir
    path_rules = ["#{topdir}/%{lang}/LC_MESSAGES/%{name}.mo", "#{topdir}/%{lang}/%{name}.mo"]
  else
    path_rules = self.class.default_path_rules
  end

  @locale_paths = {}
  path_rules.each do |rule|
    this_path_rules = rule % {:lang => "([^\/]+)", :name => name}
    Dir.glob(rule % {:lang => "*", :name => name}).each do |path|
      if /#{this_path_rules}/ =~ path
        locale_path = $1
        unless @locale_paths[locale_path]
          path.untaint if RUBY_VERSION < "2.7"
          @locale_paths[locale_path] = path
        end
      end
    end
  end
  @supported_locales = @locale_paths.keys.sort
end

Instance Attribute Details

#locale_pathsObject (readonly)

Returns the value of attribute locale_paths.



73
74
75
# File 'lib/gettext/locale_path.rb', line 73

def locale_paths
  @locale_paths
end

#supported_localesObject (readonly)

Returns the value of attribute supported_locales.



73
74
75
# File 'lib/gettext/locale_path.rb', line 73

def supported_locales
  @supported_locales
end

Class Method Details

.add_default_rule(path) ⇒ Object

Add default locale path. Usually you should use GetText.add_default_locale_path instead.

  • path: a new locale path. (e.g.) "/usr/share/locale/%lang/LC_MESSAGES/%name.mo" ('locale' => "ja_JP", 'name' => "textdomain")
  • Returns: the new DEFAULT_LOCALE_PATHS


38
39
40
# File 'lib/gettext/locale_path.rb', line 38

def add_default_rule(path)
  DEFAULT_RULES.unshift(path)
end

.default_path_rulesObject

Returns path rules as an Array. (e.g.) ["/usr/share/locale/%lang/LC_MESSAGES/%name.mo", ...]



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gettext/locale_path.rb', line 44

def default_path_rules
  default_path_rules = []

  if ENV["GETTEXT_PATH"]
    ENV["GETTEXT_PATH"].split(/,/).each {|i|
      default_path_rules += ["#{i}/%{lang}/LC_MESSAGES/%{name}.mo", "#{i}/%{lang}/%{name}.mo"]
    }
  end
  default_path_rules += DEFAULT_RULES

  load_path = $LOAD_PATH.map {|path|
    path = path.to_path if path.respond_to?(:to_path)
    path.gsub(/\/lib\z/, "")
  }
  load_path.each {|path|
    default_path_rules += [
      "#{path}/data/locale/%{lang}/LC_MESSAGES/%{name}.mo",
      "#{path}/data/locale/%{lang}/%{name}.mo",
      "#{path}/locale/%{lang}/LC_MESSAGES/%{name}.mo",
      "#{path}/locale/%{lang}/%{name}.mo",
    ]
  }
  # paths existed only.
  default_path_rules = default_path_rules.select{|path|
    Dir.glob(path % {:lang => "*", :name => "*"}).size > 0}.uniq
  default_path_rules
end

Instance Method Details

#current_path(lang) ⇒ Object

Gets the current path.

  • lang: a Locale::Tag.


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gettext/locale_path.rb', line 105

def current_path(lang)
  lang_candidates = lang.candidates

  lang_candidates.each do |tag|
    path = @locale_paths[tag.to_s]
    warn "GetText::TextDomain#load_mo: mo-file is #{path}" if $DEBUG
    return path if path
  end

  if $DEBUG
    warn "MO file is not found in"
    @locale_paths.each do |path|
      warn "  #{path[1]}"
    end
  end
  nil
end