56 lines
2.0 KiB
TypeScript
56 lines
2.0 KiB
TypeScript
import * as path from 'path'
|
|
import { CONFIG_DEFAULT } from '@plastichub/osr-commons'
|
|
import { resolve } from '@plastichub/osr-commons'
|
|
import { IOptions } from '@plastichub/osr-i18n/types'
|
|
import { translate } from '@plastichub/osr-i18n/lib/translate'
|
|
import { sanitize } from '@plastichub/osr-i18n/_cli'
|
|
import { isArray } from '@plastichub/core/primitives'
|
|
import { logger } from '../src'
|
|
import { option } from '../library'
|
|
import * as pMap from 'p-map'
|
|
|
|
const _translate = async (src, dst, options) => {
|
|
const i18nOptions = sanitize(
|
|
{
|
|
...options,
|
|
src: src,
|
|
dst: dst,
|
|
debug: options.debug,
|
|
verbose: options.verbose,
|
|
dry: options.dry
|
|
}
|
|
) as IOptions
|
|
|
|
if (!i18nOptions) {
|
|
logger.error('Invalid i18n options')
|
|
return
|
|
}
|
|
return await translate(i18nOptions)
|
|
}
|
|
|
|
const translateAll = async (files, options, dst: string) => {
|
|
return pMap(files, async (f) => _translate(f, dst, options), { concurrency: 1 })
|
|
}
|
|
|
|
export const register = (grunt) => {
|
|
grunt.registerMultiTask('i18n', 'translate files via osr-i18n', async function () {
|
|
const done = this.async();
|
|
const task_options = this.data.options || {}
|
|
const config: any = CONFIG_DEFAULT()
|
|
const options = {
|
|
...this.data.options,
|
|
root: option('root', task_options, grunt, path.resolve('./')),
|
|
debug: option('debug', task_options, grunt , false),
|
|
logLevel: option('logLevel', task_options, grunt , 'warn'),
|
|
api_key: config.deepl.auth_key
|
|
}
|
|
logger.setSettings({ minLevel: options.logLevel })
|
|
let src: string[] = isArray(this.data.src) ? this.data.src : [this.data.src]
|
|
src = src.map(f => (resolve(f)))
|
|
logger.debug('Translate - task data ', this.data)
|
|
await translateAll(src, options, this.data.options.dst)
|
|
logger.info(`Translate - task done : \n\t ${src.join('\n\t')}`)
|
|
done()
|
|
})
|
|
}
|