~george-edison55/stackapplet/1.5.2

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
//======================================
//          translations.js
//  Manages translations and implements
//      them in HTML and JavaScript
//======================================

// The Translator object is responsible for managing
// translations and returning the proper translations
// given a 'key' which is typically the English translation.

var Translator = {
    
    // Storage variables
    translation_table: null,
    
    // Loads the translations from the given
    // URL using JSON
    LoadTranslation: function(url, callback, method) {
        
        // Make the request to the URL.
        var request = new XMLHttpRequest();
        request.open((typeof method == 'undefined')?'GET':method, url, true);
        
        // Set the handler for the response
        request.onreadystatechange = function() {
            
            if(request.readyState == 4)
            {
                // Convert the response text into JSON
                // data.
                try
                {
                    Translator.translation_table = JSON.parse(request.responseText);
                    
                    // Everything has succeeded, so call the callback
                    // with the param. 'true'.
                    callback(true);
                }
                catch(exception)
                {
                    // Something went wrong, so call the callback
                    // specifying 'false' to indicate an error.
                    callback(false);
                }
            }
            
        };
        
        // Now send off the request.
        request.send();
    },
    
    // Gets the translation for a given key
    GetTranslation: function(key) {
        
        if(Translator.translation_table == null) return key;
        return (typeof Translator.translation_table[key] == 'undefined')?key:Translator.translation_table[key];
        
    },
    
    // This function makes it easy to assign translations
    // to DOM elements by simply providing their ID as a key.
    AssignToDOM: function() {
        
        // Loop through the keys in the translation
        // table.
        for(var key in Translator.translation_table)
        {
            // Eat the '#' if present.
            if(key[0] == '#') key = key.substr(1);
            
            var element = document.getElementById(key);
            
            if(element != null)
                element.innerHTML = Translator.translation_table[key];
        }
        
    }
};

// For convenience's sake, we assign the function '_'
// to the function Translator::GetTranslation
_ = Translator.GetTranslation;