pass translations to frontend as a json object

master
Andrew Dolgov 2 years ago
parent fd9cd52929
commit 56b10fea18
  1. 2
      classes/feeds.php
  2. 43
      classes/rpc.php
  3. 36
      include/functions.php
  4. 2
      index.php
  5. 16
      js/App.js
  6. 16
      js/common.js
  7. 2
      prefs.php

@ -509,7 +509,7 @@ class Feeds extends Handler_Protected {
"disable_cache" => (bool) $disable_cache];
// this is parsed by handleRpcJson() on first viewfeed() to set cdm expanded, etc
$reply['runtime-info'] = RPC::make_runtime_info();
$reply['runtime-info'] = RPC::_make_runtime_info();
print json_encode($reply);
}

@ -7,6 +7,38 @@ class RPC extends Handler_Protected {
return array_search($method, $csrf_ignored) !== false;
}*/
private function _translations_as_array() {
global $text_domains;
$rv = [];
foreach (array_keys($text_domains) as $domain) {
$l10n = _get_reader($domain);
for ($i = 0; $i < $l10n->total; $i++) {
if (isset($l10n->table_originals[$i * 2 + 2]) && $orig = $l10n->get_original_string($i)) {
if(strpos($orig, "\000") !== false) { // Plural forms
$key = explode(chr(0), $orig);
//print T_js_decl($key[0], _ngettext($key[0], $key[1], 1)); // Singular
//print T_js_decl($key[1], _ngettext($key[0], $key[1], 2)); // Plural
$rv[$key[0]] = _ngettext($key[0], $key[1], 1); // Singular
$rv[$key[1]] = _ngettext($key[0], $key[1], 2); // Plural
} else {
$translation = _dgettext($domain,$orig);
//print T_js_decl($orig, $translation);
$rv[$orig] = $translation;
}
}
}
}
return $rv;
}
function togglepref() {
$key = clean($_REQUEST["key"]);
set_pref($key, !get_pref($key));
@ -66,7 +98,7 @@ class RPC extends Handler_Protected {
function getRuntimeInfo() {
$reply = [
'runtime-info' => $this->make_runtime_info()
'runtime-info' => $this->_make_runtime_info()
];
print json_encode($reply);
@ -147,8 +179,9 @@ class RPC extends Handler_Protected {
if ($error == Errors::E_SUCCESS) {
$reply = [];
$reply['init-params'] = $this->make_init_params();
$reply['runtime-info'] = $this->make_runtime_info();
$reply['init-params'] = $this->_make_init_params();
$reply['runtime-info'] = $this->_make_runtime_info();
$reply['translations'] = $this->_translations_as_array();
print json_encode($reply);
} else {
@ -377,7 +410,7 @@ class RPC extends Handler_Protected {
print json_encode($rv);
}
private function make_init_params() {
private function _make_init_params() {
$params = array();
foreach ([Prefs::ON_CATCHUP_SHOW_NEXT_FEED, Prefs::HIDE_READ_FEEDS,
@ -440,7 +473,7 @@ class RPC extends Handler_Protected {
}
}
static function make_runtime_info() {
static function _make_runtime_info() {
$data = array();
$pdo = Db::pdo();

@ -455,42 +455,6 @@
}
}
function init_js_translations() {
print 'var T_messages = new Object();
function __(msg) {
if (T_messages[msg]) {
return T_messages[msg];
} else {
return msg;
}
}
function ngettext(msg1, msg2, n) {
return __((parseInt(n) > 1) ? msg2 : msg1);
}';
global $text_domains;
foreach (array_keys($text_domains) as $domain) {
$l10n = _get_reader($domain);
for ($i = 0; $i < $l10n->total; $i++) {
$orig = $l10n->get_original_string($i);
if(strpos($orig, "\000") !== false) { // Plural forms
$key = explode(chr(0), $orig);
print T_js_decl($key[0], _ngettext($key[0], $key[1], 1)); // Singular
print T_js_decl($key[1], _ngettext($key[0], $key[1], 2)); // Plural
} else {
$translation = _dgettext($domain,$orig);
print T_js_decl($orig, $translation);
}
}
}
}
function get_theme_path($theme) {
$check = "themes/$theme";
if (file_exists($check)) return $check;

@ -97,8 +97,6 @@
}
}
}
init_js_translations();
?>
</script>

@ -17,6 +17,15 @@ const App = {
hotkey_actions: {},
is_prefs: false,
LABEL_BASE_INDEX: -1024,
_translations: {},
l10n: {
ngettext: function(msg1, msg2, n) {
return self.__((parseInt(n) > 1) ? msg2 : msg1);
},
__: function(msg) {
return App._translations[msg] ? App._translations[msg] : msg;
}
},
FormFields: {
attributes_to_string: function(attributes) {
return Object.keys(attributes).map((k) =>
@ -525,6 +534,13 @@ const App = {
PluginHost.run(PluginHost.HOOK_PARAMS_LOADED, this._initParams);
}
const translations = reply['translations'];
if (translations) {
console.log('reading translations...');
App._translations = translations;
}
this.initSecondStage();
},
Error: {

@ -1,8 +1,22 @@
'use strict';
/* global dijit, __, App, dojo, __csrf_token */
/* global dijit, App, dojo, __csrf_token */
/* eslint-disable no-new */
/* exported __ */
function __(msg) {
if (typeof App != "undefined") {
return App.l10n.__(msg);
} else {
return msg;
}
}
/* exported ngettext */
function ngettext(msg1, msg2, n) {
return __((parseInt(n) > 1) ? msg2 : msg1);
}
/* exported $ */
function $(id) {
console.warn("FIXME: please use App.byId() or document.getElementById() instead of $():", id);

@ -79,8 +79,6 @@
}
}
}
init_js_translations();
?>
</script>

Loading…
Cancel
Save