Browse Source

Merge pull request #81 from plepe/repoTrans

Repo trans
master
Stephan Bösch-Plepelits 6 years ago
committed by GitHub
parent
commit
4539c53f4c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      doc/TwigJS.md
  2. 2
      package.json
  3. 7
      repo.php
  4. 6
      src/CategoryOverpass.js
  5. 2
      src/OpenStreetBrowserLoader.js
  6. 39
      src/RepositoryBase.php
  7. 16
      src/RepositoryDir.php
  8. 16
      src/RepositoryGit.php
  9. 4
      src/tagTranslations.js

2
doc/TwigJS.md

@ -45,6 +45,7 @@ When rendering map features, the following properties are available:
* `meta.uid` (UID of the user, who changed the object last)
* `map.zoom` (Current zoom level)
* `const.*` (Values from the 'const' option)
* `user.*` (Values from the user's options, e.g. `user.ui_lang`, `user.data_lang`, ...)
For the info-section of a category the following properties are available:
* `layer_id` (the id of the category)
@ -57,6 +58,7 @@ There are several extra functions defined for the TwigJS language:
* function `tagTransList`: return the translations of the given tag for tags with multiple values separated by ';' (e.g. 'cuisine'). Parameters: key (required, e.g. 'cuisine'), value (required, e.g. 'kebab' or 'kebab;pizza;noodles;burger').
* function `localizedTag`: return a localized tag if available (e.g. 'name:de' for the german translation of the tag). Parameters: tags (the tags property), key prefix (e.g. 'name'). Which language will be returned depends on the "data language" which can be set via Options. If no localized tag is available, the tag value itself will be returned (e.g. value of 'name').
* function `trans`: return the translation of the given string (e.g. 'save', 'unknown', 'unnamed', ...). Parameters: string (the string to translate).
* function `repoTrans`: translate strings from this repositories' language file (located in `lang/xy.json`, where `xy` stands for the current locale).
* function `tagsPrefix(tags, prefix)`: return all tags with the specified prefix. The result will be an array with `{ "en": "name:en", "de": "name:de" }` (for the input `{ "name": "foo", "name:en": "english foo", "name:de": "german foo" }` and the prefix "name:").
* function openingHoursState(opening_hours_definition): returns state of object as string: 'closed', 'open' or 'unknown'.
* function colorInterpolate(map, value): interpolates between two or more colors. E.g. `colorInterpolate([ 'red', 'yellow', 'green' ], 0.75)`.

2
package.json

@ -35,7 +35,7 @@
"openstreetbrowser-categories-main": "https://github.com/plepe/openstreetbrowser-categories-main",
"openstreetmap-date-parser": "^0.1.0",
"openstreetmap-tag-translations": "https://github.com/plepe/openstreetmap-tag-translations",
"overpass-layer": "^2.0.0",
"overpass-layer": "^2.3.0",
"query-string": "^5.0.0",
"sheet-router": "^4.2.3",
"temaki": "^1.0.0",

7
repo.php

@ -35,6 +35,9 @@ if (!isset($_REQUEST['repo'])) {
$fullRepoId = $_REQUEST['repo'];
list($repoId, $branchId) = explode('~', $fullRepoId);
if (array_key_exists('lang', $_REQUEST)) {
$fullRepoId .= '~' . $_REQUEST['lang'];
}
if (!array_key_exists($repoId, $allRepositories)) {
Header("HTTP/1.1 404 Repository not found");
@ -67,7 +70,9 @@ if (isset($config['cache'])) {
}
}
$data = $repo->data();
$data = $repo->data($_REQUEST);
$repo->updateLang($data, $_REQUEST);
if (!array_key_exists('index', $data['categories'])) {
$data['categories']['index'] = array(

6
src/CategoryOverpass.js

@ -107,6 +107,12 @@ function CategoryOverpass (options, data) {
this.layer.on('add', (ob, data) => this.emit('add', ob, data))
this.layer.on('remove', (ob, data) => this.emit('remove', ob, data))
this.layer.on('zoomChange', (ob, data) => this.emit('remove', ob, data))
this.layer.on('twigData',
(ob, data, result) => {
result.user = global.options
global.currentCategory = this
}
)
p = document.createElement('div')
p.className = 'loadingIndicator'

2
src/OpenStreetBrowserLoader.js

@ -58,6 +58,7 @@ OpenStreetBrowserLoader.prototype.getCategory = function (id, options, callback)
this.getCategoryFromData(ids.id, opt, repoData.categories[ids.entityId], function (err, category) {
if (category) {
category.setMap(this.map)
category.lang = repoData.lang
}
callback(err, category)
@ -112,6 +113,7 @@ OpenStreetBrowserLoader.prototype.getRepo = function (repo, options, callback) {
if (repo) {
param.push('repo=' + encodeURIComponent(repo))
}
param.push('lang=' + encodeURIComponent(ui_lang))
param.push(config.categoriesRev)
param = param.length ? '?' + param.join('&') : ''

39
src/RepositoryBase.php

@ -23,16 +23,53 @@ class RepositoryBase {
return $ret;
}
function data () {
function data ($options) {
$data = array(
'categories' => array(),
'templates' => array(),
'timestamp' => Date(DATE_ISO8601, $this->timestamp()),
'lang' => array(),
);
return $data;
}
function updateLang (&$data, $options) {
$lang = array_key_exists('lang', $options) ? $options['lang'] : 'en';
if (!is_array($data['lang'])) {
$data['lang'] = array();
}
foreach ($data['categories'] as $id => $category) {
$name = null;
if (array_key_exists("category:{$id}", $data['lang'])) {
$name = $data['lang']["category:{$id}"];
if ($name !== '' && $name !== null) {
$data['categories'][$id]['name'] = array(
$lang => $data['lang']["category:{$id}"],
);
}
}
elseif (array_key_exists('name', $category)) {
if (array_key_exists($lang, $category['name'])) {
$name = $category['name'][$lang];
}
elseif (array_key_exists('en', $category['name'])) {
$name = $category['name']['en'];
}
elseif (sizeof($category['name'])) {
$name = $category['name'][array_keys($category['name'])[0]];
}
$data['lang']["category:{$id}"] = $name;
$data['categories'][$id]['name'] = array($lang => $name);
}
}
}
function isCategory ($data) {
if (!array_key_exists('type', $data)) {
return false;

16
src/RepositoryDir.php

@ -14,8 +14,20 @@ class RepositoryDir extends RepositoryBase {
return $ts;
}
function data () {
$data = parent::data();
function data ($options) {
$data = parent::data($options);
$lang = array_key_exists('lang', $options) ? $options['lang'] : 'en';
if (file_exists("{$this->path}/lang/{$lang}.json")) {
$data['lang'] = json_decode(file_get_contents("{$this->path}/lang/en.json"), true);
$lang = json_decode(file_get_contents("{$this->path}/lang/{$options['lang']}.json"), true);
foreach ($lang as $k => $v) {
if ($v !== null && $v !== '') {
$data['lang'][$k] = $v;
}
}
}
$d = opendir($this->path);
while ($f = readdir($d)) {

16
src/RepositoryGit.php

@ -29,8 +29,20 @@ class RepositoryGit extends RepositoryBase {
return $ts;
}
function data () {
$data = parent::data();
function data ($options) {
$data = parent::data($options);
$lang = array_key_exists('lang', $options) ? $options['lang'] : 'en';
if (true) {
$data['lang'] = json_decode(shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:lang/en.json 2>/dev/null"), true);
$lang = json_decode(shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:lang/" . escapeShellArg("{$options['lang']}.json") . " 2>/dev/null"), true);
foreach ($lang as $k => $v) {
if ($v !== null && $v !== '') {
$data['lang'][$k] = $v;
}
}
}
$d = popen("cd " . escapeShellArg($this->path) . "; git ls-tree {$this->branchEsc}", "r");
while ($r = fgets($d)) {

4
src/tagTranslations.js

@ -25,6 +25,10 @@ OverpassLayer.twig.extendFunction('trans', function () {
OverpassLayer.twig.extendFunction('isTranslated', function (str) {
return tagTranslationsIsTranslated(str)
})
OverpassLayer.twig.extendFunction('repoTrans', function (str) {
let lang = global.currentCategory.lang
return str in lang ? lang[str] : str
})
function tagTranslationsIsTranslated (str) {
return !(str in lang_non_translated) && (str in lang_str)

Loading…
Cancel
Save