Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.
fix(i18n): rename default names when switching between any two languages
Miguel Ribeirob3acdc0672ae
diff --git a/includes/default_names.php b/includes/default_names.phpindex 4387580..3f6bfcf 100644--- a/includes/default_names.php+++ b/includes/default_names.php@@ -201,28 +201,34 @@ function default_payment_methods($language)* column changes: subscriptions reference a category and a payment method by* id, never by name.*- * @param SQLite3 $db- * @param int $userId- * @param string $language+ * @param SQLite3 $db+ * @param int $userId+ * @param string $language+ * @param string[] $from The languages the account's default names may still be+ * in. English by default, because that is what the rows+ * of an installation and of a first account are seeded in.* @return int how many rows were renamed*/-function localize_default_names($db, $userId, $language)+function localize_default_names($db, $userId, $language, array $from = ['en']){+ // The language a name is already in has nothing to be renamed to.+ $from = array_values(array_diff(array_unique($from), [$language]));+$renamed = 0;$renamed += default_names_rename($db,- $db->prepare('UPDATE categories SET name = :name WHERE user_id = :user_id AND name = :english'),+ 'categories',$userId,- default_categories('en'),+ array_map('default_categories', $from),default_categories($language));$renamed += default_names_rename($db,- $db->prepare('UPDATE payment_methods SET name = :name WHERE user_id = :user_id AND name = :english'),+ 'payment_methods',$userId,- array_column(default_payment_methods('en'), 'name'),+ array_map(fn($source) => array_column(default_payment_methods($source), 'name'), $from),array_column(default_payment_methods($language), 'name'));@@ -241,11 +247,16 @@ function localize_default_names($db, $userId, $language)* that does not touch the language must not rename anything, and neither must* a save that "changes" it to the value it already had.*- * Only rows that still carry the English default are renamed, which is+ * Only rows that still carry a default are renamed, which is* localize_default_names()'s own promise: a name somebody chose is never* rewritten, and subscriptions reference a category or payment method by id,* so nothing but the name column moves.*+ * The default a row carries is the one of the language the account had until+ * now - German names after a switch to German, so a later switch to French has+ * to start from those - or English, which is what an account has whose names+ * were seeded before it ever chose a language.+ ** @param SQLite3 $db* @param int $userId* @param string $before The language the account had.@@ -258,42 +269,73 @@ function localize_default_names_on_language_change($db, $userId, $before, $afterreturn 0;}- return localize_default_names($db, $userId, $after);+ return localize_default_names($db, $userId, $after, [$before, 'en']);}/**- * Runs one prepared rename over a pair of equally ordered name lists.+ * Renames the rows of one table whose name is a default, into the language+ * given.*- * @param SQLite3 $db- * @param SQLite3Stmt|false $stmt- * @param int $userId- * @param string[] $english the name a still-default row carries- * @param string[] $localized what it should say instead+ * Every row to rename is found before any is written, and each is renamed by+ * id. Renaming name by name would let one rename feed the next: a name that+ * one language gives to the first default and another language to the second+ * would be renamed twice on the way through.+ *+ * A row that matches the defaults of more than one source language is+ * renamed from the first of them.+ *+ * @param SQLite3 $db+ * @param string $table "categories" or "payment_methods"+ * @param int $userId+ * @param string[][] $fromLists the names a still-default row carries, one+ * equally ordered list per source language+ * @param string[] $localized what it should say instead* @return int how many rows were renamed*/-function default_names_rename($db, $stmt, $userId, $english, $localized)+function default_names_rename($db, $table, $userId, array $fromLists, $localized){- if ($stmt === false) {+ $find = $db->prepare("SELECT id FROM $table WHERE user_id = :user_id AND name = :name");+ $rename = $db->prepare("UPDATE $table SET name = :name WHERE id = :id AND user_id = :user_id");++ if ($find === false || $rename === false) {return 0;}- $renamed = 0;+ $moves = [];++ foreach ($fromLists as $names) {+ foreach ($names as $index => $name) {+ // A brand, or a language that has no translation for this name yet.+ if (!isset($localized[$index]) || $localized[$index] === $name) {+ continue;+ }++ $find->bindValue(':name', $name, SQLITE3_TEXT);+ $find->bindValue(':user_id', $userId, SQLITE3_INTEGER);+ $result = $find->execute();++ while ($result !== false && ($row = $result->fetchArray(SQLITE3_ASSOC))) {+ if (!isset($moves[$row['id']])) {+ $moves[$row['id']] = $localized[$index];+ }+ }- foreach ($english as $index => $englishName) {- // A brand, or a language that has no translation for this name yet.- if ($localized[$index] === $englishName) {- continue;+ $find->reset();}+ }++ $renamed = 0;- $stmt->bindValue(':name', $localized[$index], SQLITE3_TEXT);- $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);- $stmt->bindValue(':english', $englishName, SQLITE3_TEXT);+ foreach ($moves as $id => $name) {+ $rename->bindValue(':name', $name, SQLITE3_TEXT);+ $rename->bindValue(':id', $id, SQLITE3_INTEGER);+ $rename->bindValue(':user_id', $userId, SQLITE3_INTEGER);- if ($stmt->execute() !== false) {+ if ($rename->execute() !== false) {$renamed += $db->changes();}- $stmt->reset();+ $rename->reset();}return $renamed;diff --git a/includes/version.php b/includes/version.phpindex 072d5cf..3af4c56 100644--- a/includes/version.php+++ b/includes/version.php@@ -1,3 +1,3 @@<?php-$version = "v5.9.0";+$version = "v5.10.0";?>diff --git a/tests/cases/default_names_test.php b/tests/cases/default_names_test.phpindex f5c5012..aed78fd 100644--- a/tests/cases/default_names_test.php+++ b/tests/cases/default_names_test.php@@ -428,3 +428,107 @@ wallos_test('the endpoint that saves a language is the one that applies it', funassert_contains('SELECT main_currency, language FROM user WHERE id = :userId', $source,'and it reads the language it had, in the query it already ran');});++wallos_test('switching from one non-English language to another renames the seeded names again', function () {+ $db = wallos_test_open_database();+ wallos_test_create_user($db, 1, 'alice');++ // She names one of the seeded rows herself, then walks English -> German -> French.+ $statement = $db->prepare('UPDATE categories SET name = :name WHERE user_id = 1 AND name = :english');+ $statement->bindValue(':name', 'Kids', SQLITE3_TEXT);+ $statement->bindValue(':english', 'Gaming', SQLITE3_TEXT);+ $statement->execute();++ localize_default_names_on_language_change($db, 1, 'en', 'de');+ $renamed = localize_default_names_on_language_change($db, 1, 'de', 'fr');++ assert_true($renamed > 0, 'the second switch renames what the first one wrote');++ $categories = default_names_test_rows($db, 'categories', 1);+ assert_true(in_array('Divertissement', $categories, true), 'a seeded category now reads in French');+ assert_true(!in_array('Unterhaltung', $categories, true), 'and none is left in German');+ assert_true(in_array('Kids', $categories, true), 'the name she chose is still hers');++ $methods = default_names_test_rows($db, 'payment_methods', 1);+ assert_true(in_array('Carte de crédit', $methods, true), 'the generic payment methods follow');+ assert_true(in_array('PayPal', $methods, true), 'and a brand stays a brand');++ $db->close();+});++wallos_test('switching back to English restores the English names', function () {+ $db = wallos_test_open_database();+ wallos_test_create_user($db, 1, 'alice');++ localize_default_names_on_language_change($db, 1, 'en', 'de');+ localize_default_names_on_language_change($db, 1, 'de', 'en');++ assert_same(default_categories('en'), default_names_test_rows($db, 'categories', 1),+ 'every category reads in English again');+ assert_same('Credit Card', default_names_test_rows($db, 'payment_methods', 1)[1],+ 'and so does the generic payment method');++ $db->close();+});++wallos_test('names that are still English are renamed even when the account was not in English', function () {+ $db = wallos_test_open_database();+ wallos_test_create_user($db, 1, 'alice');++ // The language on record says German, the rows were never localized: they+ // are the English ones the installation seeded. A switch to French still+ // finds them.+ $renamed = localize_default_names_on_language_change($db, 1, 'de', 'fr');++ assert_true($renamed > 0, 'the English names are found as well');+ assert_same(default_categories('fr'), default_names_test_rows($db, 'categories', 1),+ 'every category reads in French');++ $db->close();+});++wallos_test('a language switch leaves another account\'s names alone', function () {+ $db = wallos_test_open_database();+ wallos_test_create_user($db, 1, 'alice');+ wallos_test_create_user($db, 2, 'bob');++ foreach (['Entertainment', 'Music'] as $name) {+ $statement = $db->prepare('INSERT INTO categories (name, "order", user_id) VALUES (:name, 1, 2)');+ $statement->bindValue(':name', $name, SQLITE3_TEXT);+ $statement->execute();+ }++ localize_default_names_on_language_change($db, 1, 'en', 'de');+ localize_default_names_on_language_change($db, 1, 'de', 'fr');++ assert_same(['Entertainment', 'Music'], default_names_test_rows($db, 'categories', 2),+ 'the second account still has its own names');++ $db->close();+});++wallos_test('a rename never feeds the next one: each row is renamed once', function () {+ $db = wallos_test_open_database();+ wallos_test_create_user($db, 1, 'alice');++ // Rows carrying the German default for a position are all found before any+ // is written, so a target name that happens to be another position's+ // default is not renamed a second time.+ localize_default_names_on_language_change($db, 1, 'en', 'de');+ $renamed = localize_default_names($db, 1, 'fr', ['de']);++ $differs = fn($pair) => $pair[0] !== $pair[1];+ $categoriesThatDiffer = count(array_filter(array_map(null, default_categories('de'), default_categories('fr')), $differs));+ $methodsThatDiffer = count(array_filter(array_map(+ null,+ array_column(default_payment_methods('de'), 'name'),+ array_column(default_payment_methods('fr'), 'name')+ ), $differs));++ assert_same($categoriesThatDiffer + $methodsThatDiffer, $renamed,+ 'one write for every default that differs between the two languages');+ assert_same(default_categories('fr'), default_names_test_rows($db, 'categories', 1),+ 'and each category ends on its French name');++ $db->close();+});