File:  [LON-CAPA] / modules / damieng / graphical_editor / daxe / lib / src / strings.dart
Revision 1.4: download - view: text, annotated - select for diffs
Mon Mar 25 17:29:23 2024 UTC (7 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- In Daxe Editor use current language for user determined by LON-CAPA,
  if available, otherwise default to en.

/*
  This file is part of Daxe.

  Daxe is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Daxe is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
*/

// $Id: strings.dart,v 1.4 2024/03/25 17:29:23 raeburn Exp $

/// Provides localized strings.
library Strings;

import 'dart:async';
import 'dart:collection';
import 'dart:html' as h;
import 'package:intl/intl_browser.dart'; // or intl-standalone (see findSystemLocale)


/**
 * Provides localized strings read from properties files.
 * The current language file is read at application loading time.
 */
class Strings {
  
  static String resourcePath = "packages/daxe/LocalStrings";
  static HashMap<String, String> map = null;
  static String systemLocale;
  static const defaultLocale = 'en';
  
  static Future<bool> load() {
    Completer<bool> completer = new Completer<bool>();
    getUserLanguage().then((String userlang) {
      if (userlang != null)
        systemLocale = userlang;
      else
        systemLocale = defaultLocale;
      String language = systemLocale.split('_')[0];
      String fullFilePath = "${resourcePath}_$language.properties";
      _request(fullFilePath, completer);
    });
    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]);
  }
  
  static void _request(String fullFilePath, Completer<bool> completer) {
    h.HttpRequest request = new h.HttpRequest();
    request.open("GET", fullFilePath);
    request.onLoad.listen((h.ProgressEvent event) {
      if (request.status == 404) {
        // no localization for this language, use default instead
        String defaultLanguage = defaultLocale.split('_')[0];
        String defaultFullFilePath = "${resourcePath}_$defaultLanguage.properties";
        if (fullFilePath == defaultFullFilePath)
          completer.completeError("Error when reading the strings in $resourcePath");
        else
          _request(defaultFullFilePath, completer);
      } else if (request.status != 200) {
        completer.completeError("Error when reading the strings in $resourcePath");
      } else {
        _parseResponse(request.responseText);
        completer.complete(true);
      }
    });
    request.onError.listen((h.ProgressEvent event) {
      completer.completeError("Error when reading the strings in $resourcePath");
    });
    request.send();
  }
  
  static void _parseResponse(String txt) {
    map = new HashMap<String, String>();
    List<String> lines = txt.split("\n");
    for (String line in lines) {
      if (line.startsWith('#'))
        continue;
      int ind = line.indexOf('=');
      if (ind == -1)
        continue;
      String key = line.substring(0, ind).trim();
      String value = line.substring(ind + 1).trim();
      map[key] = value;
    }
  }
}

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