
There are restrictions on the character set that can be used for xmb and xtb placeholder names. However because changing the placeholder names would change the message IDs it is not possible to add those restrictions to the names used internally. Then we have to map internal name to public names when generating an xmb file and back when translating using an xtb file. Note for implementors of `Serializer`: - When writing a file, the implementor should take care of converting the internal names to public names while visiting the message nodes - this is required because the original nodes are needed to compute the message ID. - When reading a file, the implementor does not need to take care of the mapping back to internal names as this is handled in the `I18nToHtmlVisitor` used by the `TranslationBundle`. fixes b/34339636
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import {serializeNodes} from '../../src/i18n/digest';
|
|
import * as i18n from '../../src/i18n/i18n_ast';
|
|
import {MessageBundle} from '../../src/i18n/message_bundle';
|
|
import {Serializer} from '../../src/i18n/serializers/serializer';
|
|
import {HtmlParser} from '../../src/ml_parser/html_parser';
|
|
import {DEFAULT_INTERPOLATION_CONFIG} from '../../src/ml_parser/interpolation_config';
|
|
|
|
export function main(): void {
|
|
describe('MessageBundle', () => {
|
|
describe('Messages', () => {
|
|
let messages: MessageBundle;
|
|
|
|
beforeEach(() => { messages = new MessageBundle(new HtmlParser, [], {}); });
|
|
|
|
it('should extract the message to the catalog', () => {
|
|
messages.updateFromTemplate(
|
|
'<p i18n="m|d">Translate Me</p>', 'url', DEFAULT_INTERPOLATION_CONFIG);
|
|
expect(humanizeMessages(messages)).toEqual([
|
|
'Translate Me (m|d)',
|
|
]);
|
|
});
|
|
|
|
it('should extract the all messages and duplicates', () => {
|
|
messages.updateFromTemplate(
|
|
'<p i18n="m|d">Translate Me</p><p i18n>Translate Me</p><p i18n>Translate Me</p>', 'url',
|
|
DEFAULT_INTERPOLATION_CONFIG);
|
|
expect(humanizeMessages(messages)).toEqual([
|
|
'Translate Me (m|d)',
|
|
'Translate Me (|)',
|
|
'Translate Me (|)',
|
|
]);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
class _TestSerializer extends Serializer {
|
|
write(messages: i18n.Message[]): string {
|
|
return messages.map(msg => `${serializeNodes(msg.nodes)} (${msg.meaning}|${msg.description})`)
|
|
.join('//');
|
|
}
|
|
|
|
load(content: string, url: string): {} { return null; }
|
|
|
|
digest(msg: i18n.Message): string { return 'unused'; }
|
|
}
|
|
|
|
function humanizeMessages(catalog: MessageBundle): string[] {
|
|
return catalog.write(new _TestSerializer()).split('//');
|
|
} |