Module: When::Parts::Locale

Included in:
BasicTypes::M17n
Defined in:
lib/when_exe/parts/locale.rb

Overview

Multilingualization(M17n) 対応モジュール

When::BasicTypes::M17n の実装のうち When::BasicTypes 内部で
定義すべきでない部分を切り出してモジュールとしている

Constant Summary collapse

DefaultAlias =

Locale 読み替えの初期設定

{'alias'=>'ja', '日本語'=>'ja', '英語'=>'en'}
DefaultUnification =

漢字の包摂

{
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '寿',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
  '' => '',
}
Escape =

Escape

{
  "\\\\" => "\\",
  "\\n"  => "\n",
  "\\r"  => "\r",
  "\\,"  => ","
}
Ref =

Wikipedia の URL の正規表現

/^http:\/\/(.+?)\.wikipedia\.org\/wiki\/([^#]+?)$/
/<li class="interlanguage-link interwiki-(.+?)"><a href="\/\/(.+?)\.wikipedia\.org\/wiki\/(.+?)" title="(.+?) – /

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.wikipedia_intervalNumeric

Wikipedia の連続的な参照を抑制するための遅延時間/秒

Returns:



71
72
73
# File 'lib/when_exe/parts/locale.rb', line 71

def wikipedia_interval
  @wikipedia_interval
end

Instance Attribute Details

#keysArray<String> (readonly)

有効なローケール指定

Returns:



363
364
365
# File 'lib/when_exe/parts/locale.rb', line 363

def keys
  @keys
end

文字列の説明 - additional attribute

Returns:

  • (Hash)

    { anyURI }



373
374
375
# File 'lib/when_exe/parts/locale.rb', line 373

def link
  @link
end

#namesHash (readonly)

ローケール指定時の文字列

Returns:

  • (Hash)


358
359
360
# File 'lib/when_exe/parts/locale.rb', line 358

def names
  @names
end

#valuesArray<String> (readonly)

有効な文字列 - additional attribute

Returns:



368
369
370
# File 'lib/when_exe/parts/locale.rb', line 368

def values
  @values
end

Class Method Details

._hash_value(hash, locale, default = '') ⇒ Object

locale 指定を解析して Hash の値を取り出す



238
239
240
241
242
243
244
245
# File 'lib/when_exe/parts/locale.rb', line 238

def _hash_value(hash, locale, default='')
  locale = locale.sub(/\..*/, '').sub(/-/,'_')
  return hash[locale] if (hash[locale])
  return _hash_value(hash, _alias[locale], default) if _alias[locale]
  language = locale.sub(/_.*/, '')
  return hash[language] if (hash[language])
  return hash[default]
end

._locale(source = nil) ⇒ Object

locale 指定を Array に変換する



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/when_exe/parts/locale.rb', line 183

def _locale(source=nil)
  # default の Locale
  return [[nil, '', nil]] unless source

  # source の配列化
  if source.kind_of?(String)
    source = $1 if (source=~/\A\s*\[?(.+?)\]?\s*\z/m)
    source = source.scan(/((?:[^\\\n\r,]|\\.)+)(?:[\n\r,]+(?!\z))?|[\n\r,]+/m).flatten.map {|token|
      (token||'').gsub(/\\./) {|escape| Escape[escape] || escape}
    }
  end

  # 各Localeの展開
  source.map {|v|
    if v.kind_of?(String)
      v = v.strip
      next if (v=~/^#/)
      (v =~ /^(\*)?(.*?)(?:\s*=\s*(.*))?$/) ? $~[1..3] : [[nil, '', nil]]
    else
      v
    end
  }.compact
end

._namespace(source = nil) ⇒ Object

文字列で表現された namespace 指定を Hash に変換する



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/when_exe/parts/locale.rb', line 161

def _namespace(source=nil)
  case source
  when Hash ; source
  when nil  ; {}
  when String
    namespace = {}
    source = $1 if (source=~/\A\s*\[?(.+?)\]?\s*\z/m)
    source.split(/[\n\r,]+/).each do |v|
      v.strip!
      next if (v=~/^#/)
      pair = [''] + v.split(/\s*=\s*/, 2)
      namespace[pair[-2]] = pair[-1]
    end
    namespace
  when When::Parts::Resource::ContentLine
    source.object.names
  else ; raise TypeError, "Irregal Namespace Type: #{source.class}"
  end
end

._setup_(options = {}) ⇒ Object

Note:

:alias の指定がない場合、aliases は DefaultAlias(モジュール定数)と解釈する。 :unification の指定がない場合、unifications は DefaultUnification(モジュール定数)と解釈する。

When::Parts::Locale Module のグローバルな設定を行う

Parameters:

  • options (Hash) (defaults to: {})

    下記の通り

Options Hash (options):

  • :alias (Hash)

    Locale の読み替えパターンを Hash で指定する。

  • :unification (Hash)

    漢字の包摂パターンを Hash で指定する。

  • :wikipedia_interval (Numeric)

    Wikipedia の連続的な参照を抑制するための遅延時間/秒



84
85
86
87
88
# File 'lib/when_exe/parts/locale.rb', line 84

def _setup_(options={})
  @aliases            = options[:alias]       || DefaultAlias
  @unifications       = options[:unification] || DefaultUnification
  @wikipedia_interval = options[:wikipedia_interval]
end

._setup_infoHash

設定情報を取得する

Returns:

  • (Hash)

    設定情報



94
95
96
97
98
# File 'lib/when_exe/parts/locale.rb', line 94

def _setup_info
  {:alias              => _alias,
   :unification        => _unification,
   :wikipedia_interval => @wikipedia_interval}
end

._split(source) ⇒ Object

文字列 [.., .., ..] を分割する



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/when_exe/parts/locale.rb', line 209

def _split(source)
  line  = source.dup
  return [line] unless line =~ /,/
  list  = []
  b = d = s = 0
  (source.length-1).downto(0) do |i|
    bs = 0
    (i-1).downto(0) do |k|
      break unless (line[k,1] == '\\')
      bs += 1
    end
    next if (bs[0] == 1)
    case line[i,1]
    when "'"         ; s  = 1-s  if (d == 0)
    when '"'         ; d  = 1-d  if (s == 0)
    when ']','}',')' ; b += 1 if  (d+s == 0)
    when '[','{','(' ; b -= 1 if  (d+s == 0 && b > 0)
    when ','
      if (b+d+s == 0)
        list.unshift(line[i+1..-1])
        line = i > 0 ? line[0..i-1] : ''
      end
    end
  end
  list.unshift(line)
end

._unificationObject

漢字の包摂パターン



249
250
251
# File 'lib/when_exe/parts/locale.rb', line 249

def _unification
  @unifications ||= DefaultUnification
end

.ideographic_unification(source, pattern = _unification) ⇒ When::Parts::Locale, ...

包摂リストに登録されている文字を包摂する

Parameters:

  • source (When::Parts::Locale)

    文字を包摂しようとする国際化文字列

  • source (String)

    文字を包摂しようとする文字列

  • source (Regexp)

    文字を包摂しようとする正規表現

  • pattern (Hash) (defaults to: _unification)

    包摂ルール

Returns:

  • (When::Parts::Locale)

    文字を包摂した国際化文字列

  • (String)

    文字を包摂した文字列

  • (Regexp)

    文字を包摂した正規表現



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/when_exe/parts/locale.rb', line 144

def ideographic_unification(source, pattern=_unification)
  case source
  when When::Parts::Locale
    source.ideographic_unification(pattern)
  when Regexp
    Regexp.compile(ideographic_unification(source.source.encode('UTF-8'), pattern), source.options)
  when String
    source.gsub(/./) do |c|
      pattern[c] ? pattern[c] : c
    end
  else
    source
  end
end

.translate(source, loc = '') ⇒ String

Note:

source が Hash や Array の場合、その構成要素を変換して返す

Note:

encode は通常大文字だが、大文字/小文字の変換は行わず指定されたまま使用している

特定 locale に対応した文字列の取得

@param loc locale の指定 ( langcountry.encode )

[ lang    - 言語               ]
[ country - (省略可)         ]
[ encode  - 文字コード(省略可) ]

Parameters:

  • source (String)

    もとにする String または M17n

Returns:

  • (String)

    loc に対応した文字列



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/when_exe/parts/locale.rb', line 114

def translate(source, loc='')
  return source unless loc
  case source
  when Hash
    result = {}
    source.each_pair do |key, value|
      result[translate(key, loc)] = translate(value, loc)
    end
    return result
  when Array
    return source.map {|value| translate(value, loc)}
  when Locale
    return source.translate(loc)
  when String
    return source.encode($1) if loc =~ /\.(.+)$/
  end
  source
end

Instance Method Details

#+(other) ⇒ When::Toos::Locale

文字列の連結

Parameters:

  • other (String, When::Toos::Locale)

    連結する文字列

Returns:

  • (When::Toos::Locale)

    連結された文字列



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/when_exe/parts/locale.rb', line 462

def +(other)
  names = {}
  case other
  when Locale
    (@names.keys + other.names.keys).uniq.each do |key|
      names[key] = _label_value(key) + other._label_value(key)
    end
    links = other.link
  else
    @names.keys.each do |key|
      names[key] = _label_value(key) + other.to_s
    end
    links = {}
  end
  return dup._copy({:names=>names, :link=>links.merge(link), :label=>to_s + other.to_s})
end

#=~(regexp) ⇒ Integer?

文字列の一致

Parameters:

Returns:

  • (Integer)

    マッチした位置のindex(いずれかの locale でマッチが成功した場合)

  • (nil)

    すべての locale でマッチに失敗した場合



433
434
435
436
437
438
439
# File 'lib/when_exe/parts/locale.rb', line 433

def =~(regexp)
  @keys.each do |key|
    index = (@names[key] =~ regexp)
    return index if index
  end
  return nil
end

#[](range) ⇒ When::Parts::Locale

部分文字列

Parameters:

  • range (Range)

    String#[] と同様の指定方法で範囲を指定する

Returns:



416
417
418
419
420
421
422
423
424
# File 'lib/when_exe/parts/locale.rb', line 416

def [](range)
  dup._copy({
    :label => to_s[range],
    :names => @names.keys.inject({}) {|l,k|
      l[k] =  @names[k][range]
      l
    }
  })
end

#_printf(other, locale = nil) ⇒ When::Toos::Locale Also known as: %

書式指定による文字列化

Parameters:

  • other (Array<Object>)

    文字列化する Object の Array

  • locale (Array<String>) (defaults to: nil)

    文字列化を行う locale の指定(デフォルト : すべて)

Returns:

  • (When::Toos::Locale)

    文字列化された Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/when_exe/parts/locale.rb', line 486

def _printf(other, locale=nil)
  # 処理する配列
  terms = other.kind_of?(Array) ? [self] + other : [self, other]

  # locale key の配列
  if locale == []
    keys = []
  else
    keys = terms.inject([]) {|k,t|
      k += t.keys if t.kind_of?(Locale)
      k
    }.uniq
    if locale
      locale = [locale] unless locale.kind_of?(Array)
      keys   = locale | (locale & keys)
    end
  end
  keys << nil if keys.include?('')

  # names ハッシュ
  names = keys.inject({}) {|l,k|
    l[k] = When::Coordinates::Pair._format(
             (block_given? ? yield(k, *terms) : terms).map {|t|
               t.kind_of?(Locale) ? t.translate(k) : t
             }
           )
    l
  }

  # link ハッシュ
  links = terms.reverse.inject({}) {|h,t|
    h.update(t.link) if t.kind_of?(Locale)
    h
  }

  # 生成
  dup._copy({
    :label => keys.include?('') ? names.delete(nil) : (names[''] = names[keys[0]]),
    :names => names,
    :link  => links
  })
end

#ideographic_unification(pattern = _unification) ⇒ When::Parts::Locale

包摂リストに登録されている文字を包摂する(自己保存)

Parameters:

  • pattern (Hash) (defaults to: _unification)

    包摂ルール

Returns:



578
579
580
# File 'lib/when_exe/parts/locale.rb', line 578

def ideographic_unification(pattern=_unification)
  dup.ideographic_unification!(pattern)
end

#index(other) ⇒ Integer?

部分文字列の位置

Parameters:

  • other (String)

    部分文字列

Returns:

  • (Integer)

    部分文字列の先頭のindex(いずれかの locale で部分文字列を含んだ場合)

  • (nil)

    すべての locale で部分文字列を含まない場合



448
449
450
451
452
453
454
# File 'lib/when_exe/parts/locale.rb', line 448

def index(other)
  @keys.each do |key|
    index = @names[key].index(Locale.translate(other, key))
    return index if index
  end
  return nil
end

#prefix(other, locale = nil) ⇒ Object

閏の表記を扱うための書式付整形



584
585
586
587
588
589
590
591
592
# File 'lib/when_exe/parts/locale.rb', line 584

def prefix(other, locale=nil)
  return self.dup unless other
  other = m17n(other) unless other.kind_of?(Locale)
  other._printf(self, locale) do |k, *t|
    t[0]  = t[0].translate(k)
    t[0] += "%s" unless t[0] =~ /%[-+]?[.\d]*s/
    t
  end
end

#reference(loc = '') ⇒ String

特定 locale に対応した reference URI の取得

Parameters:

  • loc (String) (defaults to: '')

    locale の指定

Returns:

  • (String)

    loc に対応した reference URI



405
406
407
408
# File 'lib/when_exe/parts/locale.rb', line 405

def reference(loc='')
  loc ||= ''
  return Locale._hash_value(@link, loc)
end

#translate(loc = '') ⇒ String Also known as: /

特定 locale に対応した文字列の取得

Parameters:

  • loc (String) (defaults to: '')

    locale の指定 ( langcountry.encode )

    lang - 言語
    country - 国(省略可)
    encode - 文字コード(省略可)

Returns:

  • (String)

    loc に対応した文字列



390
391
392
393
394
395
396
# File 'lib/when_exe/parts/locale.rb', line 390

def translate(loc='')
  return to_s unless loc
  lang, code = loc.split(/\./)
  result = _label_value(loc)
  return result if !code || @names.member?(loc)
  return result.encode(code)
end

#update(options = {}) ⇒ self

Note:

Hashキーはすべて Optional で、存在するもののみ更新します

ローケールの更新

Parameters:

  • options (Hash) (defaults to: {})

    下記の通り

Options Hash (options):

  • カントリーコード (String)

    表現文字列

  • :link (Hash)

    { カントリーコード => 参照URL文字列 }

  • :code_space (String)

    規格や辞書を特定するコードスペースのURL文字列

  • :label (String)

    代表文字列

Returns:

  • (self)

    更新された Object



541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/when_exe/parts/locale.rb', line 541

def update(options={})
  options = options.dup
  @link.update(options.delete(:link))       if (options.key?(:link))
  @code_space = options.delete(:code_space) if (options.key?(:code_space))
  self[0..-1] = options.delete(:label)      if (options.key?(:label))
  unless (options.empty?)
    @names.update(options)
    @keys     = @names.keys.sort
    @values   = @names.values.sort.reverse
  end
  return self
end