Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart, revision 1.1

1.1     ! damieng     1: /*
        !             2:   This file is part of LONCAPA-Daxe.
        !             3: 
        !             4:   LONCAPA-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:   LONCAPA-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: 
        !            18: /// Provides localized strings.
        !            19: library LCDStrings;
        !            20: 
        !            21: import 'dart:async';
        !            22: import 'dart:collection';
        !            23: import 'dart:html' as h;
        !            24: import 'package:intl/intl_browser.dart'; // or intl-standalone (see findSystemLocale)
        !            25: 
        !            26: 
        !            27: /**
        !            28:  * Provides localized strings read from properties files.
        !            29:  * The current language file is read at application loading time.
        !            30:  */
        !            31: class LCDStrings {
        !            32:   
        !            33:   static String resourcePath = "LocalStrings";
        !            34:   static HashMap<String, String> map = null;
        !            35:   static String systemLocale;
        !            36:   static const defaultLocale = 'en';
        !            37:   
        !            38:   static Future<bool> load() {
        !            39:     Completer<bool> completer = new Completer<bool>();
        !            40:     findSystemLocale().then((String sl) {
        !            41:       // note: this is not always the language chosen by the user
        !            42:       // see https://code.google.com/p/chromium/issues/detail?id=101138
        !            43:       if (sl != null)
        !            44:         systemLocale = sl;
        !            45:       else
        !            46:         systemLocale = defaultLocale;
        !            47:       String language = systemLocale.split('_')[0];
        !            48:       String fullFilePath = "${resourcePath}_$language.properties";
        !            49:       
        !            50:       h.HttpRequest request = new h.HttpRequest();
        !            51:       request.open("GET", fullFilePath);
        !            52:       request.onLoad.listen((h.ProgressEvent event) {
        !            53:         String txt = request.responseText;
        !            54:         map = new HashMap<String, String>();
        !            55:         List<String> lines = txt.split("\n");
        !            56:         for (String line in lines) {
        !            57:           if (line.startsWith('#'))
        !            58:             continue;
        !            59:           int ind = line.indexOf('=');
        !            60:           if (ind == -1)
        !            61:             continue;
        !            62:           String key = line.substring(0, ind).trim();
        !            63:           String value = line.substring(ind + 1).trim();
        !            64:           map[key] = value;
        !            65:         }
        !            66:         completer.complete(true);
        !            67:       });
        !            68:       request.onError.listen((h.ProgressEvent event) {
        !            69:         completer.completeError("Error when reading the strings in $resourcePath");
        !            70:       });
        !            71:       request.send();
        !            72:     });
        !            73:     return(completer.future);
        !            74:   }
        !            75:   
        !            76:   static String get(String key) {
        !            77:     return(map[key]);
        !            78:   }
        !            79:   
        !            80: }

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