Annotation of modules/damieng/graphical_editor/daxe/lib/src/strings.dart, revision 1.4

1.1       damieng     1: /*
                      2:   This file is part of Daxe.
                      3: 
                      4:   Daxe is free software: you can redistribute it and/or modify
                      5:   it under the terms of the GNU General Public License as published by
                      6:   the Free Software Foundation, either version 3 of the License, or
                      7:   (at your option) any later version.
                      8: 
                      9:   Daxe is distributed in the hope that it will be useful,
                     10:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     11:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     12:   GNU General Public License for more details.
                     13: 
                     14:   You should have received a copy of the GNU General Public License
                     15:   along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
                     16: */
                     17: 
1.4     ! raeburn    18: // $Id: strings.dart,v 1.3 2024/03/25 17:24:10 raeburn Exp $
1.3       raeburn    19: 
1.1       damieng    20: /// Provides localized strings.
                     21: library Strings;
                     22: 
                     23: import 'dart:async';
                     24: import 'dart:collection';
                     25: import 'dart:html' as h;
                     26: import 'package:intl/intl_browser.dart'; // or intl-standalone (see findSystemLocale)
                     27: 
                     28: 
                     29: /**
                     30:  * Provides localized strings read from properties files.
                     31:  * The current language file is read at application loading time.
                     32:  */
                     33: class Strings {
                     34:   
                     35:   static String resourcePath = "packages/daxe/LocalStrings";
                     36:   static HashMap<String, String> map = null;
                     37:   static String systemLocale;
                     38:   static const defaultLocale = 'en';
                     39:   
                     40:   static Future<bool> load() {
                     41:     Completer<bool> completer = new Completer<bool>();
1.4     ! raeburn    42:     getUserLanguage().then((String userlang) {
        !            43:       if (userlang != null)
        !            44:         systemLocale = userlang;
1.1       damieng    45:       else
                     46:         systemLocale = defaultLocale;
                     47:       String language = systemLocale.split('_')[0];
                     48:       String fullFilePath = "${resourcePath}_$language.properties";
1.2       damieng    49:       _request(fullFilePath, completer);
1.1       damieng    50:     });
                     51:     return(completer.future);
                     52:   }
1.4     ! raeburn    53: 
        !            54: /*
        !            55:  * Customization to use a user's language preference in LON-CAPA if available,
        !            56:  * otherwise default to en.
        !            57:  *
        !            58:  * Post a message from the iframe to the parent with userlang as data passed
        !            59:  * Listen for message posted back from parent with userlang:locale
        !            60:  * where locale is one of LON-CAPA's supported languages:
        !            61:  * ar en de fa fr he ja ko pt ru tr zh.  systemLocale will be set to this in
        !            62:  * load(). If no message is received within 500 ms, then completer completes
        !            63:  * and returns null, and load() will set systemLocale to default (en).
        !            64:  */
        !            65: 
        !            66:   static Future<String> getUserLanguage() {
        !            67:     Completer<String> completer = new Completer<String>();
        !            68:     String msgtarget = h.window.location.protocol+'//'+h.window.location.hostname;
        !            69:     String userlang = null;
        !            70:     Duration delay = new Duration(milliseconds:500);
        !            71:     Timer timer = new Timer(delay,() {
        !            72:       if (!completer.isCompleted) 
        !            73:         completer.complete(userlang);
        !            74:     });
        !            75:     if (!completer.isCompleted) {
        !            76:       h.window.parent.postMessage('userlang',msgtarget);
        !            77:       h.window.onMessage.listen((event) {
        !            78:         if (event.origin == msgtarget) {
        !            79:           List<String> msgdata = event.data.split(':');
        !            80:           if (msgdata[0] == 'userlang') { 
        !            81:             final RegExp usablelang = new RegExp("^(en|de|ar|fa|fr|he|ja|ko|pt|ru|tr|zh)\$");
        !            82:             if (usablelang.hasMatch(msgdata[1]))
        !            83:               userlang = msgdata[1];
        !            84:             timer.cancel();
        !            85:             if (!completer.isCompleted)
        !            86:                completer.complete(userlang);
        !            87:           }
        !            88:         }
        !            89:       });
        !            90:     }
        !            91:     return(completer.future);
        !            92:   }
1.1       damieng    93:   
                     94:   static String get(String key) {
                     95:     return(map[key]);
                     96:   }
                     97:   
1.2       damieng    98:   static void _request(String fullFilePath, Completer<bool> completer) {
                     99:     h.HttpRequest request = new h.HttpRequest();
                    100:     request.open("GET", fullFilePath);
                    101:     request.onLoad.listen((h.ProgressEvent event) {
                    102:       if (request.status == 404) {
                    103:         // no localization for this language, use default instead
                    104:         String defaultLanguage = defaultLocale.split('_')[0];
                    105:         String defaultFullFilePath = "${resourcePath}_$defaultLanguage.properties";
                    106:         if (fullFilePath == defaultFullFilePath)
                    107:           completer.completeError("Error when reading the strings in $resourcePath");
                    108:         else
                    109:           _request(defaultFullFilePath, completer);
                    110:       } else if (request.status != 200) {
                    111:         completer.completeError("Error when reading the strings in $resourcePath");
                    112:       } else {
                    113:         _parseResponse(request.responseText);
                    114:         completer.complete(true);
                    115:       }
                    116:     });
                    117:     request.onError.listen((h.ProgressEvent event) {
                    118:       completer.completeError("Error when reading the strings in $resourcePath");
                    119:     });
                    120:     request.send();
                    121:   }
                    122:   
                    123:   static void _parseResponse(String txt) {
                    124:     map = new HashMap<String, String>();
                    125:     List<String> lines = txt.split("\n");
                    126:     for (String line in lines) {
                    127:       if (line.startsWith('#'))
                    128:         continue;
                    129:       int ind = line.indexOf('=');
                    130:       if (ind == -1)
                    131:         continue;
                    132:       String key = line.substring(0, ind).trim();
                    133:       String value = line.substring(ind + 1).trim();
                    134:       map[key] = value;
                    135:     }
                    136:   }
1.1       damieng   137: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>