
(function () {
  'use strict';

  var current = { lang: 'zh', strings: {} };

  window.VizI18n = {
    get current() { return current; },
    apply: function (locale) {
      if (locale) current = locale;
      var nodes = document.querySelectorAll('[data-i18n]');
      for (var i = 0; i < nodes.length; i++) {
        var key = nodes[i].getAttribute('data-i18n');
        if (current.strings[key]) nodes[i].textContent = current.strings[key];
      }
      var titles = document.querySelectorAll('[data-i18n-title]');
      for (var j = 0; j < titles.length; j++) {
        var tkey = titles[j].getAttribute('data-i18n-title');
        if (current.strings[tkey]) titles[j].title = current.strings[tkey];
      }
    },
    tr: function (key, fallback) {
      return current.strings[key] || fallback || key;
    }
  };
})();
