This repository was archived by the owner on Nov 16, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlanguages_stats.js
More file actions
85 lines (69 loc) · 2.24 KB
/
Copy pathlanguages_stats.js
File metadata and controls
85 lines (69 loc) · 2.24 KB
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
const Lang = imports.lang;
const Signals = imports.signals;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const PrefsKeys = Me.imports.prefs_keys;
const Utils = Me.imports.utils;
const TYPE_SOURCE = 'source';
const TYPE_TARGET = 'target';
const LanguagesStats = new Lang.Class({
Name: 'LanguagesStats',
_init: function() {
this._reload();
},
_reload: function() {
this._json_data = Utils.SETTINGS.get_string(
PrefsKeys.LANGUAGES_STATS_KEY
);
this._storage = JSON.parse(this._json_data);
if(this._storage instanceof Array) {
this._storage = {};
}
},
increment: function(translator_name, type, lang_data) {
let key_string = "%s-%s-%s".format(
translator_name,
type,
lang_data.code
)
if(key_string in this._storage) {
this._storage[key_string].count++;
}
else {
let data = {
lang_name: lang_data.name,
lang_code: lang_data.code,
count: 1
};
this._storage[key_string] = data;
}
this.save();
},
get_n_most_used: function(translator_name, type, n) {
n = n || 5;
let key_string = "%s-%s".format(translator_name, type);
let keys = Object.keys(this._storage);
let filtered = keys.filter(Lang.bind(this, function(key) {
if(this._storage[key].count <= 3) return false;
return Utils.starts_with(key, key_string);
}));
filtered.sort(Lang.bind(this, function(a, b) {
return this._storage[b].count > this._storage[a].count;
}));
let result = [];
for(let i = 0; i < filtered.length; i++) {
if(i >= n) break;
let clone = JSON.parse(JSON.stringify(this._storage[filtered[i]]));
result.push(clone);
}
return result.slice(0);
},
save: function() {
Utils.SETTINGS.set_string(
PrefsKeys.LANGUAGES_STATS_KEY,
JSON.stringify(this._storage)
)
this.emit("stats-changed");
}
});
Signals.addSignalMethods(LanguagesStats.prototype);