Welcome to mirror list, hosted at ThFree Co, Russian Federation.

l10nSpec.js « specs « tests « js « core - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18c1adabea1d18bdb9b772cdfa1b92abd26385a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

describe('OC.L10N tests', function() {
	var TEST_APP = 'jsunittestapp';

	beforeEach(function() {
		OC.appswebroots[TEST_APP] = OC.getRootPath() + '/apps3/jsunittestapp';
	});
	afterEach(function() {
		OC.L10N._unregister(TEST_APP);
		delete OC.appswebroots[TEST_APP];
	});

	describe('text translation', function() {
		beforeEach(function() {
			spyOn(console, 'warn');
			OC.L10N.register(TEST_APP, {
				'Hello world!': 'Hallo Welt!',
				'Hello {name}, the weather is {weather}': 'Hallo {name}, das Wetter ist {weather}',
				'sunny': 'sonnig'
			});
		});
		it('returns untranslated text when no bundle exists', function() {
			OC.L10N._unregister(TEST_APP);
			expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
		});
		it('returns untranslated text when no key exists', function() {
			expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
		});
		it('returns translated text when key exists', function() {
			expect(t(TEST_APP, 'Hello world!')).toEqual('Hallo Welt!');
		});
		it('returns translated text with placeholder', function() {
			expect(
				t(TEST_APP, 'Hello {name}, the weather is {weather}', {name: 'Steve', weather: t(TEST_APP, 'sunny')})
			).toEqual('Hallo Steve, das Wetter ist sonnig');
		});
		it('returns text with escaped placeholder', function() {
			expect(
				t(TEST_APP, 'Hello {name}', {name: '<strong>Steve</strong>'})
			).toEqual('Hello &lt;strong&gt;Steve&lt;/strong&gt;');
		});
		it('returns text with not escaped placeholder', function() {
			expect(
				t(TEST_APP, 'Hello {name}', {name: '<strong>Steve</strong>'}, null, {escape: false})
			).toEqual('Hello <strong>Steve</strong>');
		});
		it('uses DOMPurify to escape the text', function() {
			expect(
				t(TEST_APP, '<strong>These are your search results<script>alert(1)</script></strong>', null, {escape: false})
			).toEqual('<strong>These are your search results</strong>');
		});
		it('keeps old texts when registering existing bundle', function() {
			OC.L10N.register(TEST_APP, {
				'sunny': 'sonnig',
				'new': 'neu'
			});
			expect(t(TEST_APP, 'sunny')).toEqual('sonnig');
			expect(t(TEST_APP, 'new')).toEqual('neu');
		});
	});
	describe('plurals', function() {
		function checkPlurals() {
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 0)
			).toEqual('0 Dateien herunterladen');
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 1)
			).toEqual('1 Datei herunterladen');
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 2)
			).toEqual('2 Dateien herunterladen');
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 1024)
			).toEqual('1024 Dateien herunterladen');
		}

		it('generates plural for default text when translation does not exist', function() {
			spyOn(console, 'warn');
			OC.L10N.register(TEST_APP, {
			});
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 0)
			).toEqual('download 0 files');
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 1)
			).toEqual('download 1 file');
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 2)
			).toEqual('download 2 files');
			expect(
				n(TEST_APP, 'download %n file', 'download %n files', 1024)
			).toEqual('download 1024 files');
		});
		it('generates plural with default function when no forms specified', function() {
			spyOn(console, 'warn');
			OC.L10N.register(TEST_APP, {
				'_download %n file_::_download %n files_':
					['%n Datei herunterladen', '%n Dateien herunterladen']
			});
			checkPlurals();
		});
	});
	describe('async loading of translations', function() {
		it('loads bundle for given app and calls callback', function(done) {
			var localeStub = sinon.stub(OC, 'getLocale').returns('zh_CN');
			var callbackStub = sinon.stub();
			var promiseStub = sinon.stub();
			var loading = OC.L10N.load(TEST_APP, callbackStub);
			expect(callbackStub.notCalled).toEqual(true);
			var req = fakeServer.requests[0];

			loading
				.then(promiseStub)
				.then(function() {
					expect(fakeServer.requests.length).toEqual(1);
					expect(req.url).toEqual(
						OC.getRootPath() + '/apps3/' + TEST_APP + '/l10n/zh_CN.json'
					);

					expect(callbackStub.calledOnce).toEqual(true);
					expect(promiseStub.calledOnce).toEqual(true);
					expect(t(TEST_APP, 'Hello world!')).toEqual('你好世界!');
					localeStub.restore();
				})
				.then(done);

			expect(promiseStub.notCalled).toEqual(true);
			req.respond(
				200,
				{ 'Content-Type': 'application/json' },
				JSON.stringify({
					translations: {'Hello world!': '你好世界!'},
					pluralForm: 'nplurals=2; plural=(n != 1);'
				})
			);
		});
		it('calls callback if translation already available', function(done) {
			var callbackStub = sinon.stub();
			spyOn(console, 'warn');
			OC.L10N.register(TEST_APP, {
				'Hello world!': 'Hallo Welt!'
			});
			OC.L10N.load(TEST_APP, callbackStub)
				.then(function() {
					expect(callbackStub.calledOnce).toEqual(true);
					expect(fakeServer.requests.length).toEqual(0);
				})
				.then(done);

		});
		it('calls callback if locale is en', function(done) {
			var callbackStub = sinon.stub();
			OC.L10N.load(TEST_APP, callbackStub)
				.then(function() {
					expect(callbackStub.calledOnce).toEqual(true);
					expect(fakeServer.requests.length).toEqual(0);
				})
				.then(done)
				.catch(done);
		});
	});
});