1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# -*- coding: utf-8 -*-
=begin
locale_path.rb - GetText::LocalePath
Copyright (C) 2001-2010 Masao Mutoh
You may redistribute it and/or modify it under the same
license terms as Ruby or LGPL.
=end
require 'rbconfig'
require 'gettext/core_ext/string'
module GetText
# Treats locale-path for mo-files.
class LocalePath
include Locale::Util::Memoizable
# The default locale paths.
CONFIG_PREFIX = RbConfig::CONFIG['prefix'].gsub(/\/local/, "")
DEFAULT_RULES = [
"./locale/%{lang}/LC_MESSAGES/%{name}.mo",
"./locale/%{lang}/%{name}.mo",
"#{RbConfig::CONFIG['datadir']}/locale/%{lang}/LC_MESSAGES/%{name}.mo",
"#{RbConfig::CONFIG['datadir'].gsub(/\/local/, "")}/locale/%{lang}/LC_MESSAGES/%{name}.mo",
"#{CONFIG_PREFIX}/share/locale/%{lang}/LC_MESSAGES/%{name}.mo",
"#{CONFIG_PREFIX}/local/share/locale/%{lang}/LC_MESSAGES/%{name}.mo"
].uniq
class << self
include Locale::Util::Memoizable
# 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
def add_default_rule(path)
DEFAULT_RULES.unshift(path)
end
# Returns path rules as an Array.
# (e.g.) ["/usr/share/locale/%{lang}/LC_MESSAGES/%{name}.mo", ...]
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.dup
load_path.map!{|v| v.match(/(.*?)(\/lib)*?$/); $1}
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
memoize_dup :default_path_rules
end
attr_reader :locale_paths, :supported_locales
# Creates a new GetText::TextDomain.
# * name: the textdomain name.
# * topdir: the locale path ("%{topdir}/%{lang}/LC_MESSAGES/%{name}.mo") or nil.
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_paths[$1] = path.untaint unless @locale_paths[$1]
end
end
end
@supported_locales = @locale_paths.keys.sort
end
# Gets the current path.
# * lang: a Locale::Tag.
def current_path(lang)
lang_candidates = lang.to_posix.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
memoize :current_path
end
end
|