refactor(i18n): move message and id into a separate file
This commit is contained in:
parent
ea11b3f1f8
commit
2b165944ea
|
@ -1,6 +1,6 @@
|
||||||
library angular.core.facade.lang;
|
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:math' as math;
|
||||||
import 'dart:convert' as convert;
|
import 'dart:convert' as convert;
|
||||||
import 'dart:async' show Future, Zone;
|
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));
|
var val = values.reduce((num a, num b) => (a as int) & (b as int));
|
||||||
return val as num;
|
return val as num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String escape(String s) {
|
||||||
|
return Uri.encodeComponent(s);
|
||||||
|
}
|
|
@ -18,6 +18,7 @@ export interface BrowserNodeGlobal {
|
||||||
clearTimeout: Function;
|
clearTimeout: Function;
|
||||||
setInterval: Function;
|
setInterval: Function;
|
||||||
clearInterval: Function;
|
clearInterval: Function;
|
||||||
|
encodeURI: Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
|
// 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 {
|
export function bitWiseAnd(values: number[]): number {
|
||||||
return values.reduce((a, b) => { return a & b; });
|
return values.reduce((a, b) => { return a & b; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function escape(s: string): string {
|
||||||
|
return _global.encodeURI(s);
|
||||||
|
}
|
|
@ -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}`);
|
||||||
|
}
|
|
@ -13,21 +13,11 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
|
||||||
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
||||||
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
|
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
|
||||||
import {Interpolation} from 'angular2/src/core/change_detection/parser/ast';
|
import {Interpolation} from 'angular2/src/core/change_detection/parser/ast';
|
||||||
|
import {Message, id} from './message';
|
||||||
|
|
||||||
const I18N_ATTR = "i18n";
|
const I18N_ATTR = "i18n";
|
||||||
const I18N_ATTR_PREFIX = "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.
|
* All messages extracted from a template.
|
||||||
*/
|
*/
|
||||||
|
@ -56,9 +46,8 @@ export class I18nExtractionError extends ParseError {
|
||||||
export function removeDuplicates(messages: Message[]): Message[] {
|
export function removeDuplicates(messages: Message[]): Message[] {
|
||||||
let uniq: {[key: string]: Message} = {};
|
let uniq: {[key: string]: Message} = {};
|
||||||
messages.forEach(m => {
|
messages.forEach(m => {
|
||||||
let key = `$ng__${m.meaning}__|${m.content}`;
|
if (!StringMapWrapper.contains(uniq, id(m))) {
|
||||||
if (!StringMapWrapper.contains(uniq, key)) {
|
uniq[id(m)] = m;
|
||||||
uniq[key] = m;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return StringMapWrapper.values(uniq);
|
return StringMapWrapper.values(uniq);
|
||||||
|
|
|
@ -12,7 +12,8 @@ import {
|
||||||
} from 'angular2/testing_internal';
|
} from 'angular2/testing_internal';
|
||||||
|
|
||||||
import {HtmlParser} from 'angular2/src/compiler/html_parser';
|
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 {Parser} from 'angular2/src/core/change_detection/parser/parser';
|
||||||
import {Lexer} from 'angular2/src/core/change_detection/parser/lexer';
|
import {Lexer} from 'angular2/src/core/change_detection/parser/lexer';
|
||||||
|
|
||||||
|
|
|
@ -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));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue