X Tutup
Skip to content

Commit 2b16594

Browse files
vsavkinjelbourn
authored andcommitted
refactor(i18n): move message and id into a separate file
1 parent ea11b3f commit 2b16594

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

modules/angular2/src/facade/lang.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library angular.core.facade.lang;
22

3-
export 'dart:core' show Type, RegExp, print, DateTime;
3+
export 'dart:core' show Type, RegExp, print, DateTime, Uri;
44
import 'dart:math' as math;
55
import 'dart:convert' as convert;
66
import 'dart:async' show Future, Zone;
@@ -376,3 +376,7 @@ num bitWiseAnd(List values) {
376376
var val = values.reduce((num a, num b) => (a as int) & (b as int));
377377
return val as num;
378378
}
379+
380+
String escape(String s) {
381+
return Uri.encodeComponent(s);
382+
}

modules/angular2/src/facade/lang.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface BrowserNodeGlobal {
1818
clearTimeout: Function;
1919
setInterval: Function;
2020
clearInterval: Function;
21+
encodeURI: Function;
2122
}
2223

2324
// TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
@@ -473,3 +474,7 @@ export function bitWiseOr(values: number[]): number {
473474
export function bitWiseAnd(values: number[]): number {
474475
return values.reduce((a, b) => { return a & b; });
475476
}
477+
478+
export function escape(s: string): string {
479+
return _global.encodeURI(s);
480+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {isPresent, escape} from 'angular2/src/facade/lang';
2+
3+
/**
4+
* A message extracted from a template.
5+
*
6+
* The identity of a message is comprised of `content` and `meaning`.
7+
*
8+
* `description` is additional information provided to the translator.
9+
*/
10+
export class Message {
11+
constructor(public content: string, public meaning: string, public description: string) {}
12+
}
13+
14+
/**
15+
* Computes the id of a message
16+
*/
17+
export function id(m: Message): string {
18+
let meaning = isPresent(m.meaning) ? m.meaning : "";
19+
let content = isPresent(m.content) ? m.content : "";
20+
return escape(`$ng|${meaning}|${content}`);
21+
}

modules/angular2/src/i18n/message_extractor.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,11 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
1313
import {StringMapWrapper} from 'angular2/src/facade/collection';
1414
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
1515
import {Interpolation} from 'angular2/src/core/change_detection/parser/ast';
16+
import {Message, id} from './message';
1617

1718
const I18N_ATTR = "i18n";
1819
const I18N_ATTR_PREFIX = "i18n-";
1920

20-
/**
21-
* A message extracted from a template.
22-
*
23-
* The identity of a message is comprised of `content` and `meaning`.
24-
*
25-
* `description` is additional information provided to the translator.
26-
*/
27-
export class Message {
28-
constructor(public content: string, public meaning: string, public description: string) {}
29-
}
30-
3121
/**
3222
* All messages extracted from a template.
3323
*/
@@ -56,9 +46,8 @@ export class I18nExtractionError extends ParseError {
5646
export function removeDuplicates(messages: Message[]): Message[] {
5747
let uniq: {[key: string]: Message} = {};
5848
messages.forEach(m => {
59-
let key = `$ng__${m.meaning}__|${m.content}`;
60-
if (!StringMapWrapper.contains(uniq, key)) {
61-
uniq[key] = m;
49+
if (!StringMapWrapper.contains(uniq, id(m))) {
50+
uniq[id(m)] = m;
6251
}
6352
});
6453
return StringMapWrapper.values(uniq);

modules/angular2/test/i18n/message_extractor_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
} from 'angular2/testing_internal';
1313

1414
import {HtmlParser} from 'angular2/src/compiler/html_parser';
15-
import {MessageExtractor, Message, removeDuplicates} from 'angular2/src/i18n/message_extractor';
15+
import {MessageExtractor, removeDuplicates} from 'angular2/src/i18n/message_extractor';
16+
import {Message} from 'angular2/src/i18n/message';
1617
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
1718
import {Lexer} from 'angular2/src/core/change_detection/parser/lexer';
1819

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
AsyncTestCompleter,
3+
beforeEach,
4+
ddescribe,
5+
describe,
6+
expect,
7+
iit,
8+
inject,
9+
it,
10+
xdescribe,
11+
xit
12+
} from 'angular2/testing_internal';
13+
14+
import {Message, id} from 'angular2/src/i18n/message';
15+
16+
export function main() {
17+
ddescribe('Message', () => {
18+
describe("id", () => {
19+
it("should return a different id for messages with and without the meaning", () => {
20+
let m1 = new Message("content", "meaning", null);
21+
let m2 = new Message("content", null, null);
22+
expect(id(m1)).toEqual(id(m1));
23+
expect(id(m1)).not.toEqual(id(m2));
24+
});
25+
});
26+
});
27+
}

0 commit comments

Comments
 (0)
X Tutup