Skip to content

Commit

Permalink
Added frontend internationalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensElflein committed Apr 8, 2020
1 parent c68df41 commit 06bc3c0
Show file tree
Hide file tree
Showing 23 changed files with 178 additions and 26 deletions.
2 changes: 2 additions & 0 deletions api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ message CameraSettings {
}

message BoothSettings {
required ListSetting language_choice = 5;

required BoolSetting storage_enabled = 10;
required BoolSetting printer_enabled = 11;

Expand Down
4 changes: 3 additions & 1 deletion assets/agreement.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
DIE SOFTWARE WIRD OHNE JEDE AUSDRÜCKLICHE ODER IMPLIZIERTE GARANTIE BEREITGESTELLT, EINSCHLIESSLICH DER GARANTIE ZUR BENUTZUNG FÜR DEN VORGESEHENEN ODER EINEM BESTIMMTEN ZWECK SOWIE JEGLICHER RECHTSVERLETZUNG, JEDOCH NICHT DARAUF BESCHRÄNKT. IN KEINEM FALL SIND DIE AUTOREN ODER COPYRIGHTINHABER FÜR JEGLICHEN SCHADEN ODER SONSTIGE ANSPRÜCHE HAFTBAR ZU MACHEN, OB INFOLGE DER ERFÜLLUNG EINES VERTRAGES, EINES DELIKTES ODER ANDERS IM ZUSAMMENHANG MIT DER SOFTWARE ODER SONSTIGER VERWENDUNG DER SOFTWARE ENTSTANDEN.
THE SELF-O-MAT OPEN SOURCE SOFTWARE IS LICENSED UNDER THE MIT SOFTWARE LICENSE:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"cameraName": "Kamera-Modell"
},
"booth": {
"languageChoice": "Anzeigesprache",
"storageEnabled": "USB-Speicher aktivieren?",
"printerEnabled": "Drucker aktivieren?",
"filterEnabled": "Filter-Modus",
Expand Down
13 changes: 13 additions & 0 deletions i18n/de_frontend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"frontend": {
"adjusting_focus": "Fokus wird gesucht",
"template_saved": "Template wurde gespeichert",
"print_in_progress": "Foto wird gedruckt...",
"printer_error": "Drucker wurde gestoppt",
"check_camera": "Prüfe deine Kamera",
"no_storage_found": "Kein Speichermedium gefunden",
"storage_space_low": "Geringe Speicherkapazität: ",
"storage_error": "Fehler beim Speichern des Fotos",
"cancel_print": "Druck abbrechen?"
}
}
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"cameraName": "Camera Name"
},
"booth": {
"languageChoice": "Display Language",
"storageEnabled": "USB Storage Enabled?",
"printerEnabled": "Printer Enabled?",
"filterEnabled": "Filter Mode",
Expand Down
13 changes: 13 additions & 0 deletions i18n/en_frontend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"frontend": {
"adjusting_focus": "Adjusting Focus..",
"template_saved": "Template saved successfully",
"print_in_progress": "Printing in Progress...",
"printer_error": "Printer Error!",
"check_camera": "Please check your camera",
"no_storage_found": "No USB Storage found",
"storage_space_low": "Low Storage: ",
"storage_error": "Error writing to USB storage.",
"cancel_print": "Cancel Printing?"
}
}
3 changes: 2 additions & 1 deletion settings/box_kit_v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"button_port_name": "/dev/ttyS0",
"image_dir": "/media/usb",
"show_led_setup": false,
"force_image_dir_mountpoint": 1
"force_image_dir_mountpoint": 1,
"fullscreen": true
}
3 changes: 2 additions & 1 deletion settings/box_kit_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"button_port_name": "/dev/ttyS0",
"image_dir": "/media/usb",
"show_led_setup": false,
"force_image_dir_mountpoint": 1
"force_image_dir_mountpoint": 1,
"fullscreen": true
}
3 changes: 2 additions & 1 deletion settings/box_kit_v2_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"button_port_name": "/dev/ttyS0",
"image_dir": "/media/usb",
"show_led_setup": false,
"force_image_dir_mountpoint": 1
"force_image_dir_mountpoint": 1,
"fullscreen": true
}
3 changes: 2 additions & 1 deletion settings/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"button_port_name": "/dev/ttyUSB",
"image_dir": "/tmp",
"show_led_setup": true,
"force_image_dir_mountpoint": 0
"force_image_dir_mountpoint": 0,
"fullscreen": false
}
3 changes: 2 additions & 1 deletion settings/hardware_kit_v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"button_port_name": "/dev/ttyS0",
"image_dir": "/media/usb",
"show_led_setup": true,
"force_image_dir_mountpoint": 1
"force_image_dir_mountpoint": 1,
"fullscreen": true
}
7 changes: 4 additions & 3 deletions settings/nop.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"camera_type": "nop",
"has_flash": true,
"has_button": true,
"has_flash": false,
"has_button": false,
"disable_watchdog": true,
"debug": false,
"button_port_name": "/dev/ttyS0",
"image_dir": "/tmp",
"show_led_setup": false,
"force_image_dir_mountpoint": 0
"force_image_dir_mountpoint": 0,
"fullscreen": false
}
25 changes: 25 additions & 0 deletions src/api/BoothApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,19 @@ bool BoothApi::start() {
});


mux.handle("/booth_settings/language/which")
.post([this](served::response &res, const served::request &req) {
IntUpdate update;
if (!update.ParseFromString(req.body())) {
served::response::stock_reply(400, res);
return;
}

logic->setLanguageChoice(update.value(), true);

served::response::stock_reply(200, res);
});

mux.handle("/booth_settings/filter/which")
.post([this](served::response &res, const served::request &req) {
IntUpdate update;
Expand Down Expand Up @@ -596,7 +609,19 @@ bool BoothApi::start() {
boost::property_tree::read_json("./i18n/en.json", locale);
}



auto controller = logic->getSelfomatController();

{
auto setting = currentBoothSettings.mutable_language_choice();
setting->set_name(locale.get<string>("api.booth.languageChoice"));
setting->set_update_url("/booth_settings/language/which");
setting->set_currentindex(logic->getLanguageChoice());
for(auto &choice : *logic->getLanguageChoices())
setting->add_values(choice);
}

{
auto setting = currentBoothSettings.mutable_storage_enabled();
setting->set_name(locale.get<string>("api.booth.storageEnabled"));
Expand Down
51 changes: 43 additions & 8 deletions src/logic/BoothLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void BoothLogic::cameraThread() {

if (printerEnabled && !printCanceled &&
printerManager.getCurrentPrinterState() != STATE_STOPPED) {
gui->addAlert(ALERT_PRINTER_HINT, L"Foto wird gedruckt...", true, true);
gui->addAlert(ALERT_PRINTER_HINT, getTranslation("frontend.print_in_progress"), true, true);
}
} else {
LOG_E(TAG, "Got an error while waiting for image");
Expand Down Expand Up @@ -280,25 +280,25 @@ void BoothLogic::logicThread() {
// check the printer state
printerManager.refreshPrinterState();
if (printerManager.getCurrentPrinterState() == STATE_STOPPED && printerEnabled) {
gui->addAlert(ALERT_PRINTER, L"Drucker wurde gestoppt");
gui->addAlert(ALERT_PRINTER, getTranslation("frontend.printer_error"));
} else {
gui->removeAlert(ALERT_PRINTER);
}


// check the camera state
if (camera->getState() != STATE_WORKING) {
gui->addAlert(ALERT_CAMERA, L"Prüfe deine Kamera");
gui->addAlert(ALERT_CAMERA, getTranslation("frontend.check_camera"));
} else {
gui->removeAlert(ALERT_CAMERA);
}

int freeStorage = getFreeStorageSpaceMB();
if (freeStorage < 500) {
if (freeStorage == -1)
gui->addAlert(ALERT_STORAGE, L"Kein Speichermedium gefunden");
gui->addAlert(ALERT_STORAGE, getTranslation("frontend.no_storage_found"));
else
gui->addAlert(ALERT_STORAGE, L"Geringe Speicherkapazität: " + to_wstring(freeStorage) + L"MB");
gui->addAlert(ALERT_STORAGE, getTranslation("frontend.storage_space_low") + to_wstring(freeStorage) + L"MB");
} else {
gui->removeAlert(ALERT_STORAGE);
}
Expand Down Expand Up @@ -471,7 +471,7 @@ bool BoothLogic::isMountpoint(std::string folder) {
bool BoothLogic::saveImage(void *data, size_t size, std::string filename, bool showAlert) {
auto success = saveImage(data, size, filename);
if (!success && showAlert) {
gui->addAlert(ALERT_STORAGE_ERROR, L"Fehler beim Speichern des Fotos", true);
gui->addAlert(ALERT_STORAGE_ERROR, getTranslation("frontend.storage_error"), true);
}

return success;
Expand Down Expand Up @@ -581,6 +581,8 @@ void BoothLogic::readSettings() {
setFilterGain(ptree.get<double>("filter_gain", 1.0));
setDebugLogEnabled(ptree.get<bool>("debug_log_enabled", false));
setAutofocusBeforeTrigger(ptree.get<bool>("autofocus_before_trigger", false));
setLanguageChoice(ptree.get<int>("frontend_language_choice", 0));


if (!success)
writeSettings();
Expand All @@ -598,6 +600,7 @@ void BoothLogic::writeSettings() {
ptree.put("filter_choice", filterChoice);
ptree.put("debug_log_enabled", getDebugLogEnabled());
ptree.put("autofocus_before_trigger", this->autofocus_before_trigger);
ptree.put("frontend_language_choice", this->languageChoice);

try {
boost::property_tree::write_json(std::string(getenv("HOME")) + "/.selfomat_settings.json", ptree);
Expand Down Expand Up @@ -640,7 +643,9 @@ bool BoothLogic::getTemplateLoaded() {

void BoothLogic::adjustFocus() {
camera->autofocusBlocking();
gui->addAlert(ALERT_CAMERA_HINT, L"Fokus wird gesucht", true, true);


gui->addAlert(ALERT_CAMERA_HINT, getTranslation("frontend.adjusting_focus"), true, true);
}

SelfomatController *BoothLogic::getSelfomatController() {
Expand All @@ -661,7 +666,7 @@ bool BoothLogic::updateTemplate(void *data, size_t size) {
bool result = imageProcessor.updateTemplate(data, size);
gui->reloadTemplate();
if (result) {
gui->addAlert(ALERT_TEMPLATE, L"Template wurde gespeichert", true, true);
gui->addAlert(ALERT_TEMPLATE, getTranslation("frontend.template_saved"), true, true);
}
return result;
}
Expand All @@ -681,6 +686,32 @@ void BoothLogic::setFilterChoice(int choice, bool persist) {
}
}

const std::vector<std::string> *BoothLogic::getLanguageChoices() {
return &languageNames;
}

int BoothLogic::getLanguageChoice() {
return languageChoice;
}

void BoothLogic::setLanguageChoice(int choice, bool persist) {
if(choice < 0 || choice >= languageIDs.size()) {
choice = 0;
}
languageChoice = choice;

// load the language into the property tree
try {
boost::property_tree::read_json("./i18n/" + languageIDs[choice] + "_frontend.json", locale);
} catch (boost::exception &e) {
boost::property_tree::read_json("./i18n/en_frontend.json", locale);
}

if (persist) {
writeSettings();
}
}

double BoothLogic::getFilterGain() {
return filterGain;
}
Expand Down Expand Up @@ -745,3 +776,7 @@ void BoothLogic::setAutofocusBeforeTrigger(bool newValue, bool persist) {
if (persist)
writeSettings();
}

std::wstring BoothLogic::getTranslation(std::string id) {
return converter.from_bytes(locale.get<string>(id));
}
14 changes: 14 additions & 0 deletions src/logic/BoothLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ namespace selfomat {


private:

// TODO: maybe migrate from std::wstring to std::string or convert on load
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const std::vector<std::string> languageNames { "English (EN)", "German (DE)" };
const std::vector<std::string> languageIDs { "en", "de" };
int languageChoice = 0;
boost::property_tree::ptree locale;


static std::string TAG;
SelfomatController selfomatController;
bool show_led_setup;
Expand Down Expand Up @@ -237,6 +246,11 @@ namespace selfomat {
void setAutofocusBeforeTrigger(bool newValue, bool persist = false);
bool getAutofocusBeforeTrigger();

const std::vector<std::string> * getLanguageChoices();
int getLanguageChoice();
void setLanguageChoice(int choice, bool persist = false);

std::wstring getTranslation(std::string id);
};

}
Expand Down
3 changes: 3 additions & 0 deletions src/logic/ILogicController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef SELF_O_MAT_ILOGICCONTROLLER_H
#define SELF_O_MAT_ILOGICCONTROLLER_H
#include <string>


namespace selfomat {
namespace logic {
Expand All @@ -9,6 +11,7 @@ namespace selfomat {
virtual void acceptAgreement() = 0;
virtual void cancelPrint() = 0;
virtual void stop() = 0;
virtual std::wstring getTranslation(std::string id) = 0;
};
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ int main(int argc, char *argv[]) {
bool show_led_setup = false;
bool force_image_dir_mountpoint = true;
bool autofocus_before_trigger = false;
bool fullscreen = true;
string button_port_name;
string image_dir;

Expand All @@ -146,6 +147,7 @@ int main(int argc, char *argv[]) {
show_led_setup = ptree.get<bool>("show_led_setup");
force_image_dir_mountpoint = ptree.get<bool>("force_image_dir_mountpoint", true);
autofocus_before_trigger = ptree.get<bool>("autofocus_before_trigger", false);
fullscreen = ptree.get<bool>("fullscreen", true);
} catch (boost::exception &e) {
LOG_E(TAG, "Error loading properties. Using defaults.");
}
Expand All @@ -156,10 +158,11 @@ int main(int argc, char *argv[]) {
LOG_D(TAG, "Has Flash: ", std::to_string(has_flash));
LOG_D(TAG, "Force Imagedir mountpoint: ", std::to_string(force_image_dir_mountpoint));
LOG_D(TAG, "Autofocus Before Trigger: ", std::to_string(autofocus_before_trigger));
LOG_D(TAG, "Fullscreen: ", std::to_string(fullscreen));


// We'll set the controller later when logic is initialized
BoothGui *boothGuiPtr = new BoothGui(debug, nullptr);
BoothGui *boothGuiPtr = new BoothGui(fullscreen, debug, nullptr);
p_gui = boothGuiPtr;
if (!p_gui->start()) {
LOG_E(TAG, "Error starting gui - Exiting.");
Expand Down
Loading

0 comments on commit 06bc3c0

Please sign in to comment.