|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { existsSync, mkdirSync, writeFileSync } = require( 'fs' ); |
|
|
const { relative, sep } = require( 'path' ); |
|
|
const { po } = require( 'gettext-parser' ); |
|
|
const { merge, isEmpty, forEach } = require( 'lodash' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_HEADERS = { |
|
|
'content-type': 'text/plain; charset=UTF-8', |
|
|
'x-generator': 'babel-plugin-i18n-calypso', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_DIR = 'build/'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_FUNCTIONS_ARGUMENTS_ORDER = { |
|
|
__: [], |
|
|
_n: [ 'msgid_plural' ], |
|
|
_x: [ 'msgctxt' ], |
|
|
_nx: [ 'msgid_plural', null, 'msgctxt' ], |
|
|
translate: [ 'msgid_plural', 'options_object' ], |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const REGEXP_TRANSLATOR_COMMENT = /^\s*translators:\s*([\s\S]+)/im; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getExtractedComment( path, _originalNodeLine ) { |
|
|
const { node, parent, parentPath } = path; |
|
|
|
|
|
|
|
|
|
|
|
if ( ! _originalNodeLine ) { |
|
|
_originalNodeLine = node.loc.start.line; |
|
|
} |
|
|
|
|
|
let comment; |
|
|
forEach( node.leadingComments, ( commentNode ) => { |
|
|
if ( ! commentNode.loc ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const { line } = commentNode.loc.end; |
|
|
if ( line < _originalNodeLine - 1 || line > _originalNodeLine ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const match = commentNode.value.match( REGEXP_TRANSLATOR_COMMENT ); |
|
|
if ( match ) { |
|
|
|
|
|
comment = match[ 1 ] |
|
|
.split( '\n' ) |
|
|
.map( ( text ) => text.trim() ) |
|
|
.join( ' ' ); |
|
|
|
|
|
|
|
|
return false; |
|
|
} |
|
|
} ); |
|
|
|
|
|
if ( comment ) { |
|
|
return comment; |
|
|
} |
|
|
|
|
|
if ( ! parent || ! parent.loc || ! parentPath ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const { line } = parent.loc.start; |
|
|
if ( line >= _originalNodeLine - 1 && line <= _originalNodeLine ) { |
|
|
return getExtractedComment( parentPath, _originalNodeLine ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getNodeAsString( node ) { |
|
|
if ( undefined === node ) { |
|
|
return ''; |
|
|
} |
|
|
|
|
|
switch ( node.type ) { |
|
|
case 'BinaryExpression': |
|
|
return getNodeAsString( node.left ) + getNodeAsString( node.right ); |
|
|
|
|
|
case 'StringLiteral': |
|
|
return node.value; |
|
|
|
|
|
case 'TemplateLiteral': |
|
|
return ( node.quasis || [] ).reduce( ( string, element ) => { |
|
|
return ( string += element.value.cooked ); |
|
|
}, '' ); |
|
|
|
|
|
default: |
|
|
return ''; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isValidFunctionName( name ) { |
|
|
return Object.keys( DEFAULT_FUNCTIONS_ARGUMENTS_ORDER ).includes( name ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isValidTranslationKey( key ) { |
|
|
return Object.values( DEFAULT_FUNCTIONS_ARGUMENTS_ORDER ).some( ( args ) => |
|
|
args.includes( key ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function mergeStrings( source, target ) { |
|
|
if ( ! source.comments.reference.includes( target.comments.reference ) ) { |
|
|
source.comments.reference += '\n' + target.comments.reference; |
|
|
} |
|
|
|
|
|
if ( ! source.comments.extracted ) { |
|
|
source.comments.extracted = target.comments.extracted; |
|
|
} else if ( |
|
|
target.comments.extracted && |
|
|
! source.comments.extracted.includes( target.comments.extracted ) |
|
|
) { |
|
|
source.comments.extracted += '\n' + target.comments.extracted; |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! source.hasOwnProperty( 'msgid_plural' ) && target.hasOwnProperty( 'msgid_plural' ) ) { |
|
|
source.msgid_plural = target.msgid_plural; |
|
|
source.msgstr = target.msgstr; |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = function () { |
|
|
let strings = {}; |
|
|
let nplurals = 2; |
|
|
let baseData; |
|
|
let functions = { ...DEFAULT_FUNCTIONS_ARGUMENTS_ORDER }; |
|
|
|
|
|
return { |
|
|
visitor: { |
|
|
ImportDeclaration( path ) { |
|
|
|
|
|
|
|
|
if ( 'i18n-calypso' !== path.node.source.value ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
path.node.specifiers.forEach( ( specifier ) => { |
|
|
if ( specifier.imported && 'translate' === specifier.imported.name && specifier.local ) { |
|
|
functions[ specifier.local.name ] = functions.translate; |
|
|
} |
|
|
} ); |
|
|
}, |
|
|
|
|
|
CallExpression( path, state ) { |
|
|
const { callee } = path.node; |
|
|
|
|
|
|
|
|
let name; |
|
|
if ( 'MemberExpression' === callee.type ) { |
|
|
name = callee.property.loc ? callee.property.loc.identifierName : callee.property.name; |
|
|
} else { |
|
|
name = callee.loc ? callee.loc.identifierName : callee.name; |
|
|
} |
|
|
if ( ! isValidFunctionName( name ) ) { |
|
|
return; |
|
|
} |
|
|
let i = 0; |
|
|
|
|
|
const translation = { |
|
|
msgid: getNodeAsString( path.node.arguments[ i++ ] ), |
|
|
msgstr: '', |
|
|
comments: {}, |
|
|
}; |
|
|
|
|
|
if ( ! translation.msgid.length ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( ! baseData ) { |
|
|
baseData = { |
|
|
charset: 'utf-8', |
|
|
headers: state.opts.headers || DEFAULT_HEADERS, |
|
|
translations: { |
|
|
'': { |
|
|
'': { |
|
|
msgid: '', |
|
|
msgstr: [], |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}; |
|
|
|
|
|
for ( const key in baseData.headers ) { |
|
|
baseData.translations[ '' ][ '' ].msgstr.push( |
|
|
`${ key }: ${ baseData.headers[ key ] };\n` |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
const pluralsMatch = ( baseData.headers[ 'plural-forms' ] || '' ).match( |
|
|
/nplurals\s*=\s*(\d+);/ |
|
|
); |
|
|
if ( pluralsMatch ) { |
|
|
nplurals = pluralsMatch[ 1 ]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const translator = getExtractedComment( path ); |
|
|
if ( translator ) { |
|
|
translation.comments.extracted = translator; |
|
|
} |
|
|
|
|
|
const { filename } = this.file.opts; |
|
|
const base = state.opts.base || '.'; |
|
|
const pathname = relative( base, filename ).split( sep ).join( '/' ); |
|
|
translation.comments.reference = pathname + ':' + path.node.loc.start.line; |
|
|
|
|
|
const functionKeys = state.opts.functions || functions[ name ]; |
|
|
|
|
|
if ( functionKeys ) { |
|
|
path.node.arguments.slice( i ).forEach( ( arg, index ) => { |
|
|
const key = functionKeys[ index ]; |
|
|
|
|
|
if ( 'ObjectExpression' === arg.type ) { |
|
|
arg.properties.forEach( ( property ) => { |
|
|
if ( 'ObjectProperty' !== property.type ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( 'context' === property.key.name ) { |
|
|
translation.msgctxt = property.value.value; |
|
|
} |
|
|
|
|
|
if ( 'comment' === property.key.name ) { |
|
|
translation.comments.extracted = property.value.value; |
|
|
} |
|
|
} ); |
|
|
} else if ( isValidTranslationKey( key ) ) { |
|
|
translation[ key ] = getNodeAsString( arg ); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
if ( ( translation.msgid_plural || '' ).length ) { |
|
|
translation.msgstr = Array.from( Array( nplurals ) ).map( () => '' ); |
|
|
} |
|
|
|
|
|
|
|
|
const { msgctxt = '', msgid } = translation; |
|
|
if ( ! strings.hasOwnProperty( msgctxt ) ) { |
|
|
strings[ msgctxt ] = {}; |
|
|
} |
|
|
|
|
|
if ( ! strings[ msgctxt ].hasOwnProperty( msgid ) ) { |
|
|
strings[ msgctxt ][ msgid ] = translation; |
|
|
} else { |
|
|
mergeStrings( strings[ msgctxt ][ msgid ], translation ); |
|
|
} |
|
|
}, |
|
|
Program: { |
|
|
enter() { |
|
|
strings = {}; |
|
|
functions = { ...DEFAULT_FUNCTIONS_ARGUMENTS_ORDER }; |
|
|
}, |
|
|
exit( path, state ) { |
|
|
if ( isEmpty( strings ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const data = merge( {}, baseData, { translations: strings } ); |
|
|
|
|
|
const compiled = po.compile( data ); |
|
|
|
|
|
const dir = state.opts.dir || DEFAULT_DIR; |
|
|
! existsSync( dir ) && mkdirSync( dir, { recursive: true } ); |
|
|
|
|
|
const { filename } = this.file.opts; |
|
|
const base = state.opts.base || '.'; |
|
|
const pathname = relative( base, filename ).split( sep ).join( '-' ); |
|
|
writeFileSync( dir + pathname + '.pot', compiled ); |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}; |
|
|
}; |
|
|
|