Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/lcd_strings.dart, revision 1.3
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:
1.3 ! raeburn 18: // $Id: strings.dart,v 1.3 2024/03/25 17:20:00 raeburn Exp $
! 19:
1.1 damieng 20: /// Provides localized strings.
21: library LCDStrings;
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 LCDStrings {
34:
35: static String resourcePath = "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>();
42: findSystemLocale().then((String sl) {
43: // note: this is not always the language chosen by the user
44: // see https://code.google.com/p/chromium/issues/detail?id=101138
45: if (sl != null)
46: systemLocale = sl;
47: else
48: systemLocale = defaultLocale;
49: String language = systemLocale.split('_')[0];
50: String fullFilePath = "${resourcePath}_$language.properties";
1.2 damieng 51: _request(fullFilePath, completer);
1.1 damieng 52: });
53: return(completer.future);
54: }
55:
56: static String get(String key) {
57: return(map[key]);
58: }
59:
1.2 damieng 60: static void _request(String fullFilePath, Completer<bool> completer) {
61: h.HttpRequest request = new h.HttpRequest();
62: request.open("GET", fullFilePath);
63: request.onLoad.listen((h.ProgressEvent event) {
64: if (request.status == 404) {
65: // no localization for this language, use default instead
66: String defaultLanguage = defaultLocale.split('_')[0];
67: String defaultFullFilePath = "${resourcePath}_$defaultLanguage.properties";
68: if (fullFilePath == defaultFullFilePath)
69: completer.completeError("Error when reading the strings in $resourcePath");
70: else
71: _request(defaultFullFilePath, completer);
72: } else if (request.status != 200) {
73: completer.completeError("Error when reading the strings in $resourcePath");
74: } else {
75: _parseResponse(request.responseText);
76: completer.complete(true);
77: }
78: });
79: request.onError.listen((h.ProgressEvent event) {
80: completer.completeError("Error when reading the strings in $resourcePath");
81: });
82: request.send();
83: }
84:
85: static void _parseResponse(String txt) {
86: map = new HashMap<String, String>();
87: List<String> lines = txt.split("\n");
88: for (String line in lines) {
89: if (line.startsWith('#'))
90: continue;
91: int ind = line.indexOf('=');
92: if (ind == -1)
93: continue;
94: String key = line.substring(0, ind).trim();
95: String value = line.substring(ind + 1).trim();
96: map[key] = value;
97: }
98: }
1.1 damieng 99: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>