[LON-CAPA-cvs] cvs: loncom /homework daxepage.pm modules/damieng/graphical_editor/daxe/lib/src strings.dart modules/damieng/graphical_editor/loncapa_daxe/web lcd_strings.dart

raeburn raeburn at source.lon-capa.org
Mon Mar 25 13:29:23 EDT 2024


raeburn		Mon Mar 25 17:29:23 2024 EDT

  Modified files:              
    /modules/damieng/graphical_editor/loncapa_daxe/web	lcd_strings.dart 
    /modules/damieng/graphical_editor/daxe/lib/src	strings.dart 
    /loncom/homework	daxepage.pm 
  Log:
  - In Daxe Editor use current language for user determined by LON-CAPA, 
    if available, otherwise default to en.
  
  
Index: modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart
diff -u modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart:1.3 modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart:1.4
--- modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart:1.3	Mon Mar 25 17:21:14 2024
+++ modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart	Mon Mar 25 17:29:22 2024
@@ -15,7 +15,7 @@
   along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// $Id: lcd_strings.dart,v 1.3 2024/03/25 17:21:14 raeburn Exp $
+// $Id: lcd_strings.dart,v 1.4 2024/03/25 17:29:22 raeburn Exp $
 
 /// Provides localized strings.
 library LCDStrings;
@@ -39,11 +39,9 @@
   
   static Future<bool> load() {
     Completer<bool> completer = new Completer<bool>();
-    findSystemLocale().then((String sl) {
-      // note: this is not always the language chosen by the user
-      // see https://code.google.com/p/chromium/issues/detail?id=101138
-      if (sl != null)
-        systemLocale = sl;
+    getLCLanguage().then((String ul) {
+      if (ul != null)
+        systemLocale = ul;
       else
         systemLocale = defaultLocale;
       String language = systemLocale.split('_')[0];
@@ -52,6 +50,50 @@
     });
     return(completer.future);
   }
+
+/*
+ * Customization to use a user's language preference in LON-CAPA if available,
+ * otherwise default to en.
+ *
+ * Post a message from the iframe to the parent with userlclang as data passed
+ * Listen for message posted back from parent with userlclang:locale
+ * where locale is one of LON-CAPA's supported languages:
+ * en de ar fa fr he ja ko pt ru tr zh.  systemLocale will be set to this in
+ * load().  If no message is received within 500 ms, then completer completes
+ * and returns null, and load() will set systemLocale to default (en). 
+ *
+ * A similar function is needed in strings.dart in daxe/lib/src because the UI
+ * contains strings from LocalStrings_<lang>.properties in daxe/lib/src
+ * and also from LocalStrings_<lang>.properties in loncapa_daxe/web
+ */
+  
+  static Future<String> getLCLanguage() {
+    Completer<String> completer = new Completer<String>();
+    String ul = null;
+    Duration delay = new Duration(milliseconds:500);
+    Timer lctimer = new Timer(delay,() {
+      if (!completer.isCompleted)
+        completer.complete(ul);
+    });
+    if (!completer.isCompleted) {
+      String msgtarget = h.window.location.protocol+'//'+h.window.location.hostname;
+      h.window.parent.postMessage('userlclang',msgtarget);
+      h.window.onMessage.listen((event) {
+        if (event.origin == msgtarget) {
+          List<String> msgdata = event.data.split(':');
+          if (msgdata[0] == 'userlclang') {
+            final RegExp usablelang = new RegExp("^(en|de|ar|fa|fr|he|ja|ko|pt|ru|tr|zh)\$");
+            if (usablelang.hasMatch(msgdata[1]))
+              ul = msgdata[1];
+            lctimer.cancel();
+            if (!completer.isCompleted)
+              completer.complete(ul);
+          }
+        }
+      });
+    }
+    return(completer.future);
+  }
   
   static String get(String key) {
     return(map[key]);
Index: modules/damieng/graphical_editor/daxe/lib/src/strings.dart
diff -u modules/damieng/graphical_editor/daxe/lib/src/strings.dart:1.3 modules/damieng/graphical_editor/daxe/lib/src/strings.dart:1.4
--- modules/damieng/graphical_editor/daxe/lib/src/strings.dart:1.3	Mon Mar 25 17:24:10 2024
+++ modules/damieng/graphical_editor/daxe/lib/src/strings.dart	Mon Mar 25 17:29:23 2024
@@ -15,7 +15,7 @@
   along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// $Id: strings.dart,v 1.3 2024/03/25 17:24:10 raeburn Exp $
+// $Id: strings.dart,v 1.4 2024/03/25 17:29:23 raeburn Exp $
 
 /// Provides localized strings.
 library Strings;
@@ -39,11 +39,9 @@
   
   static Future<bool> load() {
     Completer<bool> completer = new Completer<bool>();
-    findSystemLocale().then((String sl) {
-      // note: this is not always the language chosen by the user
-      // see https://code.google.com/p/chromium/issues/detail?id=101138
-      if (sl != null)
-        systemLocale = sl;
+    getUserLanguage().then((String userlang) {
+      if (userlang != null)
+        systemLocale = userlang;
       else
         systemLocale = defaultLocale;
       String language = systemLocale.split('_')[0];
@@ -52,6 +50,46 @@
     });
     return(completer.future);
   }
+
+/*
+ * Customization to use a user's language preference in LON-CAPA if available,
+ * otherwise default to en.
+ *
+ * Post a message from the iframe to the parent with userlang as data passed
+ * Listen for message posted back from parent with userlang:locale
+ * where locale is one of LON-CAPA's supported languages:
+ * ar en de fa fr he ja ko pt ru tr zh.  systemLocale will be set to this in
+ * load(). If no message is received within 500 ms, then completer completes
+ * and returns null, and load() will set systemLocale to default (en).
+ */
+
+  static Future<String> getUserLanguage() {
+    Completer<String> completer = new Completer<String>();
+    String msgtarget = h.window.location.protocol+'//'+h.window.location.hostname;
+    String userlang = null;
+    Duration delay = new Duration(milliseconds:500);
+    Timer timer = new Timer(delay,() {
+      if (!completer.isCompleted) 
+        completer.complete(userlang);
+    });
+    if (!completer.isCompleted) {
+      h.window.parent.postMessage('userlang',msgtarget);
+      h.window.onMessage.listen((event) {
+        if (event.origin == msgtarget) {
+          List<String> msgdata = event.data.split(':');
+          if (msgdata[0] == 'userlang') { 
+            final RegExp usablelang = new RegExp("^(en|de|ar|fa|fr|he|ja|ko|pt|ru|tr|zh)\$");
+            if (usablelang.hasMatch(msgdata[1]))
+              userlang = msgdata[1];
+            timer.cancel();
+            if (!completer.isCompleted)
+               completer.complete(userlang);
+          }
+        }
+      });
+    }
+    return(completer.future);
+  }
   
   static String get(String key) {
     return(map[key]);
Index: loncom/homework/daxepage.pm
diff -u loncom/homework/daxepage.pm:1.12 loncom/homework/daxepage.pm:1.13
--- loncom/homework/daxepage.pm:1.12	Sat Mar 23 23:11:52 2024
+++ loncom/homework/daxepage.pm	Mon Mar 25 17:29:23 2024
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Page with Daxe on the left side and the preview on the right side
 #
-# $Id: daxepage.pm,v 1.12 2024/03/23 23:11:52 raeburn Exp $
+# $Id: daxepage.pm,v 1.13 2024/03/25 17:29:23 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -81,11 +81,12 @@
                                         );
     my $name = $uri;
     $name =~ s/^.*\/([^\/]+)$/$1/;
+    my $lang = &Apache::lonlocal::current_language();
     my $filearg = '/daxeopen'.$uri;
     my $daxeurl = '/adm/daxe/daxe.html?config=config/loncapa_config.xml&save=/daxesave'.
                   '&file='.$filearg;
     my $headjs = &Apache::loncommon::iframe_wrapper_headjs().
-                 &listener_js($filearg).
+                 &listener_js($lang,$filearg).
                  &toggle_LCmenus_js().&saveandview_js().
                  &Apache::edit::js_change_detection();
 
@@ -159,7 +160,7 @@
 }
 
 sub listener_js {
-    my ($filearg) = @_;
+    my ($lang,$filearg) = @_;
     return <<"ENDJS";
 <script type="text/javascript">
 //<![CDATA[
@@ -173,6 +174,9 @@
     if (e.origin == reqdOrigin) {
         if (e.data == '$filearg') {
             setmode(document.daxeedit,'view');
+        } else if ((e.data == 'userlclang') || (e.data == 'userlang')) {
+            window.myIframe = document.getElementById("lcdiframe").contentWindow;
+            window.myIframe.postMessage(e.data+':$lang',reqdOrigin);
         }
         return;
     }




More information about the LON-CAPA-cvs mailing list