Home > other >  How can I read ch-zh, ru, ja character in csv?
How can I read ch-zh, ru, ja character in csv?

Time:06-27

I draft a code to generate i18n.csv

const fs = require("fs")

async function main() {
  let table = [ 
    [ "en", "ch-zh", 'ru', 'ja' ],
    [ "Hello", "你好", "Привет", "こんにちは" ]
  ]

  fs.writeFileSync(__dirname  "/i18n.csv", array2csv(table), { encoding: "utf8", flag: 'a', mode: 0o666 })
}
main()

function array2csv(arr){
  arr = arr.map((col) => { return col.join(",") })
  arr = arr.join("\n")
  return arr
}

And I got the result as my expected in the file:

en,ch-zh,ru,ja
Hello,你好,Привет,こんにちは

But I cannot see the data if the characters are not a english character in excel, how can I fix it?

enter image description here

CodePudding user response:

Thanks all guys for the helps, it works now!

[Summary] Prepend a UTF-8 BOM in the csv file

const fs = require("fs")

async function main() {
  let table = [ 
    [ "en", "ch-zh", 'ru', 'ja', 'emoji' ],
    [ "Hello", "你好", "Привет", "こんにちは", '✅' ]
  ]

  fs.writeFileSync(__dirname  "/i18n.csv", array2csv(table), { encoding: "utf8", flag: 'a', mode: 0o666 })
}
main()

function array2csv(arr){
  arr = arr.map((col) => { return "\ufeff"   col.join(",") })
  arr = arr.join("\n")
  return arr
}

enter image description here

  • Related