Skip to content

Commit

Permalink
fix: respect --cwd argument when generating/extracting translations (#…
Browse files Browse the repository at this point in the history
…540)

* fix: show relative path when logging i18n strings output destination

* fix: respect --cwd argument when generating/extracting translations

* fix: keep existing i18n options for backwards compatibility
  • Loading branch information
mediremi committed Apr 27, 2021
1 parent adc56a3 commit 280e02c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
7 changes: 6 additions & 1 deletion cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ const handler = async ({
}

reporter.info('Generating internationalization strings...')
await i18n.extract({ input: paths.src, output: paths.i18nStrings })
await i18n.extract({
input: paths.src,
output: paths.i18nStrings,
paths,
})
await i18n.generate({
input: paths.i18nStrings,
output: paths.i18nLocales,
namespace: 'default',
paths,
})

if (config.type === 'app') {
Expand Down
25 changes: 14 additions & 11 deletions cli/src/commands/i18n.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { namespace, reporter } = require('@dhis2/cli-helpers-engine')
const i18n = require('../lib/i18n')
const makePaths = require('../lib/paths')

const generate = {
description:
Expand All @@ -9,24 +10,25 @@ const generate = {
alias: 'p',
description:
'directory path to find .po/.pot files and convert to JSON',
default: './i18n/',
},
output: {
alias: 'o',
description: 'Output directory to place converted JSON files.',
default: './src/locales/',
},
namespace: {
alias: 'n',
description: 'Namespace for app locale separation',
default: 'default',
},
},
handler: async argv => {
handler: async ({ cwd, path, output, namespace }) => {
const paths = makePaths(cwd)

const result = await i18n.generate({
input: argv.path,
output: argv.output,
namespace: argv.namespace,
input: path || paths.i18nStrings,
output: output || paths.i18nLocales,
namespace,
paths,
})

if (!result) {
Expand All @@ -43,18 +45,19 @@ const extract = {
alias: 'p',
description:
'Directory path to recurse and extract i18n.t translation strings',
default: './src/',
},
output: {
alias: 'o',
description: 'Destination path for en.pot file',
default: './i18n/',
},
},
handler: async argv => {
handler: async ({ cwd, path, output }) => {
const paths = makePaths(cwd)

const result = await i18n.extract({
input: argv.path,
output: argv.output,
input: path || paths.src,
output: output || paths.i18nStrings,
paths,
})
if (!result) {
reporter.error('Failed to extract i18n strings.')
Expand Down
7 changes: 6 additions & 1 deletion cli/src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ const handler = async ({
}

reporter.info('Generating internationalization strings...')
await i18n.extract({ input: paths.src, output: paths.i18nStrings })
await i18n.extract({
input: paths.src,
output: paths.i18nStrings,
paths,
})
await i18n.generate({
input: paths.i18nStrings,
output: paths.i18nLocales,
namespace: 'default',
paths,
})

reporter.info('Bootstrapping local appShell...')
Expand Down
6 changes: 3 additions & 3 deletions cli/src/lib/i18n/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { i18nextToPot, gettextToI18next } = require('i18next-conv')
const scanner = require('i18next-scanner')
const { checkDirectoryExists, walkDirectory, arrayEqual } = require('./helpers')

const extract = async ({ input, output }) => {
const relativeInput = './' + path.relative(process.cwd(), input)
const extract = async ({ input, output, paths }) => {
const relativeInput = './' + path.relative(paths.base, input)
if (!checkDirectoryExists(input)) {
reporter.error(
`I18n source directory ${chalk.bold(relativeInput)} does not exist.`
Expand Down Expand Up @@ -73,7 +73,7 @@ const extract = async ({ input, output }) => {
reporter.print(
chalk.dim(
`Writing ${Object.keys(en).length} language strings to ${chalk.bold(
'./' + targetPath
'./' + path.relative(paths.base, targetPath)
)}...`
)
)
Expand Down
4 changes: 2 additions & 2 deletions cli/src/lib/i18n/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const writeTemplate = (outFile, data) => {
fs.writeFileSync(outFile, localesTemplate(data))
}

const generate = async ({ input, output, namespace }) => {
const generate = async ({ input, output, namespace, paths }) => {
if (!checkDirectoryExists(input)) {
const relativeInput = './' + path.relative(process.cwd(), input)
const relativeInput = './' + path.relative(paths.base, input)
reporter.debug(
`Source directory ${chalk.bold(
relativeInput
Expand Down

0 comments on commit 280e02c

Please sign in to comment.