File: test_locale_path.rb

package info (click to toggle)
ruby-gettext 3.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,284 kB
  • ctags: 1,287
  • sloc: ruby: 9,178; makefile: 8
file content (107 lines) | stat: -rw-r--r-- 4,318 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013  Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright (C) 2012-2013  Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2010  masone (Christian Felder) <ema@rh-productions.ch>
# Copyright (C) 2009-2010  Masao Mutoh
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'fixtures/simple'

class TestLocalePath < Test::Unit::TestCase
  def setup
    GetText.locale = "ja_JP.eucJP"
  end

  def teardown
    GetText.locale = nil
  end

  def test_locale_path
    test = Simple.new
    assert_equal("japanese", test.test)
    prefix = GetText::LocalePath::CONFIG_PREFIX
    default_locale_dirs = [
      "./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",
      "#{prefix}/share/locale/%{lang}/LC_MESSAGES/%{name}.mo",
      "#{prefix}/local/share/locale/%{lang}/LC_MESSAGES/%{name}.mo"
    ].uniq
    assert_equal(default_locale_dirs, GetText::LocalePath::DEFAULT_RULES)
    new_path = "/foo/%{lang}/%{name}.mo"
    GetText::LocalePath.add_default_rule(new_path)
    assert_equal([new_path] + default_locale_dirs, GetText::LocalePath::DEFAULT_RULES)
  end

  def test_initialize_with_topdir
    testdir = File.dirname(File.expand_path(__FILE__))
    path = GetText::LocalePath.new("test1", "#{testdir}/locale")
    assert_equal({
                   "ja"      => "#{testdir}/locale/ja/LC_MESSAGES/test1.mo",
                   "fr"      => "#{testdir}/locale/fr/LC_MESSAGES/test1.mo",
                   "zh_Hant" => "#{testdir}/locale/zh_Hant/LC_MESSAGES/test1.mo"
                 },
                 path.locale_paths)
    assert_equal("#{testdir}/locale/ja/LC_MESSAGES/test1.mo",
                 path.current_path(Locale::Tag.parse("ja")))
    assert_equal("#{testdir}/locale/ja/LC_MESSAGES/test1.mo",
                 path.current_path(Locale::Tag.parse("ja-JP")))
    assert_equal("#{testdir}/locale/ja/LC_MESSAGES/test1.mo",
                 path.current_path(Locale::Tag.parse("ja_JP.UTF-8")))
    assert_equal(nil,
                 path.current_path(Locale::Tag.parse("en")))
    assert_equal("#{testdir}/locale/zh_Hant/LC_MESSAGES/test1.mo",
                 path.current_path(Locale::Tag.parse("zh-Hant")))
  end

  def test_supported_locales
    testdir = File.dirname(File.expand_path(__FILE__))
    path = GetText::LocalePath.new("test1", "#{testdir}/locale")
    assert_equal ["fr", "ja", "zh_Hant"], path.supported_locales

    path = GetText::LocalePath.new("plural", "#{testdir}/locale")
    assert_equal ["cr", "da", "fr", "ir", "ja", "la", "li", "po", "sl"], path.supported_locales

    path = GetText::LocalePath.new("nodomain", "#{testdir}/locale")
    assert_equal [], path.supported_locales
  end

  def test_env_GETTEXT_PATH
    topdir = File.join(File.dirname(File.expand_path(__FILE__)), "../samples")
    path1 = File.join(topdir, "locale")
    path2 = File.join(topdir, "cgi", "locale")

    ENV["GETTEXT_PATH"] = path1
    default_path_rules = GetText::LocalePath.default_path_rules
    assert_match(Regexp.compile(path1), default_path_rules[0])

    ENV["GETTEXT_PATH"] = "#{path1},#{path2}"
    default_path_rules = GetText::LocalePath.default_path_rules
    assert_match(Regexp.compile(path1), default_path_rules[0])
    assert_match(Regexp.compile(path2), default_path_rules[1])
  end

  class TestDefaultPathRules < self
    def test_load_path_untached
      $LOAD_PATH.unshift("./lib")
      GetText::LocalePath.default_path_rules
      assert_equal($LOAD_PATH[0], "./lib")
    end
  end
end