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

Feat/add locale support #919

Open
wants to merge 2 commits into
base: qml
Choose a base branch
from
Open
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
71 changes: 53 additions & 18 deletions src/OptionsPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ Window {
columnSpacing: 10
rowSpacing: -5

Text {
text: qsTr("Locale:")
color: parent.enabled ? "black" : "grey"
}
ComboBox {
id: fieldLocale
editable: true
Layout.minimumWidth: 300
}

Text {
text: qsTr("Time zone:")
color: parent.enabled ? "black" : "grey"
Expand Down Expand Up @@ -453,6 +463,7 @@ Window {
chkEject.checked = imageWriter.getBoolSetting("eject")
var settings = imageWriter.getSavedCustomizationSettings()
fieldTimezone.model = imageWriter.getTimezoneList()
fieldLocale.model = imageWriter.getLocaleList()
fieldPublicKey.text = imageWriter.getDefaultPubKey()
fieldWifiCountry.model = imageWriter.getCountryList()
fieldKeyboardLayout.model = imageWriter.getKeymapLayoutList()
Expand Down Expand Up @@ -538,17 +549,17 @@ Window {
{
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find(imageWriter.getCurrentKeyboard())
}
else
{
/* Lacking an easy cross-platform to fetch keyboard layout
from host system, just default to "gb" for people in
UK time zone for now, and "us" for everyone else */
if (tz === "Europe/London") {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("gb")
} else {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("us")
}
else {
keyboardLayoutFromTimezone(tz)
}
}
if ('locale' in settings) {
fieldLocale.currentIndex = fieldLocale.find(settings.locale)
if (fieldLocale.currentIndex === -1) {
fieldLocale.editText = settings.locale
}
} else {
localeFromTimezone(tz)
}

if (imageWriter.isEmbeddedMode()) {
Expand All @@ -561,6 +572,29 @@ Window {
initialized = true
}

function keyboardLayoutFromTimezone(tz) {
/* Lacking an easy cross-platform to fetch keyboard layout
from host system, just default to "gb" for people in
UK time zone for now, fr for FR timezone, and "us" for everyone else */
if (tz === "Europe/London") {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("gb")
} else if (tz === "Europe/Paris") {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("fr")
} else {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("us")
}
}

function localeFromTimezone(tz) {
if (tz === "Europe/London") {
fieldLocale.currentIndex = fieldLocale.find("en_GB.UTF-8 (English - GB)")
} else if (tz === "Europe/Paris") {
fieldLocale.currentIndex = fieldLocale.find("fr_FR.UTF-8 (Français - France)")
} else {
fieldLocale.currentIndex = fieldLocale.find("en_US.UTF-8 (English - USA)")
}
}

function openPopup() {
if (!initialized) {
initialize()
Expand Down Expand Up @@ -786,10 +820,14 @@ Window {
addFirstRun(" dpkg-reconfigure -f noninteractive keyboard-configuration")
addFirstRun("fi")

var locale = fieldLocale.editText.split(" ")[0]
addFirstRun("localectl set-locale LANG=" + locale + " LC_ALL=" + locale +" LANGUAGE=" + locale)

addCloudInit("timezone: "+fieldTimezone.editText)
addCloudInit("keyboard:")
addCloudInit(" model: pc105")
addCloudInit(" layout: \"" + fieldKeyboardLayout.editText + "\"")
addCloudInit("locale: " + locale)
}

if (firstrun.length) {
Expand Down Expand Up @@ -843,6 +881,7 @@ Window {
settings.wifiPassword = cryptedPsk
}
if (chkLocale.checked) {
settings.locale = fieldLocale.editText
settings.timezone = fieldTimezone.editText
settings.keyboardLayout = fieldKeyboardLayout.editText
}
Expand All @@ -862,6 +901,7 @@ Window {
{
/* Bind copies of the lists */
fieldTimezone.model = imageWriter.getTimezoneList()
fieldLocale.model = imageWriter.getLocaleList()
fieldKeyboardLayout.model = imageWriter.getKeymapLayoutList()
fieldWifiCountry.model = imageWriter.getCountryList()

Expand All @@ -874,14 +914,9 @@ Window {

/* Timezone Settings*/
fieldTimezone.currentIndex = fieldTimezone.find(imageWriter.getTimezone())
/* Lacking an easy cross-platform to fetch keyboard layout
from host system, just default to "gb" for people in
UK time zone for now, and "us" for everyone else */
if (imageWriter.getTimezone() === "Europe/London") {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("gb")
} else {
fieldKeyboardLayout.currentIndex = fieldKeyboardLayout.find("us")
}

keyboardLayoutFromTimezone(imageWriter.getTimezone())
localeFromTimezone(imageWriter.getTimezone())

chkSetUser.checked = false
chkSSH.checked = false
Expand Down
17 changes: 16 additions & 1 deletion src/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QStandardPaths>
#include <QStorageInfo>
#include <QTimeZone>
#include <QLocale>
#include <QWindow>
#include <QGuiApplication>
#include <QNetworkInterface>
Expand Down Expand Up @@ -1158,6 +1159,20 @@ QStringList ImageWriter::getTimezoneList()
return timezones;
}

QStringList ImageWriter::getLocaleList()
{
QStringList locales;
QFile f(":/locales.txt");
if ( f.open(f.ReadOnly) )
{
locales = QString(f.readAll()).split('\n');
f.close();
}

return locales;
}


QStringList ImageWriter::getCountryList()
{
QStringList countries;
Expand Down Expand Up @@ -1235,7 +1250,7 @@ void ImageWriter::setImageCustomization(const QByteArray &config, const QByteArr

qDebug() << "Custom config.txt entries:" << config;
qDebug() << "Custom cmdline.txt entries:" << cmdline;
qDebug() << "Custom firstuse.sh:" << firstrun;
qDebug() << "Custom firstrun.sh:" << firstrun;
qDebug() << "Cloudinit:" << cloudinit;
}

Expand Down
1 change: 1 addition & 0 deletions src/imagewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class ImageWriter : public QObject
Q_INVOKABLE void generatePubKey();
Q_INVOKABLE QString getTimezone();
Q_INVOKABLE QStringList getTimezoneList();
Q_INVOKABLE QStringList getLocaleList();
Q_INVOKABLE QStringList getCountryList();
Q_INVOKABLE QStringList getKeymapLayoutList();
Q_INVOKABLE QString getSSID();
Expand Down
Loading