@tonyptang/i18n-helper
v1.27.0
Published
```ts // ih.config.ts import { defineConfig } from '@tonyptang/i18n-helper'
Readme
Run script ih gen to generate language config
// ih.config.ts
import { defineConfig } from '@tonyptang/i18n-helper'
export default defineConfig({
filename: 'lang.xlsx',
sheetNames: ['sheet1', 'sheet2'],
keyColumn: 'B',
columnToLanguageFile: {
C: 'cn.json',
D: 'en.json',
},
sheetNameToModule: { // Default is the sheetName
sheet1: 'common',
sheet2: 'sheet2'
},
savePath: './src/example/langs',
parseValue(value, key) {
return JSON.stringify(value)
},
checkDuplicateValue: 'C', // Check for duplicate values on a column; string | boolean
checkDuplicateKey: true,
ignoreRows: [1, 2], // start with 1
})import type { Awaitable } from './utils'
// example: A 、B、AA...
type CheckColumn = string
type IgnoreRowsFn = (moduleName: string | symbol) => Promise<number[]> | number[]
export interface UserConfig {
filename: string
outputFileType?: 'json' | 'ts' | 'js' | 'yaml'
sheetNames?: string[]
keyColumn: string
columnToLanguageFile: Record<string, string>
ignoreRows?: number[] | IgnoreRowsFn
sheetNameToModule?: Record<string, string>
/**
* @deprecated Use "dir" instead
*/
savePath?: string
dir?: string
/**
* @description If "configured and strategy" is used, the configuration file will be generated by "pick" and "omit"
*/
strategy?: 'insert new key' | 'rewrite all' | 'configured and strategy'
/**
*
* @description After generating the configuration, before writing the file
*/
afterGenerate?: (config: Record<string, any>) => Awaitable<void | Record<string, any>>
/**
* @default true
*/
trimKey?: boolean
transformKey?: (key: string, moduleName: string | symbol) => string | boolean | void
/**
*
* @deprecated use transformValue instead
*/
parseValue?: (value: string, key: string, moduleName: string | symbol) => string
transformValue?: (value: string, key: string, moduleName: string | symbol) => string
/**
* @default true
* @description Check for duplicate values
*/
checkDuplicateValue?: boolean | CheckColumn
/**
* @default true
* @description Check for duplicate keys
*/
checkDuplicateKey?: boolean
beforeGenerate?: () => Promise<void> | void
/**
* @description Output the generated configuration to a file,
* If the value is true, then the output will be to "{dir}/index.json"
*/
singleFile?: boolean | string
/**
* @example ['module1.key1', 'module2.*']
*/
pick?: string[]
/**
* @example ['module2.key1']
*/
omit?: string[]
}
type ConfigFn = () => Promise<UserConfig> | UserConfig
type Config = ConfigFn | UserConfig
type Arrayable<T> = T | T[]
export async function defineConfig(config: Arrayable<Config>) {
const _config = Array.isArray(config) ? config : [config]
const result: Config[] = []
for await (const c of _config) {
const realConfig = typeof c === 'function' ? await c() : c
result.push(realConfig)
}
return result
}Run the script ih set-config 'filename(en.json)' to set the target multi-language configuration
Then run ih 'filename(js|ts|vue)' to replace the multilingual text in the file
Or run ih 'filename(js|ts|vue)' --i18n-config 'filename(en.json)'
example
// en.json
{
"testKey": "this is a test",
"testKey2": {
"value": "this is a test2"
}
}// index.vue
<script setup lang="ts">
const message = ref('this is a test')
</script>
<template>
<div>this is a test2</div>
</template>
Run ih --i18n-config en.json index.vue
output
// index.vue
<script setup lang="ts">
const message = ref(t('testKey'))
</script>
<template>
<div>{{$t('testKey2.value')}}</div>
</template>
