Skip to content

Commit

Permalink
ncp-web: add localization (#372)
Browse files Browse the repository at this point in the history
* Implement localization loader L10N.

Localize a few strings in index.php for testing.

* Determine language from HTTP_ACCEPT_LANGUAGE header.

* Fix parsing of available languages for localization.

* Fix parsing of language translations (for localization).

* Fix function L10N->find_language

* Change variable naming to comply with conventions.

* Add remaining localize calls in index.php.

Add more localization strings.

* Fix styles to prevent check mark from being hidden.

* Handle exception in case localization couldn't be loaded.

* Add module localization support.

* Fix detection of core module in L10N->load

* Remove module specific localization from core l10n file.

* Fix determination of module name in L10N->load.

* Add localization files for more modules.

* Ignore l10n directory in modules path.

* Add support for dropdown lists in module settings.

* Add support for saving dropdown menus.

* Fix regex for recognizing dropdown option lists.

* Fix dropdown list regex not recognizing active option.

* Fix typo in ncp-launcher.php

* Workaround for jquery's 'find' method not working. //TODO: Find proper fix.

* Fix parsing of dropdown lists when updating config.

* Load language setting from config if available.

* add ncp-provisioning to SD card images

* Add intellij config to gitignore

* Fix bug when loading language from nc-webui config.

* Remove redundant language definition.
Replace umlauts by html special char expressions.

* Change selected option markup from `*<option>*` to `_<option>_`.

* Remove ide metadata.

* Add translations for ncp-app nc-backup.

* Add translation hooks to ncp-launcher.php

* Fix type in translations for nc-backup

* Remove translation hooks for field contents in ncp-launcher.php
  • Loading branch information
theCalcaholic authored and nachoparker committed Feb 26, 2018
1 parent c4a111c commit 1a7c8b9
Show file tree
Hide file tree
Showing 44 changed files with 385 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.*.swp
*.img
*.bz2
.idea/
qemu-raspbian-network/
torrent/
armbian/
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

[v0.46.34](https://github.com/nextcloud/nextcloudpi/commit/2694914) (2018-02-26) ncp-web: fix responsive in iPad
[v0.46.35](https://github.com/nextcloud/nextcloudpi/commit/e417926) (2018-02-26) ncp-web: add localization (#372)

[v0.46.34](https://github.com/nextcloud/nextcloudpi/commit/3fc902b) (2018-02-26) ncp-web: fix responsive in iPad

[v0.46.33](https://github.com/nextcloud/nextcloudpi/commit/dea4836) (2018-02-23) Added some useful comments for first time users

Expand Down
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-audit/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-audit": "Sicherheitsüberprüfung"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-automount/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-automount": "Automatische Datenträgereinbindung"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-autoupdate-ncp/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-autoupdate-ncp": "Automatische NextCloudPi Updates"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-backup-auto/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-backup-auto": "Automatische Backups"
}}
7 changes: 7 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-backup/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"translations": {
"nc-backup": "Backup erstellen",
"DESTDIR": "Zielverzeichnis",
"INCLUDEDATA": "Inkl. Dateien",
"COMPRESS": "Komprimieren",
"BACKUPLIMIT": "Maximale Anzahl"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-database/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-database": "Datenbank"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-datadir/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-datadir": "Datenverzeichnis"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-export-npc/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-export-ncp": "NCP Export"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-fix-permissions/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-fix-permissions": "Berechtigungen wiederherstellen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-format-USB/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-format-USB": "USB Laufwerke formatieren"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-forward-ports/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-forward-ports": "Portweiterleitungen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-httpsonly/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-httpsonly": "HTTPS Zwang"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-import-ncp/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-import-ncp": "NCP Import"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-info/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-info": "System Informationen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-init/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-init": "Initialisierung"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-limits/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-limits": "Systembegrenzungen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-nextcloud/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-nextcloud": "Nextcloud Einstellungen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-notify-updates/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-notify-updates": "Update-Benachrichtigungen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-passwd/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-passwd": "NCP Passwort"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-ramlogs/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-ramlogs": "Ram Logs"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-restore/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-restore": "Wiederherstellung"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-scan-auto/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-scan-auto": "Automatischer Datei-Scan"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-scan/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-scan": "Datei-Scan"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-snapshot-auto/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-snapshot-auto": "Automatische Snapshots"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-snapshot/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-snapshot": "Snapshot erstellen"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-static-IP/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-static-IP": "Statische IP Adresse"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-swapfile/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-swapfile": "Swap Datei"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-update/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-update": "Update"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-webui/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-webui": "NCP Weboberfläche"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/nc-wifi/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"nc-wifi": "WLAN"
}}
3 changes: 3 additions & 0 deletions etc/nextcloudpi-config.d/l10n/unattended-upgrades/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"translations": {
"unattended-upgrades": "Unüberwachte System-Upgrades"
}}
1 change: 1 addition & 0 deletions etc/nextcloudpi-config.d/nc-webui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#

ACTIVE_=yes
LANGUAGE_=[_auto_,en,de]
DESCRIPTION="Enable or disable the NCP web interface"

configure()
Expand Down
127 changes: 127 additions & 0 deletions ncp-web/L10N.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

class L10N {
private $translations = [];
const default_language = "en_US";
private $language;

public function __construct($desired_languages, $l10n_dir, $modules_path=null)
{
if (!isset($desired_languages)) {
$desired_languages = "";
}

$l10n_dir = rtrim($l10n_dir, '/');
$available_languages = array_filter(scandir($l10n_dir),
function ($s) {
return pathinfo($s, PATHINFO_EXTENSION) == "json";
});
$available_languages = array_map(
function ($s) {
return basename($s, ".json");
},
$available_languages);

$desired_lang = $this->load_language_setting($modules_path);
if($desired_lang != "auto")
{
$desired_languages = $desired_lang;
}
$lang = $this->find_language($available_languages, $desired_languages);
$this->language = $lang;
if ($lang === L10N::default_language || !file_exists(join('/', [$l10n_dir, $lang . ".json"])) || !$this->load($lang, $l10n_dir, $modules_path)) {
$this->language = L10N::default_language;
}
}

function load($lang, $l10n_dir, $modules_path)
{
$modules_path = join('/', [rtrim($modules_path, '/'), $l10n_dir]);
$files = [join('/', [$l10n_dir, $lang . ".json"])];
if (is_dir($modules_path)) {
foreach (scandir($modules_path) as $dir) {
$file_path = join('/', [$modules_path, trim($dir, '/'), $lang . ".json"]);
if (is_file($file_path)) {
array_push($files, $file_path);
}
}
}

$jsonError = null;
foreach ($files as $file) {
$module_name = pathinfo(pathinfo($file, PATHINFO_DIRNAME), PATHINFO_BASENAME);
if( $module_name == pathinfo($l10n_dir, PATHINFO_BASENAME)) {
$module_name = "__core__";
}
$json = json_decode(file_get_contents($file), true);
if (!is_array($json)) {
$jsonError = json_last_error();
continue;
}
$this->translations[$module_name] = $json['translations'];
}
return ($jsonError == null);
}

public function __($text, $module="__core__") {
if( isset($this->translations[$module]) && isset($this->translations[$module][$text])) {
return $this->translations[$module][$text];
}
return $text;
}

function find_language(array $available_languages, $desired_language) {

$available_languages = array_flip($available_languages);
$langs = [];
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($desired_language), $matches, PREG_SET_ORDER);

foreach($matches as $match) {

list($a, $b) = explode('-', $match[1]) + array('', '');
$value = isset($match[2]) ? (float) $match[2] : 1.0;

if(isset($available_languages[$match[1]])) {
$langs[$match[1]] = $value;
continue;
}

if(isset($available_languages[$a])) {
$langs[$a] = $value - 0.1;
}

}
arsort($langs);

if(count($langs) == 0) {
return L10N::default_language;
}
return array_keys($langs)[0];
}

function load_language_setting($modules_path) {
$webui_config_file = join('/', [rtrim($modules_path, '/'), 'nc-webui.sh']);
$fh = fopen( $webui_config_file ,'r')
or exit( '{ "output": "' . $webui_config_file . ' read error" }' );
$lang = "auto";
while ( $line = fgets($fh) )
{
// drop down menu
if(preg_match('/^LANGUAGE_=\[(([_\w]+,)*[_\w]+)\]$/', $line, $matches))
{
$options = explode(',', $matches[1]);
foreach($options as $option)
{
if($option[0] == "_" && $option[count($option) - 1] == "_")
{
fclose($fh);
$lang = trim($option,'_');
}
}
fclose($fh);
return $lang;
}
}

}
}
Loading

0 comments on commit 1a7c8b9

Please sign in to comment.