@ckeditor/ckeditor5-dev-utils
Advanced tools
Comparing version 2.5.1 to 2.6.0
@@ -22,5 +22,9 @@ /** | ||
* @param {String} language Target language. | ||
* @param {Object} [options] Optional config. | ||
* @param {Function} [options.getPathToPoFile] Function that return a full path to the po file. | ||
*/ | ||
constructor( language ) { | ||
constructor( language, options = {} ) { | ||
this.language = language; | ||
this.getPathToPoFile = options.getPathToPoFile || getDefaultPathToPoFile; | ||
this.packagePaths = new Set(); | ||
@@ -42,3 +46,3 @@ this.dictionary = new Map(); | ||
const pathToPoFile = path.join( pathToPackage, 'lang', 'translations', this.language + '.po' ); | ||
const pathToPoFile = this.getPathToPoFile( pathToPackage, this.language ); | ||
@@ -49,3 +53,3 @@ this._loadPoFile( pathToPoFile ); | ||
/** | ||
* Parses source, translates t call arguments and returns modified output. | ||
* Parses source, translates `t()` call arguments and returns modified output. | ||
* | ||
@@ -101,2 +105,4 @@ * @param {String} source JS source text which will be translated. | ||
if ( !fs.existsSync( pathToPoFile ) ) { | ||
console.warn( `PO file doesn't exist under the path: ${ pathToPoFile }.` ); | ||
return; | ||
@@ -126,1 +132,5 @@ } | ||
}; | ||
function getDefaultPathToPoFile( pathToPackage, languageCode ) { | ||
return path.join( pathToPackage, 'lang', 'translations', languageCode + '.po' ); | ||
} |
{ | ||
"name": "@ckeditor/ckeditor5-dev-utils", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"description": "Utils for CKEditor 5 development tools packages.", | ||
@@ -31,3 +31,3 @@ "keywords": [], | ||
"bugs": "https://github.com/ckeditor/ckeditor5-dev/issues", | ||
"repository": "https://github.com/ckeditor/ckeditor5-dev/tree/master/packages/ckeditor5-dev-utils" | ||
"repository": "https://github.com/ckeditor/ckeditor5-dev" | ||
} |
@@ -20,3 +20,3 @@ /** | ||
const sandbox = sinon.sandbox.create(); | ||
let translationService, stubs; | ||
let TranslationService, stubs; | ||
let files, fileContents; | ||
@@ -45,7 +45,5 @@ | ||
const TranslationService = proxyquire( '../../lib/translations/translationservice', { | ||
TranslationService = proxyquire( '../../lib/translations/translationservice', { | ||
'../logger': () => stubs.logger, | ||
} ); | ||
translationService = new TranslationService( 'pl' ); | ||
} ); | ||
@@ -58,4 +56,32 @@ | ||
describe( 'constructor()', () => { | ||
it( 'should be able to use custom function that returns path to the po file', () => { | ||
const pathToTranslations = path.join( 'customPathToPackage', 'lang', 'translations', 'pl.po' ); | ||
files = [ pathToTranslations ]; | ||
fileContents = { | ||
[ pathToTranslations ]: [ | ||
`msgctxt "Label for the Save button."`, | ||
`msgid "Save"`, | ||
`msgstr "Zapisz"`, | ||
'' | ||
].join( '\n' ) | ||
}; | ||
const translationService = new TranslationService( 'pl', { | ||
getPathToPoFile: ( pathToPackage, languageCode ) => path.join( pathToPackage, 'lang', 'translations', `${ languageCode }.po` ) | ||
} ); | ||
translationService.loadPackage( 'customPathToPackage' ); | ||
expect( Array.from( translationService.dictionary ) ).to.deep.equal( [ | ||
[ 'Save', 'Zapisz' ] | ||
] ); | ||
} ); | ||
} ); | ||
describe( 'loadPackage()', () => { | ||
it( 'should load po file from the package and load translations', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
const pathToTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'pl.po' ); | ||
@@ -82,2 +108,4 @@ | ||
it( 'should do nothing if the po file does not exist', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
files = []; | ||
@@ -92,3 +120,5 @@ fileContents = {}; | ||
it( 'should load po file from the package only once', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
const loadPoFileSpy = sinon.spy(); | ||
sandbox.stub( translationService, '_loadPoFile', loadPoFileSpy ); | ||
@@ -105,2 +135,3 @@ | ||
it( 'should translate t() calls in the code', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
const source = `t( 'Cancel' )`; | ||
@@ -116,2 +147,3 @@ | ||
it( 'should return original source if there is no t() calls in the code', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
const source = `translate( 'Cancel' )`; | ||
@@ -125,3 +157,5 @@ | ||
it( 'should lg the error and keep original string if the translation misses', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
const source = `t( 'Cancel' )`; | ||
const result = translationService.translateSource( source ); | ||
@@ -135,2 +169,3 @@ | ||
it( 'should throw an error when the t is called with the variable', () => { | ||
const translationService = new TranslationService( 'pl' ); | ||
const source = `const cancel = 'Cancel';t( cancel );`; | ||
@@ -137,0 +172,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
96777
32
2489