Skip to content

Commit

Permalink
Add "Edit on Github" and "Translate to..." buttons
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Borsuk <[email protected]>
  • Loading branch information
biodranik committed Dec 12, 2022
1 parent 4f1139b commit 2608e2a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,82 +21,95 @@ paths = "safe"

[extra]
stripe = "https://donate.organicmaps.app/"
github_source = "https://github.com/organicmaps/organicmaps.github.io"

# Default English translation
[translations]
address = "Address"
back = "Back to News"
contact = "Contact Us"
edit_on_github = "Edit on GitHub"
install-appgallery = "Install Organic Maps from Huawei AppGallery"
install-appstore = "Install Organic Maps from the AppStore"
install-googleplay = "Install Organic Maps from Google Play"
install-fdroid="Install Organic Maps from F-Droid"
language = "English"
name = "Name"
token = "Token"
translate_to = "Translate to…"

# Please sort all other translation sections and values in alphabetical order.
[languages.de]
[languages.de.translations]
address = "Adresse"
back = "Zurück zu Neuigkeiten"
contact = "Kontakt"
edit_on_github = "Auf GitHub bearbeiten"
install-appgallery = "Organic Maps aus der Huawei AppGallery installieren"
install-appstore = "Installiere Organic Maps aus dem AppStore"
install-googleplay = "Organic Maps von Google Play installieren"
install-fdroid="Organic Maps von F-Droid installieren"
language = "Deutsch"
name = "Name"
token = "Token"
translate_to = "Übersetzen in…"

[languages.it]
[languages.it.translations]
address = "Indirizzo"
back = "Indietro"
contact = "Contattaci"
edit_on_github = "Modifica su GitHub"
install-appgallery = "Installa Organic Maps da Huawei AppGallery"
install-appstore = "Installa Organic Maps dall'AppStore"
install-fdroid="Installa Organic Maps da F-Droid"
install-googleplay = "Installa Organic Maps da Google Play"
language = "Italiano"
name = "Nome"
token = "Token"
translate_to = "Tradurre in…"

[languages.pl]
[languages.pl.translations]
address = "Adres"
back = "Z powrotem"
contact = "Kontakt"
edit_on_github = "Edytuj na GitHubie"
install-appgallery = "Zainstaluj Organic Maps z Huawei AppGallery"
install-appstore = "Zainstaluj Organic Maps z App Store"
install-fdroid="Zainstaluj Organic Maps z F-Droid"
install-googleplay = "Zainstaluj Organic Maps z Google Play"
name = "Nazwa"
language = "Polski"
token = "Token"
translate_to = "Przetłumacz na…"

[languages.ru]
[languages.ru.translations]
address = "Адрес"
back = "Назад"
contact = "Связаться с нами"
edit_on_github = "Редактировать на GitHub"
install-appgallery = "Установить Organic Maps из Huawei AppGallery"
install-appstore = "Установить Organic Maps из Apple AppStore"
install-fdroid="Установить Organic Maps из F-Droid"
install-googleplay = "Установить Organic Maps из Google Play"
language = "Русский"
name = "Название"
token = "Токен"
translate_to = "Перевести на…"

[languages.tr]
[languages.tr.translations]
address = "Adres"
back = "Haberlere geri dön"
contact = "Bize ulaşın"
edit_on_github = "GitHub'da Düzenle"
install-appgallery = "Organic Maps'i Huawei AppGallery'den İndir"
install-appstore = "Organic Maps'i AppStore'dan İndir"
install-fdroid="Organic Maps'i F-Droid'den İndir"
install-googleplay = "Organic Maps'i Google Play'den İndir"
language = "Türkçe"
name = "İsim"
token = "Token"
translate_to = "Tercüme et…"
5 changes: 5 additions & 0 deletions sass/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,8 @@ footer {
.back_to_news {
margin-top: 1rem;
}

.translate-menu {
padding-bottom: 1rem;
text-align: right;
}
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
</main>

<footer>
{% include 'translate_menu.html' %}
{% include 'bottom_menu.html' %}
<p>
<span>© 2022 <a href="mailto:[email protected]">Organic Maps OÜ</a>, reg. code 16225385</span>
Expand Down
92 changes: 92 additions & 0 deletions templates/translate_menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<script>
// Implement format if it's not implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
}

function onTranslateToSelected(el) {
// No language element.
if (!el.value) return;

console.log(el.value);

el.disabled = true;
document.body.style.cursor = 'wait';

var dir = el.getAttribute('data-dir');
var basename = encodeURIComponent(el.getAttribute('data-basename'));
var fromLanguage = document.documentElement.getAttribute('lang');
var toLanguage = el.value;

var sourceUrl = '{{ config.extra.github_source | safe }}/'.replace(
'https://github.com', 'https://raw.githubusercontent.com');
// Non-ASCII texts are longer and do not fit easily into Github limits, see below.
// if (fromLanguage !== 'en') {
// sourceUrl = '{0}master/content/{1}{2}.{3}.md'.format(sourceUrl, dir, basename, fromLanguage);
// } else {
sourceUrl = '{0}master/content/{1}{2}.md'.format(sourceUrl, dir, basename);
//}

var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
var markdownContent = encodeURIComponent(request.responseText);

// Generate URL to create a new page translation.
var filename = encodeURIComponent('{0}.{1}.md'.format(basename, toLanguage));
var lastDir = dir.split('/');
// Last path element can be empty due to a trailing slash.
lastDir = lastDir.pop() || lastDir.pop();
// Github has strange paths/params processing, fix a special case for root _index.md translations.
if (lastDir.length == 0) lastDir = 'content';
var newFileUrl = '{{ config.extra.github_source | safe }}/new/master/content/{0}?filename={1}/{2}&value={3}'
.format(dir.substr(0, dir.length - 1), lastDir, filename, markdownContent);

// Github limits max URL len to 8192 and shows error for too long urls.
if (newFileUrl.length > 8192) {
var cutTextStub = '\n\nPlease insert here the remaining text from ' + sourceUrl;
newFileUrl = newFileUrl.substring(0, 8191 - cutTextStub.length) + cutTextStub;
}
window.open(newFileUrl, '_blank');
} else {
// TODO: Display visible errors.
console.log('Error getting ' + sourceUrl);
}
// Restore cursor and element.
el.disabled = false;
el.selectedIndex = 0;
document.body.style.cursor = 'default';
}
};
request.open('GET', sourceUrl, true);
request.send(null);

console.log(sourceUrl);
}

// TODO: Show Translate to... option only for supported user's navigator languages
// var pageTranslations = {# resource.translations | map(attribute="lang") | json_encode() | safe #};
// var userPreferredLanguages = navigator.languages ? navigator.languages : [navigator.language];

</script>
<nav class="translate-menu">
<a href="{{ config.extra.github_source ~ '/edit/master/content/' ~ resource.relative_path | safe }}">{{ trans(key='edit_on_github', lang=lang) }}</a>
{% set pathArray = resource.relative_path | split(pat='/') %}
{% set contentDir = pathArray | slice(end=-1) | join(sep='/') %}
{% set baseFileName = pathArray | last | split(pat='.') | first %}
<select class="translate-to" onchange="onTranslateToSelected(this)" data-dir="{{ contentDir ~ '/' }}" data-basename="{{ baseFileName }}">
<option value="">{{ trans(key='translate_to', lang=lang) }}</option>
<!-- TODO: Dynamically generate the list of needed languages and their names using Intl api or hard-coded list:
See https://stackoverflow.com/a/69968496 -->
<option value="es">Español</option>
<option value="fr">Français</option>
<option value="pt">Português</option>
<option value="be">Беларуская</option>
</select>
</nav>

0 comments on commit 2608e2a

Please sign in to comment.