Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Generator & Added Search add Add selected lang code to CSV column #11

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
trailingComma: 'none',
tabWidth: 2,
printWidth: 110,
semi: false,
singleQuote: true
// bracketSameLine: true,
}
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ You can also:
- [Extra Tools](#extra-tools)
- [Generate language switcher array code](#generate-language-switcher-array-code)
- [Search and List i18n Locale Codes](#search-and-list-i18n-locale-codes)
- [Search and Add i18n Locale Codes](#search-and-add-i18n-locale-codes)
- [Commands](#commands)
- [`generate`](#generate)
- [`create-csv`](#create-csv)
- [`lang-switcher`](#lang-switcher)
- [`list-codes`](#list-codes)
- [`parse`](#parse)
- [Example](#example)
- [`translate`](#translate)
- [`check-trans`](#check-trans)
- [`trans-clean`](#trans-clean)

## Getting Started

Expand Down Expand Up @@ -580,6 +586,21 @@ Basque (Spain), eu_ES

You can then copy and paste your language name and code straight into your CSV column header.


### Search and Add i18n Locale Codes

Don't know the locale code for a language? Just search for it and selected language name, code straight into `/translations.csv` column header:

```bash
$ quasalang list-codes --add

? Enter a search query (e.g. "russian") or hit Enter to list all codes: italian

? Select languages to add: Russian, ru, Russian (Moldova), ru_MD

CSV file successfully written with new lang codes: Russian, ru, Russian (Moldova), ru_MD.
```

## Commands

### `generate`
Expand Down Expand Up @@ -636,4 +657,74 @@ Options:
-h, --help display help for command
```

### `parse`

```
Usage: quasalang parse|p [options]

Parse your source files from (/src/**/*.{js,vue}) and Add them to (/translations.csv) as Default language

Options:
-h, --help display help for command
```

#### Example

Given the following label in your source files:

```html
<base-label>{{ $t("// Phone Number") }}</base-label>
or
<base-label>{{ $t("// label::Phone Number") }}</base-label>
```

The command will convert them to:

```html
<base-label>{{ $t("phoneNumber") }}</base-label>
or
<base-label>{{ $t("label.phoneNumber") }}</base-label>
```

The corresponding translation key will be added to `translations.csv`:

```
phoneNumber,"Phone Number"
or
label.phoneNumber,"Phone Number"
```

### `translate`

```
Usage: quasalang translate|t [options]

Translate your CSV file using Google translate

Options:
-h, --help display help for command
```

### `check-trans`

```
Usage: quasalang check-trans|ct [options]

Find missing trans key in (/translations.csv) from (/src/**/*.{js,vue})

Options:
-h, --help display help for command
```

### `trans-clean`

```
Usage: quasalang trans-clean|tc [options]

Remove unused trans key from (/translations.csv)

Options:
-h, --help display help for command
```


59 changes: 54 additions & 5 deletions commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const {
createCSV,
generate,
langSwitcher,
listCodes
listCodes,
translate,
parse,
checkTrans,
transClean
} = require('./index.js')

const helpText = `
Expand All @@ -26,7 +30,9 @@ Step 3. Generate your language files:

program
.version(pjson.version)
.description('Generate Quasar i18n language files from a CSV file. Run it from the root of a Quasar project.')
.description(
'Generate Quasar i18n language files from a CSV file. Run it from the root of a Quasar project.'
)
.addHelpText('after', helpText)

program
Expand All @@ -36,13 +42,53 @@ program
.option('-o, --output <mode>', 'Path to i18n output folder', 'src/i18n')
.option('-f, --force', 'Force write files (without prompt)', false)
.option('-nw, --nowatermark', 'Disable the watermark ("This file was auto-generated..") ', false)
.option('-ls, --lang-switcher', `Generate language switcher options array & output to console i.e. [{ label: 'English', value: 'en-US'}, ..]`, false)
.option(
'-ls, --lang-switcher',
`Generate language switcher options array & output to console i.e. [{ label: 'English', value: 'en-US'}, ..]`,
false
)
.option('-w, --watch', `Watch CSV file for changes & regenerate files`, false)
.description('Generate your i18n folder & all language files based on a CSV file')
.action((options) => {
generate(options)
})

program
.command('translate')
.alias('t')
.option('-f, --force', 'Force write files (without prompt)', false)
.description('Translate your CSV file using Google translate')
.action((options) => {
translate(options)
})

program
.command('parse')
.alias('p')
.option('-f, --force', 'Force write files (without prompt)', false)
.description(
'Parse your source files from (/src/**/*.{js,vue}) and Add them to (/translations.csv) as Default language'
)
.action((options) => {
parse(options)
})

program
.command('check-trans')
.alias('ct')
.description('Find missing trans key in (/translations.csv) from (/src/**/*.{js,vue})')
.action((options) => {
checkTrans(options)
})

program
.command('trans-clean')
.alias('tc')
.description('Remove unused trans key from (/translations.csv)')
.action((options) => {
transClean(options)
})

program
.command('create-csv')
.alias('c')
Expand All @@ -56,17 +102,20 @@ program
.command('lang-switcher')
.alias('ls')
.option('-i, --input <mode>', 'Path to input CSV', 'translations.csv')
.description(`Generate language switcher options array & output to console i.e. [{ label: 'English', value: 'en-US'}, ..]`)
.description(
`Generate language switcher options array & output to console i.e. [{ label: 'English', value: 'en-US'}, ..]`
)
.action((options) => {
langSwitcher(options)
})

program
.command('list-codes')
.alias('lc')
.option('-a, --add', 'Add locale codes to translations.csv', false)
.description(`Search & list i18n locale codes`)
.action((options) => {
listCodes(options)
})

program.parse(process.argv)
program.parse(process.argv)
51 changes: 51 additions & 0 deletions commands/check-trans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs')
const csv = require('csv-parser')
const glob = require('glob')
const { sourcePath, transPath } = require('./helpers')

const filteredRows = []

// Function to replace the pattern in a file
function replacePatternInFile(filePath) {
let content = fs.readFileSync(filePath, 'utf8')

const patternDoubleQuotes = /\$t\("(.*?)"\)/g
content = content.replace(patternDoubleQuotes, (match, value) => {
if (!filteredRows.find((item) => item.Key === value)) {
console.log(filePath, value)
}
})

const patternSingleQuotes = /\$t\('(.*?)'\)/g
content = content.replace(patternSingleQuotes, (match, value) => {
if (!filteredRows.find((item) => item.Key === value)) {
console.log(filePath, value)
}
})
}

// Function to find the pattern in all matching files
function findPatternInFiles(sourcePath) {
// Read the input CSV file
fs.createReadStream(transPath)
.pipe(csv()) // Skip header row
.on('data', (row) => {
filteredRows.push(row)
})

glob(`${sourcePath}/**/*.{vue,js}`, (err, files) => {
if (err) {
console.error('Error reading files:', err)
return
}
files.forEach((file) => {
replacePatternInFile(file)
})
})
}

module.exports = function () {
this.checkTrans = function (options) {
findPatternInFiles(sourcePath)
}
}
19 changes: 9 additions & 10 deletions commands/create-csv.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var fs = require('fs')

const { prompt } = require('inquirer')
const { transPath } = require('./helpers')

module.exports = function() {
this.createCSV = function(options) {
let outputPath = 'translations.csv'
module.exports = function () {
this.createCSV = function (options) {
let outputPath = transPath

// check if output file exists
if (fs.existsSync(outputPath)) {
Expand All @@ -15,26 +16,24 @@ module.exports = function() {
name: 'confirmOverwrite',
message: `File ${outputPath} exists. Overwrite it?`
}
]).then(answers => {
]).then((answers) => {
if (answers.confirmOverwrite) {
console.log('INFO: Skip this prompt in future with the --force (or -f) option.')
copyFile()
}
})
}
else {
} else {
copyFile()
}
}
else {
} else {
copyFile()
}

function copyFile() {
fs.copyFile(`${__dirname}/../sample-csv/translations.csv`, `${outputPath}`, (err) => {
if (err) throw err
console.log(`/translations.csv was generated.`)
console.log(`/${transPath} was generated.`)
})
}
}
}
}
Loading