diff options
| author | winter | 2024-12-24 17:46:51 +0000 |
|---|---|---|
| committer | winter | 2024-12-24 17:46:51 +0000 |
| commit | 721674973ca0a4d98cd081e9ff130c3356ddac03 (patch) | |
| tree | 5caa082baa4f23bd8a88f4f268fea6d9233f6553 | |
| parent | d5a57276eb27e215dd0b1bd5eb67ac26acc9575b (diff) | |
implement ui translations using gettext
this was horrible i don't like gettext anymore
| -rwxr-xr-x | bin/extract_strings | 3 | ||||
| -rw-r--r-- | index.php | 9 | ||||
| -rw-r--r-- | locale/digitigrade.pot | 62 | ||||
| -rw-r--r-- | locale/en_GB/LC_MESSAGES/digitigrade.mo | bin | 0 -> 1270 bytes | |||
| -rw-r--r-- | locale/en_GB/LC_MESSAGES/digitigrade.po | 77 | ||||
| -rw-r--r-- | locale/fr_FR/LC_MESSAGES/digitigrade.mo | bin | 0 -> 839 bytes | |||
| -rw-r--r-- | locale/fr_FR/LC_MESSAGES/digitigrade.po | 75 | ||||
| -rw-r--r-- | misc/get_ui_language.php | 29 | ||||
| -rw-r--r-- | templates/actor_profile.php | 8 | ||||
| -rw-r--r-- | templates/actor_profile_page.php | 2 | ||||
| -rw-r--r-- | templates/global_timeline.php | 4 | ||||
| -rw-r--r-- | templates/interaction_button.php | 6 | ||||
| -rw-r--r-- | templates/local_timeline.php | 4 | ||||
| -rw-r--r-- | templates/note.php | 2 | ||||
| -rw-r--r-- | templates/placeholder_text.php | 2 | ||||
| -rw-r--r-- | templates/skeleton.php | 6 |
16 files changed, 272 insertions, 17 deletions
diff --git a/bin/extract_strings b/bin/extract_strings new file mode 100755 index 0000000..8893b0e --- /dev/null +++ b/bin/extract_strings @@ -0,0 +1,3 @@ +#!/bin/sh +cd "$(dirname "$0")"/.. || exit 1 +xgettext -L PHP **/*.php -d digitigrade --from-code=UTF-8 -o locale/digitigrade.pot --omit-header @@ -11,4 +11,13 @@ function require_all_glob(string $pattern) { require_all_glob('routes/*.php'); require_all_glob('misc/*.php'); +$lang = get_ui_language(); +header('Content-Language: ' . str_replace('_', '-', $lang)); +putenv("LANG=$lang"); +setlocale(LC_MESSAGES, $lang); + +bindtextdomain('digitigrade', __DIR__ . '/locale'); +bind_textdomain_codeset('digitigrade', 'UTF-8'); +textdomain('digitigrade'); + Digitigrade\Router::getInstance()->processRequest(); diff --git a/locale/digitigrade.pot b/locale/digitigrade.pot new file mode 100644 index 0000000..5fe4841 --- /dev/null +++ b/locale/digitigrade.pot @@ -0,0 +1,62 @@ +#: templates/actor_profile.php:7 +#, php-format +msgid "Joined on %s" +msgstr "" + +#: templates/actor_profile.php:12 +msgid "Open remote profile page" +msgstr "" + +#: templates/actor_profile.php:19 +msgid "This user hasn't written a bio yet." +msgstr "" + +#: templates/actor_profile_page.php:14 +msgid "This user hasn't posted anything yet." +msgstr "" + +#: templates/global_timeline.php:1 +msgid "Global timeline" +msgstr "" + +#: templates/global_timeline.php:5 +msgid "This timeline shows all known public indexable notes." +msgstr "" + +#: templates/interaction_button.php:4 +msgid "Like" +msgstr "" + +#: templates/interaction_button.php:5 +msgid "Dislike" +msgstr "" + +#: templates/interaction_button.php:6 +msgid "Reshare" +msgstr "" + +#: templates/local_timeline.php:1 +msgid "Local timeline" +msgstr "" + +#: templates/local_timeline.php:5 +msgid "" +"This timeline shows all public indexable notes posted by users on this " +"server." +msgstr "" + +#: templates/placeholder_text.php:1 +msgid "There's nothing here." +msgstr "" + +#: templates/skeleton.php:17 +msgid "Global" +msgstr "" + +#: templates/skeleton.php:18 +msgid "Local" +msgstr "" + +#: templates/skeleton.php:20 +msgid "Digitigrade" +msgstr "" diff --git a/locale/en_GB/LC_MESSAGES/digitigrade.mo b/locale/en_GB/LC_MESSAGES/digitigrade.mo Binary files differnew file mode 100644 index 0000000..13b2645 --- /dev/null +++ b/locale/en_GB/LC_MESSAGES/digitigrade.mo diff --git a/locale/en_GB/LC_MESSAGES/digitigrade.po b/locale/en_GB/LC_MESSAGES/digitigrade.po new file mode 100644 index 0000000..3c5d38a --- /dev/null +++ b/locale/en_GB/LC_MESSAGES/digitigrade.po @@ -0,0 +1,77 @@ +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: \n" +"PO-Revision-Date: \n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: en_GB\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 3.5\n" + +#: templates/actor_profile.php:7 +#, php-format +msgid "Joined on %s" +msgstr "Joined on %s" + +#: templates/actor_profile.php:12 +msgid "Open remote profile page" +msgstr "Open remote profile page" + +#: templates/actor_profile.php:19 +msgid "This user hasn't written a bio yet." +msgstr "This user hasn't written a bio yet." + +#: templates/actor_profile_page.php:14 +msgid "This user hasn't posted anything yet." +msgstr "This user hasn't posted anything yet." + +#: templates/global_timeline.php:1 +msgid "Global timeline" +msgstr "Global timeline" + +#: templates/global_timeline.php:5 +msgid "This timeline shows all known public indexable notes." +msgstr "This timeline shows all known public indexable notes." + +#: templates/interaction_button.php:4 +msgid "Like" +msgstr "Like" + +#: templates/interaction_button.php:5 +msgid "Dislike" +msgstr "Dislike" + +#: templates/interaction_button.php:6 +msgid "Reshare" +msgstr "Reshare" + +#: templates/local_timeline.php:1 +msgid "Local timeline" +msgstr "Local timeline" + +#: templates/local_timeline.php:5 +msgid "" +"This timeline shows all public indexable notes posted by users on this " +"server." +msgstr "" +"This timeline shows all public indexable notes posted by users on this " +"server." + +#: templates/placeholder_text.php:1 +msgid "There's nothing here." +msgstr "There's nothing here." + +#: templates/skeleton.php:17 +msgid "Global" +msgstr "Global" + +#: templates/skeleton.php:18 +msgid "Local" +msgstr "Local" + +#: templates/skeleton.php:20 +msgid "Digitigrade" +msgstr "Digitigrade" diff --git a/locale/fr_FR/LC_MESSAGES/digitigrade.mo b/locale/fr_FR/LC_MESSAGES/digitigrade.mo Binary files differnew file mode 100644 index 0000000..7af445c --- /dev/null +++ b/locale/fr_FR/LC_MESSAGES/digitigrade.mo diff --git a/locale/fr_FR/LC_MESSAGES/digitigrade.po b/locale/fr_FR/LC_MESSAGES/digitigrade.po new file mode 100644 index 0000000..778f6b0 --- /dev/null +++ b/locale/fr_FR/LC_MESSAGES/digitigrade.po @@ -0,0 +1,75 @@ +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: \n" +"PO-Revision-Date: \n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 3.5\n" + +#: templates/actor_profile.php:7 +#, php-format +msgid "Joined on %s" +msgstr "Inscrit le %s" + +#: templates/actor_profile.php:12 +msgid "Open remote profile page" +msgstr "" + +#: templates/actor_profile.php:19 +msgid "This user hasn't written a bio yet." +msgstr "Cet utilisateur n'a pas encore écrit de biographie." + +#: templates/actor_profile_page.php:14 +msgid "This user hasn't posted anything yet." +msgstr "Cet utilisateur n'a encore rien publié." + +#: templates/global_timeline.php:1 +msgid "Global timeline" +msgstr "Chronologie mondiale" + +#: templates/global_timeline.php:5 +msgid "This timeline shows all known public indexable notes." +msgstr "" + +#: templates/interaction_button.php:4 +msgid "Like" +msgstr "" + +#: templates/interaction_button.php:5 +msgid "Dislike" +msgstr "" + +#: templates/interaction_button.php:6 +msgid "Reshare" +msgstr "" + +#: templates/local_timeline.php:1 +msgid "Local timeline" +msgstr "Chronologie locale" + +#: templates/local_timeline.php:5 +msgid "" +"This timeline shows all public indexable notes posted by users on this " +"server." +msgstr "" + +#: templates/placeholder_text.php:1 +msgid "There's nothing here." +msgstr "Il n'y a rien ici." + +#: templates/skeleton.php:17 +msgid "Global" +msgstr "Mondiale" + +#: templates/skeleton.php:18 +msgid "Local" +msgstr "Locale" + +#: templates/skeleton.php:20 +msgid "Digitigrade" +msgstr "La Digitigrade 🇫🇷🇫🇷🇫🇷🇫🇷" diff --git a/misc/get_ui_language.php b/misc/get_ui_language.php new file mode 100644 index 0000000..b1305b3 --- /dev/null +++ b/misc/get_ui_language.php @@ -0,0 +1,29 @@ +<?php + +function get_ui_language(?array $possibleLanguages = null): string { + if ($possibleLanguages == null) { + $possibleLanguages = array_map(function ($path) { + $parts = explode('/', $path); + return $parts[count($parts) - 2]; + }, glob(__DIR__ . '/../locale/*/LC_MESSAGES')); + } + + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + return $possibleLanguages[0]; + } + + $accepts = array_map( + fn($spec) => str_replace('-', '_', explode(';', $spec)[0]), + explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) + ); + + foreach ($accepts as $alang) { + foreach ($possibleLanguages as $candidate) { + if (str_starts_with(strtolower($candidate), strtolower($alang))) { + return $candidate; + } + } + } + + return $possibleLanguages[0]; +}
\ No newline at end of file diff --git a/templates/actor_profile.php b/templates/actor_profile.php index f721fc8..c5b86c4 100644 --- a/templates/actor_profile.php +++ b/templates/actor_profile.php @@ -4,18 +4,18 @@ <div> <div class="displayName"><?= $actor->displayName ?></div> <div class="handle">@<?= $actor->getFullHandle() ?></div> - <div class="joinedDate">Joined on <?= $actor->created->format('Y-m-d') ?></div> + <div class="joinedDate"><?= sprintf(_('Joined on %s'), $actor->created->format('Y-m-d')) ?></div> </div> <div> <?php if (!$actor->isLocal): ?> - <a class="icon material-symbols-outlined" href="<?= $actor->homepage ?>" title="Open remote profile page" - target="_blank">open_in_new</a> + <a class="icon material-symbols-outlined" href="<?= $actor->homepage ?>" + title="<?= _('Open remote profile page') ?>" target="_blank">open_in_new</a> <?php endif; ?> </div> </div> <?php if (isset($actor->bio) && $actor->bio != '') { ?> <p class="bio"><?= $actor->bio ?></p> <?php } else { - call_template('placeholder_text', ['message' => "This user hasn't written a bio yet."]); + call_template('placeholder_text', ['message' => _("This user hasn't written a bio yet.")]); } ?> </div>
\ No newline at end of file diff --git a/templates/actor_profile_page.php b/templates/actor_profile_page.php index ffa94e3..bd60e2c 100644 --- a/templates/actor_profile_page.php +++ b/templates/actor_profile_page.php @@ -11,7 +11,7 @@ call_template('skeleton', [ echo '<hr>'; $notes = Note::findAllWithAuthor($actor, 50); if (count($notes) == 0) { - call_template('placeholder_text', ['message' => "This user hasn't posted anything yet."]); + call_template('placeholder_text', ['message' => _("This user hasn't posted anything yet.")]); } foreach ($notes as $n) { call_template('note', ['note' => $n]); diff --git a/templates/global_timeline.php b/templates/global_timeline.php index bf91df0..f8fca52 100644 --- a/templates/global_timeline.php +++ b/templates/global_timeline.php @@ -1,8 +1,8 @@ -<?php call_template('skeleton', ['pageTitle' => 'Global timeline'], function () { +<?php call_template('skeleton', ['pageTitle' => _('Global timeline')], function () { global $notes; call_template('alertbox', [ 'variety' => 'info', - 'message' => 'This timeline shows all known public indexable notes.' + 'message' => _('This timeline shows all known public indexable notes.') ]); foreach ($notes as $n) { call_template('note', ['note' => $n]); diff --git a/templates/interaction_button.php b/templates/interaction_button.php index ed257df..40fe392 100644 --- a/templates/interaction_button.php +++ b/templates/interaction_button.php @@ -1,9 +1,9 @@ <?php $active = $active ?? false; $tooltip = match ($action) { - 'like' => 'Like', - 'dislike' => 'Dislike', - 'reshare' => 'Reshare' + 'like' => _('Like'), + 'dislike' => _('Dislike'), + 'reshare' => _('Reshare') }; $icon = match ($action) { 'like' => 'favorite', diff --git a/templates/local_timeline.php b/templates/local_timeline.php index f93300a..ec894a9 100644 --- a/templates/local_timeline.php +++ b/templates/local_timeline.php @@ -1,8 +1,8 @@ -<?php call_template('skeleton', ['pageTitle' => 'Local timeline'], function () { +<?php call_template('skeleton', ['pageTitle' => _('Local timeline')], function () { global $notes; call_template('alertbox', [ 'variety' => 'info', - 'message' => 'This timeline shows all public indexable notes posted by users on this server.' + 'message' => _('This timeline shows all public indexable notes posted by users on this server.') ]); foreach ($notes as $n) { call_template('note', ['note' => $n]); diff --git a/templates/note.php b/templates/note.php index 8887afb..83770aa 100644 --- a/templates/note.php +++ b/templates/note.php @@ -7,7 +7,7 @@ <span class="authorName"><?= $note->author->displayName ?></span> <span class="authorHandle">@<?= $note->author->getFullHandle() ?></span> <a class="timestamp" href="/@/<?= $note->author->getFullHandle() ?>/note/<?= $note->id ?>"> - <?= + <?= // TODO: localise $note->created->format('Y-m-d \a\t H:i') . (isset($note->modified) && $note->modified != $note->created ? ' (edited ' . $note->modified->format('Y-m-d \a\t H:i') . ')' diff --git a/templates/placeholder_text.php b/templates/placeholder_text.php index b27e490..1f03ebc 100644 --- a/templates/placeholder_text.php +++ b/templates/placeholder_text.php @@ -1 +1 @@ -<p class="placeholder"><?= $message ?? "There's nothing here." ?></p>
\ No newline at end of file +<p class="placeholder"><?= $message ?? _("There's nothing here.") ?></p>
\ No newline at end of file diff --git a/templates/skeleton.php b/templates/skeleton.php index 9cc4636..49e4386 100644 --- a/templates/skeleton.php +++ b/templates/skeleton.php @@ -14,10 +14,10 @@ <header> <nav> <ul> - <li><a href="/feed/global">Global</a></li> - <li><a href="/feed/local">Local</a></li> + <li><a href="/feed/global"><?= _('Global') ?></a></li> + <li><a href="/feed/local"><?= _('Local') ?></a></li> </ul> - <span id="siteTitle">Digitigrade</span> + <span id="siteTitle"><?= _('Digitigrade') ?></span> </nav> </header> <main> |
