Move localization.js to app

Since it is no longer used in core. Also splits localization tests into
a separate file.
This commit is contained in:
Samuel Mannehed 2017-11-13 12:12:41 +01:00
parent 689580381c
commit 7279364c9a
5 changed files with 79 additions and 68 deletions

View File

@ -12,7 +12,7 @@
/* global window, document.getElementById, Util, WebUtil, RFB, Display */ /* global window, document.getElementById, Util, WebUtil, RFB, Display */
import * as Log from '../core/util/logging.js'; import * as Log from '../core/util/logging.js';
import _, { l10n } from '../core/util/localization.js'; import _, { l10n } from './localization.js';
import { isTouchDevice } from '../core/util/browsers.js'; import { isTouchDevice } from '../core/util/browsers.js';
import { setCapture, getPointerEvent } from '../core/util/events.js'; import { setCapture, getPointerEvent } from '../core/util/events.js';
import KeyTable from "../core/input/keysym.js"; import KeyTable from "../core/input/keysym.js";

View File

@ -61,6 +61,7 @@ module.exports = function(config) {
files: [ files: [
{ pattern: 'vendor/sinon.js', included: false }, { pattern: 'vendor/sinon.js', included: false },
{ pattern: 'node_modules/sinon-chai/lib/sinon-chai.js', included: false }, { pattern: 'node_modules/sinon-chai/lib/sinon-chai.js', included: false },
{ pattern: 'app/localization.js', included: false },
{ pattern: 'core/**/*.js', included: false }, { pattern: 'core/**/*.js', included: false },
{ pattern: 'vendor/pako/**/*.js', included: false }, { pattern: 'vendor/pako/**/*.js', included: false },
{ pattern: 'tests/test.*.js', included: false }, { pattern: 'tests/test.*.js', included: false },
@ -90,6 +91,7 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser // preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: { preprocessors: {
'app/localization.js': ['babel'],
'core/**/*.js': ['babel'], 'core/**/*.js': ['babel'],
'tests/test.*.js': ['babel'], 'tests/test.*.js': ['babel'],
'tests/fake.*.js': ['babel'], 'tests/fake.*.js': ['babel'],

View File

@ -0,0 +1,76 @@
/* jshint expr: true */
var assert = chai.assert;
var expect = chai.expect;
import l10nGet, { l10n } from '../app/localization.js';
describe('Localization', function() {
"use strict";
describe('language selection', function () {
var origNavigator;
beforeEach(function () {
// window.navigator is a protected read-only property in many
// environments, so we need to redefine it whilst running these
// tests.
origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
if (origNavigator === undefined) {
// Object.getOwnPropertyDescriptor() doesn't work
// properly in any version of IE
this.skip();
}
Object.defineProperty(window, "navigator", {value: {}});
if (window.navigator.languages !== undefined) {
// Object.defineProperty() doesn't work properly in old
// versions of Chrome
this.skip();
}
window.navigator.languages = [];
});
afterEach(function () {
Object.defineProperty(window, "navigator", origNavigator);
});
it('should use English by default', function() {
expect(l10n.language).to.equal('en');
});
it('should use English if no user language matches', function() {
window.navigator.languages = ["nl", "de"];
l10n.setup(["es", "fr"]);
expect(l10n.language).to.equal('en');
});
it('should use the most preferred user language', function() {
window.navigator.languages = ["nl", "de", "fr"];
l10n.setup(["es", "fr", "de"]);
expect(l10n.language).to.equal('de');
});
it('should prefer sub-languages languages', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt", "pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
it('should fall back to language "parents"', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["fr", "pt", "de"]);
expect(l10n.language).to.equal('pt');
});
it('should not use specific language when user asks for a generic language', function() {
window.navigator.languages = ["pt", "de"];
l10n.setup(["fr", "pt-BR", "de"]);
expect(l10n.language).to.equal('de');
});
it('should handle underscore as a separator', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt_BR"]);
expect(l10n.language).to.equal('pt_BR');
});
it('should handle difference in case', function() {
window.navigator.languages = ["pt-br"];
l10n.setup(["pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
});
});

View File

@ -4,7 +4,6 @@ var assert = chai.assert;
var expect = chai.expect; var expect = chai.expect;
import * as Log from '../core/util/logging.js'; import * as Log from '../core/util/logging.js';
import l10nGet, { l10n } from '../core/util/localization.js';
import sinon from '../vendor/sinon.js'; import sinon from '../vendor/sinon.js';
@ -63,72 +62,6 @@ describe('Utils', function() {
}); });
}); });
describe('language selection', function () {
var origNavigator;
beforeEach(function () {
// window.navigator is a protected read-only property in many
// environments, so we need to redefine it whilst running these
// tests.
origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
if (origNavigator === undefined) {
// Object.getOwnPropertyDescriptor() doesn't work
// properly in any version of IE
this.skip();
}
Object.defineProperty(window, "navigator", {value: {}});
if (window.navigator.languages !== undefined) {
// Object.defineProperty() doesn't work properly in old
// versions of Chrome
this.skip();
}
window.navigator.languages = [];
});
afterEach(function () {
Object.defineProperty(window, "navigator", origNavigator);
});
it('should use English by default', function() {
expect(l10n.language).to.equal('en');
});
it('should use English if no user language matches', function() {
window.navigator.languages = ["nl", "de"];
l10n.setup(["es", "fr"]);
expect(l10n.language).to.equal('en');
});
it('should use the most preferred user language', function() {
window.navigator.languages = ["nl", "de", "fr"];
l10n.setup(["es", "fr", "de"]);
expect(l10n.language).to.equal('de');
});
it('should prefer sub-languages languages', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt", "pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
it('should fall back to language "parents"', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["fr", "pt", "de"]);
expect(l10n.language).to.equal('pt');
});
it('should not use specific language when user asks for a generic language', function() {
window.navigator.languages = ["pt", "de"];
l10n.setup(["fr", "pt-BR", "de"]);
expect(l10n.language).to.equal('de');
});
it('should handle underscore as a separator', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt_BR"]);
expect(l10n.language).to.equal('pt_BR');
});
it('should handle difference in case', function() {
window.navigator.languages = ["pt-br"];
l10n.setup(["pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
});
// TODO(directxman12): test the conf_default and conf_defaults methods // TODO(directxman12): test the conf_default and conf_defaults methods
// TODO(directxman12): test decodeUTF8 // TODO(directxman12): test decodeUTF8
// TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent) // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)