diff --git a/packages/ckeditor5-dev-utils/lib/translations/multiplelanguagetranslationservice.js b/packages/ckeditor5-dev-utils/lib/translations/multiplelanguagetranslationservice.js index 33ff57ed5..4c37ab376 100644 --- a/packages/ckeditor5-dev-utils/lib/translations/multiplelanguagetranslationservice.js +++ b/packages/ckeditor5-dev-utils/lib/translations/multiplelanguagetranslationservice.js @@ -21,14 +21,14 @@ const { EventEmitter } = require( 'events' ); module.exports = class MultipleLanguageTranslationService extends EventEmitter { /** * @param {Array.} languages Target languages. - * @param {Boolean} compileAllLanguages Flag indicates whether the languages are specified or should be found at runtime. + * @param {Boolean} [compileAllLanguages] Flag indicates whether the languages are specified or should be found at runtime. */ constructor( languages, compileAllLanguages ) { super(); this._languages = new Set( languages ); - this._compileAllLanguages = compileAllLanguages; + this._compileAllLanguages = !!compileAllLanguages; this._packagePaths = new Set(); diff --git a/packages/ckeditor5-dev-utils/tests/translations/multiplelanguagetranslationservice.js b/packages/ckeditor5-dev-utils/tests/translations/multiplelanguagetranslationservice.js index 70e6ed45e..a99cea302 100644 --- a/packages/ckeditor5-dev-utils/tests/translations/multiplelanguagetranslationservice.js +++ b/packages/ckeditor5-dev-utils/tests/translations/multiplelanguagetranslationservice.js @@ -12,15 +12,19 @@ const proxyquire = require( 'proxyquire' ); describe( 'translations', () => { describe( 'MultipleLanguageTranslationService', () => { - let MultipleLanguageTranslationService, stubs, files, fileContents, sandbox; + let MultipleLanguageTranslationService, stubs, filesAndDirs, fileContents, dirContents; + const sandbox = sinon.sandbox.create(); beforeEach( () => { - sandbox = sinon.sandbox.create(); + filesAndDirs = []; + fileContents = {}; + dirContents = {}; stubs = { fs: { - existsSync: path => files.includes( path ), - readFileSync: path => fileContents[ path ] + existsSync: path => filesAndDirs.includes( path ), + readFileSync: path => fileContents[ path ], + readdirSync: dir => dirContents[ dir ] } }; @@ -42,12 +46,12 @@ describe( 'translations', () => { } ); describe( 'loadPackage()', () => { - it( 'should load po file from the package and load translations', () => { + it( 'should load PO file from the package and load translations', () => { const translationService = new MultipleLanguageTranslationService( [ 'pl', 'de' ] ); const pathToPlTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'pl.po' ); const pathToDeTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'de.po' ); - files = [ pathToPlTranslations, pathToDeTranslations ]; + filesAndDirs = [ pathToPlTranslations, pathToDeTranslations ]; fileContents = { [ pathToPlTranslations ]: [ @@ -76,10 +80,10 @@ describe( 'translations', () => { } ); } ); - it( 'should do nothing if the po file does not exist', () => { + it( 'should do nothing if the PO file does not exist', () => { const translationService = new MultipleLanguageTranslationService( [ 'pl', 'de' ] ); - files = []; + filesAndDirs = []; fileContents = {}; translationService.loadPackage( 'pathToPackage' ); @@ -87,7 +91,7 @@ describe( 'translations', () => { expect( translationService._dictionary ).to.deep.equal( {} ); } ); - it( 'should load po file from the package only once per language', () => { + it( 'should load PO file from the package only once per language', () => { const translationService = new MultipleLanguageTranslationService( [ 'pl', 'de' ] ); const loadPoFileSpy = sandbox.stub( translationService, '_loadPoFile' ); @@ -97,6 +101,48 @@ describe( 'translations', () => { sinon.assert.calledTwice( loadPoFileSpy ); } ); + + it.only( 'should load all PO files for the current package and add languages to the language list', () => { + const translationService = new MultipleLanguageTranslationService( [], true ); + + const pathToTranslations = path.join( 'pathToPackage', 'lang', 'translations' ); + const pathToPlTranslations = path.join( pathToTranslations, 'pl.po' ); + const pathToDeTranslations = path.join( pathToTranslations, 'de.po' ); + + filesAndDirs = [ pathToPlTranslations, pathToDeTranslations, pathToTranslations ]; + + fileContents = { + [ pathToPlTranslations ]: [ + 'msgctxt "Label for the Save button."', + 'msgid "Save"', + 'msgstr "Zapisz"', + '' + ].join( '\n' ), + [ pathToDeTranslations ]: [ + 'msgctxt "Label for the Save button."', + 'msgid "Save"', + 'msgstr "Speichern"', + '' + ].join( '\n' ) + }; + + dirContents = { + [ pathToTranslations ]: [ 'pl.po', 'de.po' ] + }; + + translationService.loadPackage( 'pathToPackage' ); + + expect( translationService._dictionary ).to.deep.equal( { + pl: { + 'Save': 'Zapisz' + }, + de: { + 'Save': 'Speichern' + } + } ); + + expect( Array.from( translationService._languages ) ).to.deep.equal( [ 'pl', 'de' ] ); + } ); } ); describe( 'translateSource()', () => { @@ -260,7 +306,7 @@ describe( 'translations', () => { const pathToTranslations = path.join( 'custom', 'path', 'to', 'pathToPackage', 'en.po' ); - files = [ pathToTranslations ]; + filesAndDirs = [ pathToTranslations ]; fileContents = { [ pathToTranslations ]: [ @@ -287,7 +333,7 @@ describe( 'translations', () => { const pathToPlTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'pl.po' ); const pathToDeTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'de.po' ); - files = [ pathToPlTranslations, pathToDeTranslations ]; + filesAndDirs = [ pathToPlTranslations, pathToDeTranslations ]; fileContents = { [ pathToPlTranslations ]: [ diff --git a/packages/ckeditor5-dev-webpack-plugin/tests/index.js b/packages/ckeditor5-dev-webpack-plugin/tests/index.js index 1c5f9858b..e50ac2758 100644 --- a/packages/ckeditor5-dev-webpack-plugin/tests/index.js +++ b/packages/ckeditor5-dev-webpack-plugin/tests/index.js @@ -108,5 +108,22 @@ describe( 'webpack-plugin/CKEditorWebpackPlugin', () => { expect( stubs.serveTranslations.getCall( 0 ).args[ 2 ] ).to.be.instanceof( MultipleLanguageTranslationService ); expect( stubs.serveTranslations.getCall( 0 ).args[ 3 ] ).to.equal( stubs.ckeditor5EnvUtils ); } ); + + it( 'should serve `MultipleLanguageTranslationService` if the `languages` is set to `all`.', () => { + const options = { + languages: 'all' + }; + + const compiler = {}; + + const ckeditorWebpackPlugin = new CKEditorWebpackPlugin( options ); + ckeditorWebpackPlugin.apply( compiler ); + + sinon.assert.calledOnce( stubs.serveTranslations ); + expect( stubs.serveTranslations.getCall( 0 ).args[ 0 ] ).to.equal( compiler ); + expect( stubs.serveTranslations.getCall( 0 ).args[ 1 ] ).to.equal( options ); + expect( stubs.serveTranslations.getCall( 0 ).args[ 2 ] ).to.be.instanceof( MultipleLanguageTranslationService ); + expect( stubs.serveTranslations.getCall( 0 ).args[ 3 ] ).to.equal( stubs.ckeditor5EnvUtils ); + } ); } ); } );