summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Lang.html
blob: 9f4fa9b36ddc8c43ad26464bb41a934042f7d57c (plain)
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
<html>
<head>
    <script src="OLLoader.js"></script>
    <script src="../lib/OpenLayers/Lang/en-CA.js" type="text/javascript"></script>
    <script src="../lib/OpenLayers/Lang/fr.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    function test_setCode(t) {
        t.plan(4);
        OpenLayers.Lang.code = null;
        
        // test with no argument - this could result in the default or the
        // browser language if a dictionary exists
        OpenLayers.Lang.setCode();
        t.ok(OpenLayers.Lang.code != null,
             "code set when no argument is sent");
        
        var primary = "xx";
        var subtag = "XX";
        var code = primary + "-" + subtag;
        OpenLayers.Lang[code] = {};
        
        // test code for dictionary that exists
        OpenLayers.Lang.setCode(code);
        t.eq(OpenLayers.Lang.code, code,
             "code properly set for existing dictionary");
        
        // test code for dictionary that doesn't exist
        OpenLayers.Lang.setCode(primary + "-YY");
        t.eq(OpenLayers.Lang.code, OpenLayers.Lang.defaultCode,
             "code set to default for non-existing dictionary");
        
        // test code for existing primary but missing subtag
        OpenLayers.Lang[primary] = {};
        OpenLayers.Lang.setCode(primary + "-YY");
        t.eq(OpenLayers.Lang.code, primary,
             "code set to primary when subtag dictionary is missing");
        
        // clean up
        delete OpenLayers.Lang[code];
        delete OpenLayers.Lang[primary];
        OpenLayers.Lang.code = null;
    }
    
    function test_getCode(t) {
        t.plan(3);
        OpenLayers.Lang.code = null;
        
        // test that a non-null value is retrieved - could be browser language
        // or defaultCode
        var code = OpenLayers.Lang.getCode();
        t.ok(code != null, "returns a non-null code");
        t.ok(OpenLayers.Lang.code != null, "sets the code to a non-null value");
        
        // test that the code is returned if non-null
        OpenLayers.Lang.code = "foo";
        t.eq(OpenLayers.Lang.getCode(), "foo", "returns the code if non-null");
        
        // clean up
        OpenLayers.Lang.code = null;        
    }
    
    function test_i18n(t) {
        t.plan(1);        
        t.ok(OpenLayers.i18n === OpenLayers.Lang.translate,
             "i18n is an alias for OpenLayers.Lang.translate");
    }
    
    function test_translate(t) {
        var keys = ['test1', 'test3', 'noKey'];
        var codes = ['en', 'en-CA', 'fr', 'fr-CA', 'sw'];
        var result = {
            'en': {'Overlays':'Overlays', 
                   'unhandledRequest':'Unhandled request return foo', 
                   'noKey':'noKey'},
            'en-CA': {'Overlays':'Overlays',
                   'unhandledRequest':'Unhandled request return foo', 
                      'noKey':'noKey'},
            'fr': {'Overlays':'Calques', 
                   'unhandledRequest':'Requête non gérée, retournant foo',
                  'noKey':'noKey'},
            'fr-CA': {'Overlays':'Calques', //this should result in 'fr'
                   'unhandledRequest':'Requête non gérée, retournant foo',
                  'noKey':'noKey'},
            'sw': {'Overlays':'Overlays', //this should result in 'en'
                   'unhandledRequest':'Unhandled request return foo', 
                  'noKey':'noKey'}
        };
        
        t.plan(keys.length*codes.length);

        for (var i=0; i<codes.length; ++i) {
            var code = codes[i];
            OpenLayers.Lang.setCode(code);
            t.eq(OpenLayers.Lang.translate('Overlays'), result[code]['Overlays'], "simple key lookup in "+code);
            t.eq(OpenLayers.Lang.translate('unhandledRequest',{'statusText':'foo'}), 
                        result[code]['unhandledRequest'], "lookup with argument substitution in "+code);
            t.eq(OpenLayers.Lang.translate('noKey'), result[code]['noKey'], "invalid key returns the key in "+code);
        }
    }

  </script>
</head>
<body>
</body>
</html>