aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorwinter2024-12-31 14:30:16 +0000
committerwinter2024-12-31 14:30:16 +0000
commitad554b48e7dc355d9b33eede98f4e3e829fd867e (patch)
treeeb11cd4357bdbe2964b9d04495ab2e6c7212d9ed
parent721674973ca0a4d98cd081e9ff130c3356ddac03 (diff)
switch to my own translations system
i really don't like gettext
-rw-r--r--Digitigrade/Text.php49
-rwxr-xr-xbin/extract_strings3
-rw-r--r--index.php7
-rw-r--r--locale/digitigrade.pot62
-rw-r--r--locale/en_GB.json17
-rw-r--r--locale/en_GB/LC_MESSAGES/digitigrade.mobin1270 -> 0 bytes
-rw-r--r--locale/en_GB/LC_MESSAGES/digitigrade.po77
-rw-r--r--locale/fr_FR.json12
-rw-r--r--locale/fr_FR/LC_MESSAGES/digitigrade.mobin839 -> 0 bytes
-rw-r--r--locale/fr_FR/LC_MESSAGES/digitigrade.po75
-rw-r--r--misc/__.php7
-rw-r--r--misc/get_ui_language.php4
-rw-r--r--templates/actor_profile.php6
-rw-r--r--templates/actor_profile_page.php2
-rw-r--r--templates/global_timeline.php4
-rw-r--r--templates/interaction_button.php6
-rw-r--r--templates/local_timeline.php4
-rw-r--r--templates/placeholder_text.php2
-rw-r--r--templates/skeleton.php6
19 files changed, 103 insertions, 240 deletions
diff --git a/Digitigrade/Text.php b/Digitigrade/Text.php
new file mode 100644
index 0000000..952818b
--- /dev/null
+++ b/Digitigrade/Text.php
@@ -0,0 +1,49 @@
+<?php
+namespace Digitigrade;
+
+class Text {
+ private static string $defaultLanguage;
+ private static array $dictionary = [];
+ private string $translationKey;
+
+ public function __construct(string $translationKey) {
+ $this->translationKey = $translationKey;
+ }
+
+ public function __tostring(): string {
+ return $this->getInLanguage(self::$defaultLanguage);
+ }
+
+ public function getInLanguage(string $language): string {
+ $dict = self::getDict($language);
+ if ($dict == null) {
+ Logger::getInstance()->warning("tried to use language '$language', which does not exist");
+ return $this->translationKey;
+ }
+ if (isset($dict[$this->translationKey])) {
+ return $dict[$this->translationKey];
+ }
+ if (isset($dict['FALLBACK'])) {
+ return $this->getInLanguage($dict['FALLBACK']);
+ }
+ Logger::getInstance()->warning("looking up translation key '$this->translationKey' in language '$language' failed");
+ return $this->translationKey;
+ }
+
+ private static function getDict(string $language): ?array {
+ if (isset(self::$dictionary[$language])) {
+ return self::$dictionary[$language];
+ }
+ $filename = __DIR__ . "/../locale/$language.json";
+ if (!is_file($filename)) {
+ return null;
+ }
+ $dict = json_decode(file_get_contents($filename), associative: true);
+ self::$dictionary[$language] = $dict;
+ return $dict;
+ }
+
+ public static function setDefaultLanguage(string $language) {
+ self::$defaultLanguage = $language;
+ }
+} \ No newline at end of file
diff --git a/bin/extract_strings b/bin/extract_strings
deleted file mode 100755
index 8893b0e..0000000
--- a/bin/extract_strings
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-cd "$(dirname "$0")"/.. || exit 1
-xgettext -L PHP **/*.php -d digitigrade --from-code=UTF-8 -o locale/digitigrade.pot --omit-header
diff --git a/index.php b/index.php
index dafc999..fe0357d 100644
--- a/index.php
+++ b/index.php
@@ -13,11 +13,6 @@ 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\Text::setDefaultLanguage($lang);
Digitigrade\Router::getInstance()->processRequest();
diff --git a/locale/digitigrade.pot b/locale/digitigrade.pot
deleted file mode 100644
index 5fe4841..0000000
--- a/locale/digitigrade.pot
+++ /dev/null
@@ -1,62 +0,0 @@
-#: 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.json b/locale/en_GB.json
new file mode 100644
index 0000000..67a4ec5
--- /dev/null
+++ b/locale/en_GB.json
@@ -0,0 +1,17 @@
+{
+ "user.profile.createdAt": "Joined on %s",
+ "user.profile.openRemote": "Open remote profile page",
+ "user.profile.bio.placeholder": "This user hasn't written a bio yet.",
+ "user.notes.placeholder": "This user hasn't posted anything yet.",
+ "timeline.global": "Global timeline",
+ "timeline.global.description": "This timeline shows all known public indexable notes.",
+ "note.action.like": "Like",
+ "note.action.dislike": "Dislike",
+ "note.action.reshare": "Reshare",
+ "timeline.local": "Local timeline",
+ "timeline.local.description": "This timeline shows all public indexable notes posted by users on this server.",
+ "placeholder": "There's nothing here.",
+ "timeline.global.shortName": "Global",
+ "timeline.local.shortName": "Local",
+ "digitigrade": "Digitigrade"
+}
diff --git a/locale/en_GB/LC_MESSAGES/digitigrade.mo b/locale/en_GB/LC_MESSAGES/digitigrade.mo
deleted file mode 100644
index 13b2645..0000000
--- a/locale/en_GB/LC_MESSAGES/digitigrade.mo
+++ /dev/null
Binary files differ
diff --git a/locale/en_GB/LC_MESSAGES/digitigrade.po b/locale/en_GB/LC_MESSAGES/digitigrade.po
deleted file mode 100644
index 3c5d38a..0000000
--- a/locale/en_GB/LC_MESSAGES/digitigrade.po
+++ /dev/null
@@ -1,77 +0,0 @@
-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.json b/locale/fr_FR.json
new file mode 100644
index 0000000..bc09a4c
--- /dev/null
+++ b/locale/fr_FR.json
@@ -0,0 +1,12 @@
+{
+ "FALLBACK": "en_GB",
+ "user.profile.createdAt": "Inscrit le %s",
+ "user.profile.bio.placeholder": "Cet utilisateur n'a pas encore écrit de biographie.",
+ "user.notes.placeholder": "Cet utilisateur n'a encore rien publié.",
+ "timeline.global": "Chronologie mondiale",
+ "timeline.local": "Chronologie locale",
+ "placeholder": "Il n'y a rien ici.",
+ "timeline.global.shortName": "Mondiale",
+ "timeline.local.shortName": "Locale",
+ "digitigrade": "La Digitigrade 🇫🇷🇫🇷🇫🇷🇫🇷"
+}
diff --git a/locale/fr_FR/LC_MESSAGES/digitigrade.mo b/locale/fr_FR/LC_MESSAGES/digitigrade.mo
deleted file mode 100644
index 7af445c..0000000
--- a/locale/fr_FR/LC_MESSAGES/digitigrade.mo
+++ /dev/null
Binary files differ
diff --git a/locale/fr_FR/LC_MESSAGES/digitigrade.po b/locale/fr_FR/LC_MESSAGES/digitigrade.po
deleted file mode 100644
index 778f6b0..0000000
--- a/locale/fr_FR/LC_MESSAGES/digitigrade.po
+++ /dev/null
@@ -1,75 +0,0 @@
-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/__.php b/misc/__.php
new file mode 100644
index 0000000..af6366a
--- /dev/null
+++ b/misc/__.php
@@ -0,0 +1,7 @@
+<?php
+
+use Digitigrade\Text;
+
+function __(string $key): string {
+ return (new Text($key))->__tostring();
+} \ No newline at end of file
diff --git a/misc/get_ui_language.php b/misc/get_ui_language.php
index b1305b3..712b6c5 100644
--- a/misc/get_ui_language.php
+++ b/misc/get_ui_language.php
@@ -4,8 +4,8 @@ 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'));
+ return str_replace('.json', '', $parts[count($parts) - 1]);
+ }, glob(__DIR__ . '/../locale/*.json'));
}
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
diff --git a/templates/actor_profile.php b/templates/actor_profile.php
index c5b86c4..8a97bd8 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"><?= sprintf(_('Joined on %s'), $actor->created->format('Y-m-d')) ?></div>
+ <div class="joinedDate"><?= sprintf(__('user.profile.createdAt'), $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>
+ title="<?= __('user.profile.openRemote') ?>" 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' => __('user.profile.bio.placeholder')]);
} ?>
</div> \ No newline at end of file
diff --git a/templates/actor_profile_page.php b/templates/actor_profile_page.php
index bd60e2c..e7d28b7 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' => __('user.notes.placeholder')]);
}
foreach ($notes as $n) {
call_template('note', ['note' => $n]);
diff --git a/templates/global_timeline.php b/templates/global_timeline.php
index f8fca52..f475656 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' => __('timeline.global')], function () {
global $notes;
call_template('alertbox', [
'variety' => 'info',
- 'message' => _('This timeline shows all known public indexable notes.')
+ 'message' => __('timeline.global.description')
]);
foreach ($notes as $n) {
call_template('note', ['note' => $n]);
diff --git a/templates/interaction_button.php b/templates/interaction_button.php
index 40fe392..9115435 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' => __('note.action.like'),
+ 'dislike' => __('note.action.dislike'),
+ 'reshare' => __('note.action.reshare')
};
$icon = match ($action) {
'like' => 'favorite',
diff --git a/templates/local_timeline.php b/templates/local_timeline.php
index ec894a9..9d9027c 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' => __('timeline.local')], function () {
global $notes;
call_template('alertbox', [
'variety' => 'info',
- 'message' => _('This timeline shows all public indexable notes posted by users on this server.')
+ 'message' => __('timeline.local.description')
]);
foreach ($notes as $n) {
call_template('note', ['note' => $n]);
diff --git a/templates/placeholder_text.php b/templates/placeholder_text.php
index 1f03ebc..f529de9 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 ?? __('placeholder') ?></p> \ No newline at end of file
diff --git a/templates/skeleton.php b/templates/skeleton.php
index 49e4386..0d8afb4 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"><?= __('timeline.global.shortName') ?></a></li>
+ <li><a href="/feed/local"><?= __('timeline.local.shortName') ?></a></li>
</ul>
- <span id="siteTitle"><?= _('Digitigrade') ?></span>
+ <span id="siteTitle"><?= __('digitigrade') ?></span>
</nav>
</header>
<main>