Permalink
Browse files

the sync-locales cmd line tool is now written in js (works with node)…

…, needs cleaup still, but works.
  • Loading branch information...
1 parent 81efcf8 commit 1cea9c372f6e605e7cb91499e35eb843cf88a221 @erikvold erikvold committed Jul 23, 2011
Showing with 77 additions and 55 deletions.
  1. +77 −0 sync-locales.js
  2. +0 −55 sync-locales.py
View
77 sync-locales.js
@@ -0,0 +1,77 @@
+#!/usr/bin/env node
+
+var path = require("path");
+var glob = require("glob").glob;
+var globSync = require("glob").globSync;
+var fs = require("fs");
+
+
+function Properties(file) {
+ this.file = file;
+ this.items = {};
+ var self = this;
+
+ // store the keys/values
+ var data = fs.readFileSync(file, "utf-8");
+ var list = data.match(/[^\n]*(\n|$)/g);
+
+ for (var i = list.length - 1; ~i; i--) {
+ var m = list[i].match(/(^[^#=]*)=([^\n]*)(\n|$)/, "");
+ if (!m) continue;
+ self.items[m[1]] = m[2];
+ }
+}
+
+Properties.prototype.merge = function(rhs) {
+ // remove the obsolete keys
+ for (var key in this.items)
+ if (!rhs.items[key])
+ delete this.items[key];
+
+ // add new keys
+ for (var key in rhs.items)
+ if (!this.items[key])
+ this.items[key] = "";
+};
+
+Properties.prototype.save = function() {
+ var keys = [], newStr = [], self = this;
+ for (var key in this.items) {
+ keys.push(key);
+ }
+
+ keys.sort(function(a, b) {
+ return (a.toLowerCase() > b.toLowerCase()) ? 1 : -1;
+ });
+ for (var i = 0, e = keys.length; i < e; i++) {
+ newStr.push(keys[i] + "=" + this.items[keys[i]]);
+ }
+ newStr = newStr.join("\n") + "\n";
+ fs.writeFile(self.file, newStr, "utf-8", function(e) {
+ if (e) throw e;
+ });
+};
+
+function process_properties(base, files) {
+ base = new Properties(base);
+
+ for (var i = files.length - 1, file; ~i; i--) {
+ file = files[i];
+ if (file == base.file)
+ continue
+ console.log(file);
+ file = new Properties(file);
+ file.merge(base);
+ file.save();
+ }
+}
+
+glob("extension/locale/en-US/*.properties", function(e, m) {
+ if (e) throw e;
+ m.forEach(function(f) {
+ var fn = f.match(/[^\\\/\.]*.properties$/)[0];
+ var files = globSync("extension/locale/*/" + fn);
+ process_properties(f, files);
+ });
+})
+
View
55 sync-locales.py
@@ -1,55 +0,0 @@
-#!/usr/bin/env python
-
-# Nils Maier has dedicated this work to the public domain by waiving all of his
-# rights to the work worldwide under copyright law, including all related
-# and neighboring rights, to the extent allowed by law.
-# http://creativecommons.org/publicdomain/zero/1.0/
-
-import os, sys, re
-from glob import glob
-from codecs import open as copen
-
-class Properties(object):
- def __init__(self, file):
- self.file = file
- self._items = {}
- with copen(self.file, "rb", encoding="utf-8") as fp:
- for line in fp:
- line = line.strip()
- if line.startswith("#"):
- continue
- key, value = line.split("=", 1)
- self._items[key] = value
-
- def merge(self, rhs):
- # remove obsolete
- for k in self._items.keys():
- if not k in rhs._items:
- del self._items[key]
-
- # add new keys
- for k in rhs._items.keys():
- if not k in self._items:
- self._items[k] = ""
-
- def save(self):
- with copen(self.file, "wb", encoding="utf-8") as op:
- for k in sorted(self._items.keys(), key=unicode.lower):
- op.write("%s=%s\n" % (k, self._items[k]))
-
-def process_properties(base, files):
- base = Properties(base)
- base.save()
- for file in files:
- if file == base.file:
- continue
- print file
- file = Properties(file)
- file.merge(base)
- file.save()
-
-
-if __name__ == "__main__":
- for f in glob("extension/locale/en-US/*.properties"):
- fn = os.path.basename(f)
- process_properties(f, glob("extension/locale/*/" + fn))

0 comments on commit 1cea9c3

Please sign in to comment.