UNPKG

arb-convert

Version:

Convert Application Resource Bundle (ARB) translation files to other translation formats and back

253 lines (252 loc) 9.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var gettext_1 = require("../gettext"); var now = mockDateNow().now; var source = JSON.stringify({ '@@locale': 'en_US', '@@last_modified': '2019-12-31T16:00:00.000Z', simple: 'Super simple', '@simple': { description: '', type: 'text', placeholders: {}, }, param: 'Walk {distance}', '@param': { description: 'Walking instruction', type: 'text', placeholders: { distance: { example: '500 m', }, }, }, long: 'Very long string that exceeds the max char limit of 80 characters easily and thus forces a line break in the resulting PO file', '@long': { description: 'We need to test long comments as well, so this is me wasting time by writing something meaningful. Did you really read this to the end?', type: 'text', placeholders: {}, }, longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongkey: 'But a short string :D', '@longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongkey': { description: 'Well, also the key might be very long and need to be broken to multiple lines', type: 'text', placeholders: {}, }, }, null, 2); var target = JSON.stringify({ '@@locale': 'de_DE', '@@last_modified': '2019-12-31T16:00:00.000Z', simple: 'Super simpel', '@simple': { description: '', type: 'text', placeholders: {}, }, param: 'Laufe {distance}', '@param': { description: 'Walking instruction', type: 'text', placeholders: { distance: { example: '500 m', }, }, }, long: 'Ein sehr langer String der problemlos das maximale Zeichenlimit von 80 Zeichen überschreitet und damit einen Zeilenumbruch in der resultierenden PO-Datei erwingt', '@long': { description: 'We need to test long comments as well, so this is me wasting time by writing something meaningful. Did you really read this to the end?', type: 'text', placeholders: {}, }, longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongkey: 'Aber eine kurze Zeichenkette :D', '@longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongkey': { description: 'Well, also the key might be very long and need to be broken to multiple lines', type: 'text', placeholders: {}, }, }, null, 2); var expectedContentEmpty = '' + '# Translation converted from ARB\n' + '# original: \n' + '# srcLang: \n' + '# trgLang: \n' + 'msgid ""\n' + 'msgstr ""\n' + '"PO-Revision-Date: 2019-12-31 16:00+0000"\n' + '"MIME-Version: 1.0"\n' + '"Content-Type: text/plain; charset=UTF-8"\n' + '"Content-Transfer-Encoding: 8bit"\n'; var expectedContentWithSource = '' + '# Translation converted from ARB\n' + '# original: some ns\n' + '# srcLang: en-US\n' + '# trgLang: \n' + 'msgid ""\n' + 'msgstr ""\n' + '"PO-Revision-Date: 2019-12-31 16:00+0000"\n' + '"MIME-Version: 1.0"\n' + '"Content-Type: text/plain; charset=UTF-8"\n' + '"Content-Transfer-Encoding: 8bit"\n' + '\n' + 'msgctxt "simple"\n' + 'msgid "Super simple"\n' + 'msgstr ""\n' + '\n' + '#. Walking instruction\n' + '#. {distance} example: 500 m\n' + 'msgctxt "param"\n' + 'msgid "Walk {distance}"\n' + 'msgstr ""\n' + '\n' + '#. We need to test long comments as well, so this is me wasting time by writing \n' + '#. something meaningful. Did you really read this to the end?\n' + 'msgctxt "long"\n' + 'msgid ""\n' + '"Very long string that exceeds the max char limit of 80 characters easily and t"\n' + '"hus forces a line break in the resulting PO file"\n' + 'msgstr ""\n' + '\n' + '#. Well, also the key might be very long and need to be broken to multiple lines\n' + 'msgctxt ""\n' + '"longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglo"\n' + '"ngkey"\n' + 'msgid "But a short string :D"\n' + 'msgstr ""\n'; var expectedContentWithSourceAndTarget = '' + '# Translation converted from ARB\n' + '# original: some ns\n' + '# srcLang: en-US\n' + '# trgLang: de-DE\n' + 'msgid ""\n' + 'msgstr ""\n' + '"PO-Revision-Date: 2019-12-31 16:00+0000"\n' + '"MIME-Version: 1.0"\n' + '"Content-Type: text/plain; charset=UTF-8"\n' + '"Content-Transfer-Encoding: 8bit"\n' + '\n' + 'msgctxt "simple"\n' + 'msgid "Super simple"\n' + 'msgstr "Super simpel"\n' + '\n' + '#. Walking instruction\n' + '#. {distance} example: 500 m\n' + 'msgctxt "param"\n' + 'msgid "Walk {distance}"\n' + 'msgstr "Laufe {distance}"\n' + '\n' + '#. We need to test long comments as well, so this is me wasting time by writing \n' + '#. something meaningful. Did you really read this to the end?\n' + 'msgctxt "long"\n' + 'msgid ""\n' + '"Very long string that exceeds the max char limit of 80 characters easily and t"\n' + '"hus forces a line break in the resulting PO file"\n' + 'msgstr ""\n' + '"Ein sehr langer String der problemlos das maximale Zeichenlimit von 80 Zeichen"\n' + '" überschreitet und damit einen Zeilenumbruch in der resultierenden PO-Datei er"\n' + '"wingt"\n' + '\n' + '#. Well, also the key might be very long and need to be broken to multiple lines\n' + 'msgctxt ""\n' + '"longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglo"\n' + '"ngkey"\n' + 'msgid "But a short string :D"\n' + 'msgstr "Aber eine kurze Zeichenkette :D"\n'; describe('convert ARB to gettext PO', function () { test('with empty source and no other options', function () { expect(gettext_1.convert({ source: '{}' })).toEqual({ content: expectedContentEmpty, }); }); test('with source strings only', function () { expect(gettext_1.convert({ source: source, sourceLanguage: 'en-US', original: 'some ns', })).toEqual({ content: expectedContentWithSource, }); }); test('with source and target strings', function () { expect(gettext_1.convert({ source: source, target: target, sourceLanguage: 'en-US', targetLanguage: 'de-DE', original: 'some ns', })).toEqual({ content: expectedContentWithSourceAndTarget, }); }); }); describe('convert gettext PO to ARB', function () { test('with source strings only', function () { var content = expectedContentWithSource; expect(gettext_1.parse({ content: content })).toEqual({ source: source, target: '', original: 'some ns', sourceLanguage: 'en-US', targetLanguage: '', }); }); test('with source and target strings', function () { var content = expectedContentWithSourceAndTarget; expect(gettext_1.parse({ content: content })).toEqual({ source: source, target: target, original: 'some ns', sourceLanguage: 'en-US', targetLanguage: 'de-DE', }); }); }); describe('gettext utility functions', function () { test('timezoneOffset output with different timezones', function () { var date = new Date(now); // date.getTimezoneOffset() always returns the loca TZ offset, so we have to // monkey patch it to return different results. date.getTimezoneOffset = function () { return 0; }; expect(gettext_1.timezoneOffset(date)).toBe('+0000'); date.getTimezoneOffset = function () { return -60; }; expect(gettext_1.timezoneOffset(date)).toBe('+0100'); date.getTimezoneOffset = function () { return -270; }; expect(gettext_1.timezoneOffset(date)).toBe('+0430'); date.getTimezoneOffset = function () { return 300; }; expect(gettext_1.timezoneOffset(date)).toBe('-0500'); }); test('gettextToJs with all possible comment types', function () { var content = '' + '# first line\n' + '# second line\n' + '#. 1st line\n' + '#. 2nd line\n' + '#: file.dart:123, another_file.dart:456\n' + '#, fuzzy, c-format\n' + 'msgctxt "context"\n' + 'msgid "source string"\n' + 'msgstr ""\n' + '"target "\n' + '"string"\n'; expect(gettext_1.gettextToJs(content)).toEqual([ { translatorCommentLines: [ 'first line', 'second line', ], extractedCommentLines: [ '1st line', '2nd line', ], references: [ 'file.dart:123', 'another_file.dart:456', ], flags: ['fuzzy', 'c-format'], msgctxt: 'context', msgid: 'source string', msgstr: 'target string', }, ]); }); });