From e1ea63376c7e1045c6996d19de2852cb96deee78 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 24 Nov 2022 22:00:12 +0100 Subject: [PATCH 1/3] Add i18n to test/preferences To test the output of FLUID code and demonstrate i18n, preferences emulates GNU gettext. --- test/preferences.fl | 137 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 127 insertions(+), 10 deletions(-) diff --git a/test/preferences.fl b/test/preferences.fl index 89878f070f..6617aac65b 100644 --- a/test/preferences.fl +++ b/test/preferences.fl @@ -1,7 +1,32 @@ # data file for the Fltk User Interface Designer (fluid) version 1.0400 +i18n_type 1 +i18n_include {} +i18n_conditional {} +i18n_function _ +i18n_static_function N_ header_name {.h} code_name {.cxx} +comment {About test/preferences: + +The preferences app shows two features of FLTK and FLUID. + +The Fl_Preferences class is used as a storage for user +settings between app launches. Fl_Preferences can store +small amounts of arbitrary data in an .ini file format +which can be retrieved again at the next app launch. + +The FLUID setup uses GNU gettext for internationalisation +(i18n). FLUID finds the texts that need to be translated +and writes them into .po files that can be processed by +the GNU gettext tools. FLUID produces source code that +will translate all text into the current locale when +generating the UI. + + +In this small example, 'getttext' is only emulated.} {selected in_source not_in_header +} + decl {\#include } {public local } @@ -17,10 +42,13 @@ decl {\#include } {private local decl {\#include } {private local } -decl {void readPrefs();} {public local +decl {\#define _(text) gettext(text)} {private local } -decl {void writePrefs();} {public local +decl {int g_language = 0;} { + comment {Current languages are: + 0 = English + 1 = German} private global } decl {const char *project = "fltk.org";} {private local @@ -29,6 +57,68 @@ decl {const char *project = "fltk.org";} {private local decl {const char *application = "test/preferences";} {private local } +Function {gettext(const char *text)} { + comment {This is a minimal implementation of the GNU gettext API +for systems that don't have GNU libintl library.} open return_type {const char*} +} { + code {static const char* translation_table[][2] = { + { "Alarm at:", "Wecken um:" }, + { "Bread:", "Brot:" }, + { "Breakfast:", "Frühstück:" }, + { "Cancel", "Abbrechen" }, + { "Drink:", "Getränk:" }, + { "English", "Englisch" }, + { "German", "Deutsch" }, + { "Get Up:", "Aufstehen:" }, + { "Language:", "Sprache:" }, + { "My Preferences", "Meine Vorlieben" }, + { "NY Times", "Der Spiegel" }, + { "Newspaper:", "Tageszeitung:" }, + { "OK", "OK" }, + { "Please restart the app to use your new language setting.", + "Bitte starten Sie die App erneut um Ihre Spracheinstellung zu nutzen." }, + { "Wear:", "Schuhwerk:" }, + { "a.m.", "früh" }, + { "bare foot", "barfuß" }, + { "brush teeth", "Zähne putzen" }, + { "coffee", "Kaffee" }, + { "eggs", "Eier" }, + { "flip flops", "Schlappen" }, + { "juice", "Saft" }, + { "left side", "linke Seite" }, + { "min.", "Min." }, + { "of the bed", "vom Bett" }, + { "p.m.", "spät" }, + { "right side", "rechte Seite" }, + { "rye", "Roggen" }, + { "sandals", "Sandalen" }, + { "shave", "rasieren" }, + { "shoes", "Schuhe" }, + { "shower", "duschen" }, + { "sourdough", "Sauerteig" }, + { "tea", "Tee" }, + { "wheat", "Weizen" }, + { "white", "Weißbrot" }, + { "with butter", "mit Butter" }, + { "with milk", "mit Milch" }, +}; +int lang = g_language; +int i, n = 38; +const char *found = 0L; + +// As this is just a minimal demo, I did not implement binary search. +for (i=0; i} + } { + MenuItem {} { + label English + xywh {20 20 100 20} + } + MenuItem {} { + label German + xywh {20 20 100 20} + } + } } code {readPrefs();} {} } +Function {readLanguagePrefs()} { + comment {Read the language setting before we create the UI.} open return_type void +} { + code {Fl_Preferences app( Fl_Preferences::USER_L, project, application ); +app.get( "language", g_language, 0 );} {} +} + Function {readPrefs()} {open return_type void } { code {int boolValue; @@ -227,6 +340,8 @@ Fl_Preferences app( Fl_Preferences::USER_L, project, application ); } else { printf("Location of Preferences user data directory not found.\\n"); } + + wLanguage->value( g_language ); Fl_Preferences bed( app, "Bed" ); bed.get( "alarm", buffer, "8:00", 79 ); @@ -271,7 +386,7 @@ Fl_Preferences app( Fl_Preferences::USER_L, project, application ); wMinutes->value( doubleValue ); char *flexBuffer; - eat.get( "newspaper", flexBuffer, "NY Tymes" ); + eat.get( "newspaper", flexBuffer, gettext("NY Times") ); wPaper->value( flexBuffer ); if ( flexBuffer ) free( flexBuffer ); @@ -297,6 +412,8 @@ Function {writePrefs()} {open return_type void } { code {Fl_Preferences app( Fl_Preferences::USER_L, project, application ); + app.set( "language", wLanguage->value() ); + Fl_Preferences bed( app, "Bed" ); bed.set( "alarm", wAlarm->value() ); From 34f3c31cbb7f7395eef869557be6dd4142c5dd4b Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 24 Nov 2022 22:29:47 +0100 Subject: [PATCH 2/3] Remove warning about varargs string --- test/preferences.fl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/preferences.fl b/test/preferences.fl index 6617aac65b..32e6d13ad4 100644 --- a/test/preferences.fl +++ b/test/preferences.fl @@ -24,7 +24,7 @@ will translate all text into the current locale when generating the UI. -In this small example, 'getttext' is only emulated.} {selected in_source not_in_header +In this small example, 'getttext' is only emulated.} {in_source not_in_header } decl {\#include } {public local @@ -283,7 +283,7 @@ Function {} {open return_type int } Fl_Choice wLanguage { label {Language:} - callback {fl_message(_("Please restart the app to use your new language setting."));} open + callback {fl_message("%s", _("Please restart the app to use your new language setting."));} open selected xywh {120 269 105 20} down_box BORDER_BOX code0 {\#include } } { From cac67cab983d70aeecbecd3dcfbfd8b84961b3d0 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 24 Nov 2022 22:38:21 +0100 Subject: [PATCH 3/3] Fixes case sensitive misspelled filename --- test/preferences.fl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/preferences.fl b/test/preferences.fl index 32e6d13ad4..a855ce0f43 100644 --- a/test/preferences.fl +++ b/test/preferences.fl @@ -285,7 +285,7 @@ Function {} {open return_type int label {Language:} callback {fl_message("%s", _("Please restart the app to use your new language setting."));} open selected xywh {120 269 105 20} down_box BORDER_BOX - code0 {\#include } + code0 {\#include } } { MenuItem {} { label English