Sha256: 23e855627302a1394cb08b3ed0a8bc800f3eb021b08a4cacff6bf1f5ac7bc106

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

// This library provide a helper to recover current translation
// Note: To use this you will need load js translations like this:
// <script> var I18n_data = <%= I18n.backend.send(:translations)[current_locale.to_sym][:admin][:js].to_json.html_safe %> </script>

// return translation of a key
// sample: I18n('button.edit', 'Editar %{title}', {title: 'Articulo'})  ==> Edit
// return String with the translation
// default_val: (String) this value is returned if there is no exist translation for key
// if default_val is empty, will be returned the last key titleized
// data: (hash) replacement values in the value, sample: {title: 'my title'}
var I18n = function(key, default_val, data){
    try {
        var res = eval("I18n_data." + key);
        if (!res) res = default_val ? default_val : ("" + key.split(".").pop()).titleize();
    }catch(e){
        var res = (""+key.split(".").pop()).titleize();
    }

    // replacements
    data = data || {}
    for(key in data){
        console.log(key, data[key])
        res = res.replace("%{"+key+"}", data[key])
    }
    return res;
}

// helper to convert not found translations key into titleized string
String.prototype.titleize = function() {
    var words = this.replace(/_/g, " ").split(' ')
    var array = []
    for (var i=0; i<words.length; ++i) {
        array.push(words[i].charAt(0).toUpperCase() + words[i].toLowerCase().slice(1))
    }
    return array.join(' ')
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
camaleon_cms-2.4.2 app/assets/javascripts/camaleon_cms/admin/_i18n.js
camaleon_cms-2.4.1 app/assets/javascripts/camaleon_cms/admin/_i18n.js
camaleon_cms-2.4.0 app/assets/javascripts/camaleon_cms/admin/_i18n.js
camaleon_cms-2.3.7.2 app/assets/javascripts/camaleon_cms/admin/_i18n.js
camaleon_cms-2.3.7.1 app/assets/javascripts/camaleon_cms/admin/_i18n.js
camaleon_cms-2.3.7 app/assets/javascripts/camaleon_cms/admin/_i18n.js