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);