generateTemplate function

String generateTemplate(
  1. TranslationTemplate template,
  2. XliffVersion version
)

Implementation

String generateTemplate(TranslationTemplate template, XliffVersion version) {
  final builder = XmlBuilder();
  builder.processing('xml', 'version="1.0 encoding="UTF-8""');
  builder.element('xliff', attributes: {
    ...attributesForVersion(version),
    if (version == XliffVersion.v1) ...{
      'version': '1.2',
      'source-language': template.defaultLocale,
    } else ...{
      'version': '2.0',
      'srcLang': template.defaultLocale,
    }
  }, nest: () {
    builder.element('file', nest: () {
      template.messages.forEach((key, message) {
        final text = messageToIcuString(message);
        if (version == XliffVersion.v2) {
          builder.element('unit', attributes: {'id': key, 'name': key},
              nest: () {
            builder.element('segment', nest: () {
              builder.element('notes', nest: () {
                builder.element('note', attributes: {'category': 'format'},
                    nest: () {
                  builder.text('icu');
                });
                if (message.description != null) {
                  builder.element('note',
                      attributes: {'category': 'description'}, nest: () {
                    builder.text(message.description);
                  });
                }
              });
              builder.element('source', nest: () {
                builder.text(text);
              });
              builder.element('target', nest: () {
                builder.text('');
              });
            });
          });
        } else {
          builder.element('trans-unit', attributes: {'id': key}, nest: () {
            builder.element('notes', nest: () {
              builder.element('note', attributes: {'category': 'format'},
                  nest: () {
                builder.text('icu');
              });
              if (message.description != null) {
                builder.element('note', attributes: {'category': 'description'},
                    nest: () {
                  builder.text(message.description);
                });
              }
            });
            builder.element('source', nest: () {
              builder.text(text);
            });
            builder.element('target', nest: () {
              builder.text('');
            });
          });
        }
      });
    });
  });
  // ignore: deprecated_member_use
  return builder.build().toXmlString(pretty: true);
}