當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript xtb.Xtb類代碼示例

本文整理匯總了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>&lt;b&gt;</ex></ph>with placeholders<ph name="CLOSE_BOLD_TEXT"><ex>&lt;/b&gt;</ex></ph> <ph name="INTERPOLATION"/></translation>
  <translation id="b">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</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>&lt;p&gt;</ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</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">
//.........這裏部分代碼省略.........
開發者ID:AngularLovers,項目名稱:angular,代碼行數:101,代碼來源:xtb_spec.ts

示例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;
    }
開發者ID:chong999,項目名稱:angular,代碼行數:8,代碼來源:xtb_spec.ts

示例3: expect

 () => { expect(() => { serializer.write({}); }).toThrow(); });
開發者ID:AngularLovers,項目名稱:angular,代碼行數:1,代碼來源:xtb_spec.ts


注:本文中的@angular/compiler/src/i18n/serializers/xtb.Xtb類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。