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

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";
1.2     ! damieng    49:       _request(fullFilePath, completer);
1.1       damieng    50:     });
                     51:     return(completer.future);
                     52:   }
                     53:   
                     54:   static String get(String key) {
                     55:     return(map[key]);
                     56:   }
                     57:   
1.2     ! damieng    58:   static void _request(String fullFilePath, Completer<bool> completer) {
        !            59:     h.HttpRequest request = new h.HttpRequest();
        !            60:     request.open("GET", fullFilePath);
        !            61:     request.onLoad.listen((h.ProgressEvent event) {
        !            62:       if (request.status == 404) {
        !            63:         // no localization for this language, use default instead
        !            64:         String defaultLanguage = defaultLocale.split('_')[0];
        !            65:         String defaultFullFilePath = "${resourcePath}_$defaultLanguage.properties";
        !            66:         if (fullFilePath == defaultFullFilePath)
        !            67:           completer.completeError("Error when reading the strings in $resourcePath");
        !            68:         else
        !            69:           _request(defaultFullFilePath, completer);
        !            70:       } else if (request.status != 200) {
        !            71:         completer.completeError("Error when reading the strings in $resourcePath");
        !            72:       } else {
        !            73:         _parseResponse(request.responseText);
        !            74:         completer.complete(true);
        !            75:       }
        !            76:     });
        !            77:     request.onError.listen((h.ProgressEvent event) {
        !            78:       completer.completeError("Error when reading the strings in $resourcePath");
        !            79:     });
        !            80:     request.send();
        !            81:   }
        !            82:   
        !            83:   static void _parseResponse(String txt) {
        !            84:     map = new HashMap<String, String>();
        !            85:     List<String> lines = txt.split("\n");
        !            86:     for (String line in lines) {
        !            87:       if (line.startsWith('#'))
        !            88:         continue;
        !            89:       int ind = line.indexOf('=');
        !            90:       if (ind == -1)
        !            91:         continue;
        !            92:       String key = line.substring(0, ind).trim();
        !            93:       String value = line.substring(ind + 1).trim();
        !            94:       map[key] = value;
        !            95:     }
        !            96:   }
1.1       damieng    97: }

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