Created
September 11, 2021 22:14
-
-
Save zholmes1/f164a4e68afc6cb789a4a2fa877713e3 to your computer and use it in GitHub Desktop.
React Native Translation System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writeFileSync } from 'fs' | |
import { map } from 'lodash' | |
import en from './src/localize/en' | |
const lines = map(en, (text, key) => { | |
return `"${key}","${text}"` | |
}) | |
writeFileSync('english.csv', lines.join('\n')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readdirSync, readFileSync, writeFileSync } from 'fs' | |
import { trim } from 'lodash' | |
import { join } from 'path' | |
const inDir = join('./translations') | |
const files = readdirSync(inDir) | |
for (const file of files) { | |
const path = join(inDir, file) | |
const outPath = join( | |
'./src/localize', | |
file.toLocaleLowerCase().replace('csv', 'ts') | |
) | |
const filename = file.split('.')[0].replace('-', '') | |
const lines = readFileSync(path, 'utf-8') | |
.split('\n') | |
.filter(line => line.length > 0) | |
let out = `import { LanguageFile } from './en'\n\nconst ${filename}: LanguageFile = {` | |
for (let i = 0; i < lines.length; i++) { | |
const line = lines[i] | |
const commaIndex = line.indexOf(',') | |
const key = line.substring(0, commaIndex) | |
const value = trim(line.substring(commaIndex + 1), '"') | |
out += `\n ${key}: "${value}"` | |
if (i < lines.length - 1) { | |
out += ',' | |
} | |
} | |
out += `}\n\nexport default ${filename}` | |
writeFileSync(outPath, out) | |
console.log(path, outPath) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const en = { | |
hello_world: 'Hello, world!', | |
} | |
export type LanguageFile = typeof en | |
export default en |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment