Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ dotnet build
dotnet test
```

### Settings page translations

Settings strings live in `TheIntroDB/Configuration/locales/`. Use `en.json` as
the reference for translation keys and keep placeholders such as `{count}`
unchanged. Name a new file after its language or locale (for example, `fr.json`
or `fr-CA.json`). Locale files are embedded automatically; missing translations
fall back to English.

Run `node --test tests/settings-localization.test.cjs` to check the UI
translations, and `dotnet test` to verify packaged locale resolution.


## Pull Request Guidelines

1. **Create a Branch**: Create a descriptive branch name for your changes (e.g., `feat/add-new-feature` or `fix/issue-description`).
Expand Down
33 changes: 33 additions & 0 deletions TheIntroDB.Tests/SettingsLocalizationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 TheIntroDB

using System.Collections.Generic;
using TheIntroDB.Api;
using Xunit;

namespace TheIntroDB.Tests;

public sealed class SettingsLocalizationTests
{
[Theory]
[InlineData("zh-CN", true)]
[InlineData("zh_CN", true)]
[InlineData("zh-Hans-CN", true)]
[InlineData("zh-SG", true)]
[InlineData("zh", true)]
[InlineData("zh-TW", false)]
[InlineData("en-US", false)]
[InlineData("fr-FR", false)]
[InlineData("", false)]
[InlineData(@"..\configPage", false)]
public void LoadsEmbeddedDictionaryForRequestedLocale(string locale, bool isSimplifiedChinese)
{
var service = new SettingsLocalizationService();
var messages = Assert.IsType<Dictionary<string, string>>(
service.Get(new SettingsLocalizationService.GetTranslations { Locale = locale }));

Assert.Equal(isSimplifiedChinese ? "\u4fdd\u5b58" : "Save", messages["save"]);
Assert.Contains("{count}", messages["clearedEntries"]);
Assert.True(messages.Count > 100);
}
}
68 changes: 68 additions & 0 deletions TheIntroDB/Api/SettingsLocalizationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 TheIntroDB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Services;

namespace TheIntroDB.Api
{
[Authenticated(Roles = "Admin")]
public sealed class SettingsLocalizationService : IService
{
private const string Prefix = "TheIntroDB.SettingsLocales.";
private const string Suffix = ".json";
private static readonly System.Reflection.Assembly Assembly = typeof(SettingsLocalizationService).Assembly;
private static readonly Dictionary<string, string> LocaleResources = Assembly.GetManifestResourceNames()
.Where(name => name.StartsWith(Prefix, StringComparison.Ordinal) && name.EndsWith(Suffix, StringComparison.Ordinal))
.ToDictionary(name => name.Substring(Prefix.Length, name.Length - Prefix.Length - Suffix.Length),
name => name, StringComparer.OrdinalIgnoreCase);

[Route("/TheIntroDB/Settings/Translations", "GET", Summary = "Get settings-page translations")]
public sealed class GetTranslations : IReturn<Dictionary<string, string>>
{
public string Locale { get; set; }
}

public object Get(GetTranslations request)
{
var messages = ReadLocale("en");
var locale = request.Locale?.Trim().Replace('_', '-') ?? string.Empty;
var separator = locale.IndexOf('-');
var language = separator < 0 ? locale : locale.Substring(0, separator);

// Neutral and Simplified Chinese variants use the existing zh-CN translation.
var simplifiedChinese = locale.Equals("zh", StringComparison.OrdinalIgnoreCase)
|| locale.Equals("zh-SG", StringComparison.OrdinalIgnoreCase)
|| locale.Equals("zh-Hans", StringComparison.OrdinalIgnoreCase)
|| locale.StartsWith("zh-Hans-", StringComparison.OrdinalIgnoreCase);
var selected = LocaleResources.ContainsKey(locale)
? locale
: simplifiedChinese ? "zh-CN" : language;
if (!selected.Equals("en", StringComparison.OrdinalIgnoreCase))
{
foreach (var entry in ReadLocale(selected))
{
messages[entry.Key] = entry.Value;
}
}

return messages;
}

private static Dictionary<string, string> ReadLocale(string locale)
{
if (!LocaleResources.TryGetValue(locale, out var resource))
{
return new Dictionary<string, string>();
}

using var stream = Assembly.GetManifestResourceStream(resource);
return JsonSerializer.Deserialize<Dictionary<string, string>>(stream)
?? new Dictionary<string, string>();
}
}
}
Loading