Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

fix(categories): recognise the no-category placeholder by language

Miguel Ribeiro1f27e7a7c47a
diff --git a/includes/default_names.php b/includes/default_names.php
index 6c2346f..d9ee016 100644
--- a/includes/default_names.php
+++ b/includes/default_names.php
@@ -3,19 +3,19 @@
The categories and payment methods a new account starts with, in the language
of that account.
- The lists were repeated verbatim in every place that creates an account —
+ The lists were repeated verbatim in every place that creates an account -
registration.php, endpoints/admin/adduser.php and
- includes/oidc/oidc_create_user.php — and every copy was English. Somebody who
+ includes/oidc/oidc_create_user.php - and every copy was English. Somebody who
registered in German got an application that was German everywhere except in
its own data, on the very first screen they saw.
The categories and the generic payment methods are held here as translation
keys and resolved once, at the moment the account is created. What is stored
is ordinary text the owner renames, reorders and deletes like any other row,
- so changing the account language later never rewrites it — the same contract
+ so changing the account language later never rewrites it - the same contract
the rows have always had.
- Brand names — PayPal, SEPA, Klarna — are the same word in every language and
+ Brand names - PayPal, SEPA, Klarna - are the same word in every language and
stay literal.
Currencies are deliberately not here. Their names are a separate list with a
@@ -55,7 +55,7 @@ const DEFAULT_CATEGORY_KEYS = [
/**
* The default payment methods, in display order.
*
- * A generic term — "Credit Card", "Bank Transfer", "Direct Debit", "Money" —
+ * A generic term - "Credit Card", "Bank Transfer", "Direct Debit", "Money" -
* reads differently to a German and to an English speaker, so it carries a
* translation "key". A brand is the same word in every language, so it keeps a
* literal "name".
@@ -193,8 +193,8 @@ function default_payment_methods($language)
* before a language does: createdatabase.php seeds the categories and payment
* methods while the database is being created, and migration 000020 gives them
* user_id 1, so registration.php never seeds them for that account. Its owner
- * picks a language on the registration form — the first moment anybody states
- * one — and gets English data anyway.
+ * picks a language on the registration form - the first moment anybody states
+ * one - and gets English data anyway.
*
* Only a row whose name is still exactly the English default is touched, so
* this cannot rewrite something somebody named themselves, and only the name
@@ -266,3 +266,33 @@ function default_names_rename($db, $stmt, $userId, $english, $localized)
return $renamed;
}
+
+/**
+ * The "No category" text a fresh account in the given language got as its
+ * untouched placeholder name.
+ *
+ * categories.id is one global sequence shared by every account, not one per
+ * user, so only the very first row this application ever created is actually
+ * id 1 - nothing here can tell "the placeholder row" apart from an ordinary
+ * category by id. Comparing against the literal English string had the same
+ * problem the other way once this file gave every language its own default:
+ * a category still carrying its untouched default name no longer answers
+ * "No category" for anybody who registered in a language other than English,
+ * so it stopped being recognised as the placeholder at all.
+ *
+ * What a display site actually wants to know is whether this row is still
+ * exactly what the account was seeded with, so it compares against that
+ * account's own creation-language default, not a literal English string, and
+ * re-translates it into whichever language is being viewed right now. A
+ * category the owner has renamed matches neither and is shown as stored,
+ * same as before this existed.
+ *
+ * @param string $language
+ * @return string
+ */
+function default_no_category_name($language)
+{
+ $translations = default_names_translations($language);
+
+ return $translations['no_category'] ?? 'No category';
+}
diff --git a/includes/filters_menu.php b/includes/filters_menu.php
index 0d4338a..a071878 100644
--- a/includes/filters_menu.php
+++ b/includes/filters_menu.php
@@ -1,3 +1,4 @@
+<?php require_once __DIR__ . '/default_names.php'; ?>
<div class="filtermenu-content">
<?php
if (count($members) > 1) {
@@ -35,11 +36,17 @@
<div class="filter-title" onClick="toggleSubMenu('category')"><?= translate("category", $i18n) ?></div>
<div class="filtermenu-submenu-content" id="filter-category">
<?php
+ // A category still carrying its untouched default name is
+ // recognised against the account's own creation-language default,
+ // not a literal English string - #1216 gives every language its
+ // own default, so an account created in German never had an
+ // English name to match here.
+ $noCategoryDefaultName = default_no_category_name($userData['language'] ?? 'en');
foreach ($categories as $category) {
if ($category['count'] == 0) {
continue;
}
- if ($category['name'] == "No category") {
+ if ($category['name'] === $noCategoryDefaultName) {
$category['name'] = translate("no_category", $i18n);
}
$selectedClass = '';
diff --git a/includes/stats_calculations.php b/includes/stats_calculations.php
index fc827c7..6981c6e 100644
--- a/includes/stats_calculations.php
+++ b/includes/stats_calculations.php
@@ -1,6 +1,7 @@
<?php
require_once __DIR__ . '/budget_period_calculations.php';
require_once __DIR__ . '/currency_rates.php';
+require_once __DIR__ . '/default_names.php';
function getPricePerMonth($cycle, $frequency, $price)
{
@@ -122,9 +123,14 @@ if (isset($_GET['member']) && $_GET['member'] !== '') {
if (isset($_GET['category']) && $_GET['category'] !== '') {
$categoryIds = array_map('intval', explode(',', $_GET['category']));
$conditions[] = "category_id IN (" . implode(',', $categoryIds) . ")";
+ // A category still carrying its untouched default name is recognised
+ // against the account's own creation-language default, not a literal
+ // English string - #1216 gives every language its own default, so an
+ // account created in German never had an English name to match here.
+ $noCategoryDefaultName = default_no_category_name($userData['language'] ?? 'en');
foreach ($categoryIds as $cid) {
if (isset($categories[$cid])) {
- $statsSubtitleParts[] = $categories[$cid]['name'] == "No category" ? translate("no_category", $i18n) : $categories[$cid]['name'];
+ $statsSubtitleParts[] = $categories[$cid]['name'] === $noCategoryDefaultName ? translate("no_category", $i18n) : $categories[$cid]['name'];
}
}
}
diff --git a/stats.php b/stats.php
index c81ad74..d8454e8 100644
--- a/stats.php
+++ b/stats.php
@@ -1,6 +1,7 @@
<?php
require_once 'includes/header.php';
require_once 'includes/logo_theme_variant.php';
+require_once 'includes/default_names.php';
// Get code of main currency to display on statistics
@@ -81,12 +82,18 @@ $potentialMonthlySavings = get_upcoming_cancellations_monthly_value($upcomingCan
<div class="filter-title" onClick="toggleSubMenu('category')"><?= translate("category", $i18n) ?></div>
<div class="filtermenu-submenu-content" id="filter-category">
<?php
+ // A category still carrying its untouched default name is
+ // recognised against the account's own creation-language
+ // default, not a literal English string - #1216 gives every
+ // language its own default, so an account created in German
+ // never had an English name to match here.
+ $noCategoryDefaultName = default_no_category_name($userData['language'] ?? 'en');
foreach ($categories as $category) {
$isSelected = isset($_GET['category']) && in_array($category['id'], array_map('intval', explode(',', $_GET['category'])));
if (($menuCategoryCounts[$category['id']] ?? 0) == 0 && !$isSelected) {
continue;
}
- $categoryName = $category['name'] == "No category" ? translate("no_category", $i18n) : $category['name'];
+ $categoryName = $category['name'] === $noCategoryDefaultName ? translate("no_category", $i18n) : $category['name'];
$selectedClass = $isSelected ? 'selected' : '';
?>
<div class="filter-item <?= $selectedClass ?>" data-categoryid="<?= $category['id'] ?>">
diff --git a/tests/cases/default_names_test.php b/tests/cases/default_names_test.php
index 23ddca6..ebe6708 100644
--- a/tests/cases/default_names_test.php
+++ b/tests/cases/default_names_test.php
@@ -4,7 +4,7 @@
Every path that creates an account carried its own copy of the same English
list, so somebody who registered in German got an application that was German
- everywhere except in its own data — on the dashboard, which is the first
+ everywhere except in its own data - on the dashboard, which is the first
screen they ever see. The lists are held as translation keys now and resolved
once, at the moment the account is created.
@@ -238,8 +238,8 @@ wallos_test('the first account is renamed into its language, rows and all', func
assert_same('Kreditkarte', $methods[1], 'the generic method is German');
assert_same('Überweisung', $methods[2], 'and so is the next one');
- // 16 categories — "Software" is the same word in both languages, so that
- // row is not written at all — and the 4 generic payment methods.
+ // 16 categories - "Software" is the same word in both languages, so that
+ // row is not written at all - and the 4 generic payment methods.
assert_same(20, $renamed, 'it reports what it renamed');
$db->close();
@@ -302,3 +302,79 @@ wallos_test('every key the seeding uses exists in English', function () {
}
}
});
+
+/*
+ A category still carrying its untouched default name is what a display site
+ recognises as "the no-category placeholder" - there is no other way to tell:
+ categories.id is one global sequence shared by every account, not one per
+ user, so only the very first row this application ever created is actually
+ id 1, and nothing here can tell the placeholder apart from an ordinary
+ category by id.
+
+ Before this file existed, that comparison was against the literal English
+ string "No category", which is exactly right when every account is seeded in
+ English. Once an account can be seeded in German, comparing the same literal
+ string stops recognising a German account's own untouched placeholder as the
+ placeholder at all - default_no_category_name() is what a display site
+ compares against instead: that account's own creation-language default,
+ rather than an English literal.
+*/
+
+wallos_test('the no-category default matches what a fresh account of that language is actually seeded with', function () {
+ // The property this function exists for: comparing $category['name'] to
+ // default_no_category_name($account's own language) is exactly comparing
+ // against what default_categories() put there in the first place.
+ foreach (['en', 'de', 'fr', 'ja'] as $language) {
+ assert_same(default_categories($language)[0], default_no_category_name($language),
+ $language . " matches that language's own seeded placeholder name");
+ }
+});
+
+wallos_test('the no-category default already differs by language, with no new translations needed', function () {
+ // "no_category" is not a key #1216 introduced - it already existed,
+ // already translated, in every language file, which is what makes this
+ // safe to use for every account regardless of when it registered.
+ assert_same('No category', default_no_category_name('en'), 'the English default');
+ assert_same('Keine Kategorie', default_no_category_name('de'), 'the German default');
+ assert_same('Pas de catégorie', default_no_category_name('fr'), 'the French default');
+ assert_same('カテゴリなし', default_no_category_name('ja'), 'the Japanese default');
+});
+
+wallos_test('an unrecognised language answers the English default, same as everything else here', function () {
+ foreach (['', 'xx', 'not a language', "../../../etc/passwd\0"] as $value) {
+ assert_same('No category', default_no_category_name($value),
+ var_export($value, true) . ' falls back to English');
+ }
+});
+
+wallos_test('a category renamed by its owner matches neither default, and is shown as stored', function () {
+ // The comparison this function feeds is an == against a fixed string:
+ // a category somebody has genuinely renamed - to anything, including
+ // another language's word for "no category" - is not the placeholder and
+ // must not be relabelled out from under them.
+ assert_true('Sin categoría' !== default_no_category_name('en'), 'not the English default');
+ assert_true('Sin categoría' !== default_no_category_name('de'), 'nor the German one');
+});
+
+wallos_test('every display site recognises the placeholder by language, not by an English literal', function () {
+ // The three places that used to compare $category['name'] == "No category":
+ // stats.php's filter menu, includes/filters_menu.php, and the stats
+ // subtitle in includes/stats_calculations.php. Each has to have moved to
+ // default_no_category_name() and have stopped comparing to the fixed
+ // English string, or a German account's own placeholder shows up as an
+ // uncategorised-looking category named "Keine Kategorie" instead of being
+ // recognised and re-translated to the page's own display language.
+ foreach ([
+ 'stats.php',
+ 'includes/filters_menu.php',
+ 'includes/stats_calculations.php',
+ ] as $path) {
+ $source = file_get_contents(WALLOS_ROOT . '/' . $path);
+
+ assert_contains('default_names.php', $source, $path . ' includes the default names helper');
+ assert_contains('default_no_category_name(', $source,
+ $path . ' compares against the account-language default');
+ assert_true(strpos($source, '== "No category"') === false,
+ $path . ' no longer compares against the English literal');
+ }
+});