本文整理匯總了TypeScript中@angular/compiler/src/i18n/serializers/xtb.Xtb類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Xtb類的具體用法?TypeScript Xtb怎麽用?TypeScript Xtb使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Xtb類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: describe
describe('XTB serializer', () => {
let serializer: Xtb;
function loadAsText(content: string, placeholders: {[id: string]: {[name: string]: string}}):
{[id: string]: string} {
const asAst = serializer.load(content, 'url', placeholders);
let asText: {[id: string]: string} = {};
Object.getOwnPropertyNames(asAst).forEach(
id => { asText[id] = serializeAst(asAst[id]).join(''); });
return asText;
}
beforeEach(() => { serializer = new Xtb(new HtmlParser(), DEFAULT_INTERPOLATION_CONFIG); });
describe('load', () => {
it('should load XTB files with a doctype', () => {
const XTB = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE translationbundle [<!ELEMENT translationbundle (translation)*>
<!ATTLIST translationbundle lang CDATA #REQUIRED>
<!ELEMENT translation (#PCDATA|ph)*>
<!ATTLIST translation id CDATA #REQUIRED>
<!ELEMENT ph EMPTY>
<!ATTLIST ph name CDATA #REQUIRED>
]>
<translationbundle>
<translation id="foo">bar</translation>
</translationbundle>`;
expect(loadAsText(XTB, {})).toEqual({foo: 'bar'});
});
it('should load XTB files without placeholders', () => {
const XTB = `<?xml version="1.0" encoding="UTF-8"?>
<translationbundle>
<translation id="foo">bar</translation>
</translationbundle>`;
expect(loadAsText(XTB, {})).toEqual({foo: 'bar'});
});
it('should load XTB files with placeholders', () => {
const XTB = `<?xml version="1.0" encoding="UTF-8"?>
<translationbundle>
<translation id="foo">bar<ph name="PLACEHOLDER"/><ph name="PLACEHOLDER"/></translation>
</translationbundle>`;
expect(loadAsText(XTB, {foo: {PLACEHOLDER: '!'}})).toEqual({foo: 'bar!!'});
});
it('should load complex XTB files', () => {
const XTB = `<? xml version="1.0" encoding="UTF-8" ?>
<translationbundle>
<translation id="a">translatable element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></translation>
<translation id="b">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</translation>
<translation id="c" desc="d" meaning="m">foo</translation>
<translation id="d">{ count, plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</translation>
</translationbundle>`;
const PLACEHOLDERS = {
a: {
START_BOLD_TEXT: '<b>',
CLOSE_BOLD_TEXT: '</b>',
INTERPOLATION: '{{ a + b }}',
},
b: {
START_PARAGRAPH: '<p translated=true>',
CLOSE_PARAGRAPH: '</p>',
},
d: {
START_PARAGRAPH: '<p>',
CLOSE_PARAGRAPH: '</p>',
},
};
expect(loadAsText(XTB, PLACEHOLDERS)).toEqual({
a: 'translatable element <b>with placeholders</b> {{ a + b }}',
b: '{ count, plural, =0 {<p translated="true">test</p>}}',
c: 'foo',
d: '{ count, plural, =0 {{ sex, gender, other {<p>deeply nested</p>}} }}',
});
});
});
describe('errors', () => {
it('should throw on nested <translationbundle>', () => {
const XTB =
'<translationbundle><translationbundle></translationbundle></translationbundle>';
expect(() => {
serializer.load(XTB, 'url', {});
}).toThrowError(/<translationbundle> elements can not be nested/);
});
it('should throw on nested <translation>', () => {
const XTB = `<translationbundle>
<translation id="outer">
//.........這裏部分代碼省略.........
示例2: loadAsText
function loadAsText(content: string, placeholders: {[id: string]: {[name: string]: string}}):
{[id: string]: string} {
const asAst = serializer.load(content, 'url', placeholders);
let asText: {[id: string]: string} = {};
Object.keys(asAst).forEach(id => { asText[id] = serializeAst(asAst[id]).join(''); });
return asText;
}
示例3: expect
() => { expect(() => { serializer.write({}); }).toThrow(); });