Skip to content

Commit

Permalink
Translation update 2.0 (lighttube-org#171)
Browse files Browse the repository at this point in the history
* Add translation percentages to the language selector

* Display language names both in english and in their native language

* Add validation for the imported language files

* Add InnerTube language check to the language checks
  • Loading branch information
kuylar authored Sep 14, 2024
1 parent c1ea1b1 commit eff6730
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
5 changes: 4 additions & 1 deletion LightTube/Contexts/AppearanceSettingsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ public class AppearanceSettingsContext(
HttpContext context,
ApiLocals locals,
Dictionary<string, string> customThemes,
Language[] languages) : BaseContext(context)
Language[] languages,
Dictionary<string, int> languagePercentages) : BaseContext(context)
{
public Language[] Languages = languages;
public Dictionary<string, int> LanguagePercentages { get; set; } = languagePercentages;
public ApiLocals Locals = locals;
public Dictionary<string, string> CustomThemes = customThemes;
public Dictionary<string, string> BuiltinThemes = new()
Expand All @@ -19,4 +21,5 @@ public class AppearanceSettingsContext(
["light"] = "Light",
["dark"] = "Dark",
};

}
2 changes: 1 addition & 1 deletion LightTube/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SettingsController(SimpleInnerTubeClient innerTube) : Controller
public IActionResult Appearance()
{
ApiLocals locals = Utils.GetLocals();
AppearanceSettingsContext ctx = new(HttpContext, locals, Configuration.CustomThemeDefs, LocalizationManager.GetAllLanguages());
AppearanceSettingsContext ctx = new(HttpContext, locals, Configuration.CustomThemeDefs, LocalizationManager.GetAllLanguages(), LocalizationManager.GetLanguagePercentages());
return View(ctx);
}

Expand Down
2 changes: 2 additions & 0 deletions LightTube/Localization/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class Language
public string Name { get; set; }
public string EnglishName { get; set; }
public CultureInfo Culture { get; set; }

public string GetDisplayName() => Name == EnglishName ? Name : $"{Name} - {EnglishName}";
}
42 changes: 39 additions & 3 deletions LightTube/Localization/LocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class LocalizationManager(string locale)
private Dictionary<string, string> PreferredLocaleStrings { get; set; }
private Dictionary<string, string> FallbackLocaleStrings { get; set; }

private static readonly string[] RequiredKeys =
["language.code", "language.name", "language.name.english", "language.ietf", "language.innertube"];

public string CurrentLocale => locale;

public string GetRawString(string key, bool forceFallback = false)
Expand Down Expand Up @@ -59,15 +62,42 @@ public static void Init()
{
string code = file.Split('/')[2].Split('.')[0];
using FileStream s = File.Open(file, FileMode.Open, FileAccess.Read);
Localizations.Add(code, JsonSerializer.Deserialize<Dictionary<string, string>>(s) ?? []);
Dictionary<string, string>? parsed = JsonSerializer.Deserialize<Dictionary<string, string>>(s);
s.Close();
Log.Information("[Localization] Loaded localization file for {0} with {1} keys", code,
Localizations[code].Count);
AddLocalization(code, parsed ?? []);
}

Log.Information("[Localization] Localization files initialized.");
}

private static void AddLocalization(string code, Dictionary<string, string> parsed)
{
// remove all empty/whitespace keys
foreach ((string? key, _) in parsed.Where(x => string.IsNullOrWhiteSpace(x.Value)))
parsed.Remove(key);

// don't add languages without their information present
if (!RequiredKeys.All(parsed.ContainsKey))
{
Log.Warning(
"[Localization] Not loading localization {0}, since it doesn't contain the required fields",
code);
return;
}

if (!Utils.GetLocals().Languages.ContainsKey(parsed["language.innertube"]))
{
Log.Warning(
"[Localization] Not loading localization {0}, since it's InnerTube language equivalent isn't valid against any other language",
code);
return;
}

Localizations.Add(code, parsed);
Log.Information("[Localization] Loaded localization file for {0} with {1} keys", code,
Localizations[code].Count);
}

public static LocalizationManager GetFromHttpContext(HttpContext context)
{
string preferredLocale = "en";
Expand Down Expand Up @@ -120,4 +150,10 @@ public static Language[] GetAllLanguages()

return languages.OrderBy(x => x.Name).ToArray();
}

public static Dictionary<string, int> GetLanguagePercentages()
{
int defaultKeys = Localizations["en"].Count;
return Localizations.ToDictionary(x => x.Key, x => x.Value.Count * 100 / defaultKeys);
}
}
4 changes: 2 additions & 2 deletions LightTube/Views/Settings/Appearance.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
{
@if (Model.Localization.CurrentLocale == language.Code)
{
<option value="@language.Code" selected>@language.Name</option>
<option value="@language.Code" selected>@language.GetDisplayName() (@Model.LanguagePercentages[language.Code]%)</option>
}
else
{
<option value="@language.Code">@language.Name</option>
<option value="@language.Code">@language.GetDisplayName() (@Model.LanguagePercentages[language.Code]%)</option>
}
}
</select>
Expand Down

0 comments on commit eff6730

Please sign in to comment.