refactor(i18n): move message and id into a separate file

This commit is contained in:
vsavkin 2016-03-14 11:45:14 -07:00 committed by Jeremy Elbourn
parent ea11b3f1f8
commit 2b165944ea
6 changed files with 63 additions and 16 deletions

View File

@ -1,6 +1,6 @@
library angular.core.facade.lang;
export 'dart:core' show Type, RegExp, print, DateTime;
export 'dart:core' show Type, RegExp, print, DateTime, Uri;
import 'dart:math' as math;
import 'dart:convert' as convert;
import 'dart:async' show Future, Zone;
@ -376,3 +376,7 @@ num bitWiseAnd(List values) {
var val = values.reduce((num a, num b) => (a as int) & (b as int));
return val as num;
}
String escape(String s) {
return Uri.encodeComponent(s);
}

View File

@ -18,6 +18,7 @@ export interface BrowserNodeGlobal {
clearTimeout: Function;
setInterval: Function;
clearInterval: Function;
encodeURI: Function;
}
// TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
@ -473,3 +474,7 @@ export function bitWiseOr(values: number[]): number {
export function bitWiseAnd(values: number[]): number {
return values.reduce((a, b) => { return a & b; });
}
export function escape(s: string): string {
return _global.encodeURI(s);
}

View File

@ -0,0 +1,21 @@
import {isPresent, escape} from 'angular2/src/facade/lang';
/**
* A message extracted from a template.
*
* The identity of a message is comprised of `content` and `meaning`.
*
* `description` is additional information provided to the translator.
*/
export class Message {
constructor(public content: string, public meaning: string, public description: string) {}
}
/**
* Computes the id of a message
*/
export function id(m: Message): string {
let meaning = isPresent(m.meaning) ? m.meaning : "";
let content = isPresent(m.content) ? m.content : "";
return escape(`$ng|${meaning}|${content}`);
}

View File

@ -13,21 +13,11 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
import {Interpolation} from 'angular2/src/core/change_detection/parser/ast';
import {Message, id} from './message';
const I18N_ATTR = "i18n";
const I18N_ATTR_PREFIX = "i18n-";
/**
* A message extracted from a template.
*
* The identity of a message is comprised of `content` and `meaning`.
*
* `description` is additional information provided to the translator.
*/
export class Message {
constructor(public content: string, public meaning: string, public description: string) {}
}
/**
* All messages extracted from a template.
*/
@ -56,9 +46,8 @@ export class I18nExtractionError extends ParseError {
export function removeDuplicates(messages: Message[]): Message[] {
let uniq: {[key: string]: Message} = {};
messages.forEach(m => {
let key = `$ng__${m.meaning}__|${m.content}`;
if (!StringMapWrapper.contains(uniq, key)) {
uniq[key] = m;
if (!StringMapWrapper.contains(uniq, id(m))) {
uniq[id(m)] = m;
}
});
return StringMapWrapper.values(uniq);

View File

@ -12,7 +12,8 @@ import {
} from 'angular2/testing_internal';
import {HtmlParser} from 'angular2/src/compiler/html_parser';
import {MessageExtractor, Message, removeDuplicates} from 'angular2/src/i18n/message_extractor';
import {MessageExtractor, removeDuplicates} from 'angular2/src/i18n/message_extractor';
import {Message} from 'angular2/src/i18n/message';
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
import {Lexer} from 'angular2/src/core/change_detection/parser/lexer';

View File

@ -0,0 +1,27 @@
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
iit,
inject,
it,
xdescribe,
xit
} from 'angular2/testing_internal';
import {Message, id} from 'angular2/src/i18n/message';
export function main() {
ddescribe('Message', () => {
describe("id", () => {
it("should return a different id for messages with and without the meaning", () => {
let m1 = new Message("content", "meaning", null);
let m2 = new Message("content", null, null);
expect(id(m1)).toEqual(id(m1));
expect(id(m1)).not.toEqual(id(m2));
});
});
});
}