-
Notifications
You must be signed in to change notification settings - Fork 27.2k
I18n: Xmb Serializer #7620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
I18n: Xmb Serializer #7620
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a4af6b8
refactor(i18n): move message and id into a separate file
vsavkin 2d4614c
feat(i18n): implement xmb serializer
vsavkin 31accf1
feat(i18n): create i18n barrel
vsavkin a333e23
feat(i18n): add a simple dart script extracting all i18n messages fro…
vsavkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** | ||
| * @module | ||
| * @description | ||
| * Entry point to i18n | ||
| */ | ||
| export * from './src/i18n/message'; | ||
| export * from './src/i18n/xmb_serializer'; | ||
| export * from './src/i18n/message_extractor'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import {isPresent} from 'angular2/src/facade/lang'; | ||
| import {Message, id} from './message'; | ||
|
|
||
| export function serialize(messages: Message[]): string { | ||
| let ms = messages.map((m) => _serializeMessage(m)).join(""); | ||
| return `<message-bundle>${ms}</message-bundle>`; | ||
| } | ||
|
|
||
| function _serializeMessage(m: Message): string { | ||
| let desc = isPresent(m.description) ? ` desc='${m.description}'` : ""; | ||
| return `<msg id='${id(m)}'${desc}>${m.content}</msg>`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)); | ||
| }); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { | ||
| AsyncTestCompleter, | ||
| beforeEach, | ||
| ddescribe, | ||
| describe, | ||
| expect, | ||
| iit, | ||
| inject, | ||
| it, | ||
| xdescribe, | ||
| xit | ||
| } from 'angular2/testing_internal'; | ||
|
|
||
| import {Message, id} from 'angular2/src/i18n/message'; | ||
| import {serialize} from 'angular2/src/i18n/xmb_serializer'; | ||
|
|
||
| export function main() { | ||
| describe('Xmb Serialization', () => { | ||
| it("should return an empty message bundle for an empty list of messages", | ||
| () => { expect(serialize([])).toEqual("<message-bundle></message-bundle>"); }); | ||
|
|
||
| it("should serialize messages without desc", () => { | ||
| let m = new Message("content", "meaning", null); | ||
| let expected = `<message-bundle><msg id='${id(m)}'>content</msg></message-bundle>`; | ||
| expect(serialize([m])).toEqual(expected); | ||
| }); | ||
|
|
||
| it("should serialize messages with desc", () => { | ||
| let m = new Message("content", "meaning", "description"); | ||
| let expected = | ||
| `<message-bundle><msg id='${id(m)}' desc='description'>content</msg></message-bundle>`; | ||
| expect(serialize([m])).toEqual(expected); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import 'package:build/build.dart'; | ||
| import 'package:analyzer/src/generated/element.dart'; | ||
| import 'src/transform/common/url_resolver.dart'; | ||
| import 'dart:async'; | ||
| import 'package:angular2/i18n.dart'; | ||
| import 'package:angular2/src/core/change_detection/parser/parser.dart'; | ||
| import 'package:angular2/src/core/change_detection/parser/lexer.dart'; | ||
| import 'package:angular2/src/core/reflection/reflector.dart'; | ||
| import 'package:angular2/src/core/reflection/reflection_capabilities.dart'; | ||
| import 'package:angular2/src/compiler/html_parser.dart'; | ||
|
|
||
| /** | ||
| * An command-line utility extracting i18n messages from an application. | ||
| * | ||
| * For instance, the following command will extract all the messages from the 'my-app-package' package, where | ||
| * index.dart is the entry point, and will serialize them into i18n-messages.xml. | ||
| * | ||
| * pub run packages/angular2/extract_messages.dart 'my-app-package' 'web/src/index.dart' 'i18n-messages.xml' | ||
| */ | ||
| main(List<String> args) async { | ||
| final input = new InputSet(args[0], [args[1]]); | ||
| final output = new AssetId(args[0], args[2]); | ||
|
|
||
| await build(new PhaseGroup.singleAction(new I18nMessageExtractorBuilder(output), input)); | ||
| } | ||
|
|
||
| class I18nMessageExtractorBuilder implements Builder { | ||
| final AssetId outputAssetId; | ||
|
|
||
| I18nMessageExtractorBuilder(this.outputAssetId); | ||
|
|
||
| Future build(BuildStep buildStep) async { | ||
| final resolver = await buildStep.resolve(buildStep.input.id); | ||
| final entryLib = resolver.getLibrary(buildStep.input.id); | ||
|
|
||
| final extractor = new I18nMessageExtractor((path) => buildStep.readAsString(path)); | ||
| await extractor.processLibrary(entryLib); | ||
| resolver.release(); | ||
|
|
||
| if (extractor.errors.length > 0) { | ||
| print("Errors:"); | ||
| extractor.errors.forEach(print); | ||
| throw "Failed to extract messages"; | ||
|
|
||
| } else { | ||
| await buildStep.writeAsString(new Asset(outputAssetId, extractor.output)); | ||
| } | ||
| } | ||
|
|
||
| List<AssetId> declareOutputs(AssetId inputId) => [outputAssetId]; | ||
| } | ||
|
|
||
| class I18nMessageExtractor { | ||
| final TransformerUrlResolver urlResovler = new TransformerUrlResolver(); | ||
| final List<Message> messages = []; | ||
| final List errors = []; | ||
| final HtmlParser htmlParser = new HtmlParser(); | ||
| final Parser parser = new Parser(new Lexer(), new Reflector(new ReflectionCapabilities())); | ||
|
|
||
| final Function readInput; | ||
|
|
||
| I18nMessageExtractor(this.readInput); | ||
|
|
||
| String get output => serialize(removeDuplicates(messages)); | ||
|
|
||
| Future processLibrary(LibraryElement el) async { | ||
| return Future.wait(el.units.map(processCompilationUnit)); | ||
| } | ||
|
|
||
| Future processCompilationUnit(CompilationUnitElement el) async { | ||
| return Future.wait(el.types.map(processClass)); | ||
| } | ||
|
|
||
| Future processClass(ClassElement el) async { | ||
| final baseUrl = (el.source as dynamic).assetId; | ||
| final filtered = el.metadata.where((m) { | ||
| if (m.element is ConstructorElement) { | ||
| final isComponent = m.element.enclosingElement.name == "Component" && | ||
| m.element.library.displayName == "angular2.src.core.metadata"; | ||
|
|
||
| final isView = m.element.enclosingElement.name == "View" && | ||
| m.element.library.displayName == "angular2.src.core.metadata"; | ||
|
|
||
| return isComponent || isView; | ||
| } else { | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| return Future.wait(filtered.map((m) => processAnnotation(el, m, baseUrl))); | ||
| } | ||
|
|
||
| Future processAnnotation(ClassElement el, ElementAnnotation m, baseUrl) async { | ||
| final fields = (m.constantValue as dynamic).fields["(super)"].fields; | ||
| final template = fields["template"]; | ||
| final templateUrl = fields["templateUrl"]; | ||
|
|
||
| if (template != null && !template.isNull) { | ||
| processTemplate(template.toStringValue(), baseUrl.toString()); | ||
| } | ||
|
|
||
| if (templateUrl != null && !templateUrl.isNull) { | ||
| final value = templateUrl.toStringValue(); | ||
| final resolvedPath = urlResovler.resolve(toAssetUri(baseUrl), value); | ||
| final template = await readInput(fromUri(resolvedPath)); | ||
| processTemplate(template.toStringValue(), baseUrl.toString()); | ||
| } | ||
| } | ||
|
|
||
| void processTemplate(String template, String sourceUrl) { | ||
| final m = new MessageExtractor(htmlParser, parser); | ||
| final res = m.extract(template, sourceUrl); | ||
| if (res.errors.isNotEmpty) { | ||
| errors.addAll(res.errors); | ||
| } else { | ||
| messages.addAll(res.messages); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that importing this was expensive. Check with @yjbanov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just importing, no. If it's only used in the compiler and tree-shaken off before going to production, it won't do anything bad. If pulled into production, it will eat ~15kb as it implements the full Uri spec.