Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the KeyboardLayout and KeyboardVariant handling #787

Merged
merged 2 commits into from
Jul 26, 2024
Merged
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
3 changes: 2 additions & 1 deletion po/POTFILES
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
src/Application.vala
src/MainWindow.vala
src/Utils.vala
src/Helpers/KeyboardLayoutHelper.vala
src/Helpers/LocaleHelper.vala
src/Helpers/LogHelper.vala
src/Objects/Configuration.vala
src/Objects/KeyboardLayout.vala
src/Objects/KeyboardVariant.vala
src/Objects/Mount.vala
src/Views/AbstractInstallerView.vala
src/Views/CheckView.vala
Expand Down
98 changes: 0 additions & 98 deletions src/Helpers/KeyboardLayoutHelper.vala

This file was deleted.

45 changes: 34 additions & 11 deletions src/Helpers/LocaleHelper.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ namespace LocaleHelper {
}

private static Gee.HashMap<string, LangEntry?> lang_entries;
private static GLib.Mutex lang_mutex;

public static Gee.HashMap<string, LangEntry?> get_lang_entries () {
if (lang_entries == null) {
lang_mutex = Mutex ();
lang_entries = new Gee.HashMap<string, LangEntry?> ();
var langs = Build.LANG_LIST.split (";");

Expand Down Expand Up @@ -112,26 +115,17 @@ namespace LocaleHelper {
}

// Now translate the labels in their original language.
var current_lang = Environment.get_variable ("LANGUAGE");
foreach (var lang_entry in lang_entries.values) {
var lang_code = lang_entry.get_code ();
Environment.set_variable ("LANGUAGE", lang_code, true);
lang_entry.name = dgettext ("iso_639_3", lang_entry.name);
lang_entry.name = lang_gettext (lang_entry.name, lang_code, "iso_639_3");
if (lang_entry.countries.length > 0) {
lang_entry.name = _("%s…").printf (lang_entry.name);
}

foreach (var country in lang_entry.countries) {
Environment.set_variable ("LANGUAGE", lang_code + "_" + country.alpha_2, true);
country.name = dgettext ("iso_3166", country.name);
country.name = lang_gettext (country.name, lang_code + "_" + country.alpha_2, "iso_3166");
}
}

if (current_lang != null) {
Environment.set_variable ("LANGUAGE", current_lang, true);
} else {
Environment.unset_variable ("LANGUAGE");
}
}

return lang_entries;
Expand Down Expand Up @@ -219,4 +213,33 @@ namespace LocaleHelper {

return null;
}

/*
* Always use this function to translate into another language to make sure
* that no race occurs when switchbing the environment variable.
*/
public unowned string lang_gettext (string source, string lang, string? domain = null) {
lang_mutex.lock ();
unowned string translation;
var current_lang = GLib.Environment.get_variable ("LANGUAGE");
GLib.Environment.set_variable ("LANGUAGE", lang, true);

if (domain == null) {
Intl.textdomain (Build.GETTEXT_PACKAGE);
translation = _(source);
} else {
translation = dgettext (domain, source);
}

if (current_lang != null) {
GLib.Environment.set_variable ("LANGUAGE", current_lang, true);
} else {
GLib.Environment.unset_variable ("LANGUAGE");
}

Intl.textdomain (Build.GETTEXT_PACKAGE);
lang_mutex.unlock ();

return translation;
}
}
Loading
Loading