0

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

Yuu
  • 159
  • 1
  • 10
  • 1
    You might want to prepend a UTF-8 BOM, to your CSV file output, Excel is likely just defaulting to UTF-16 – Keith Jun 23 '22 at 09:31
  • 1
    You can try this https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-open-csv-file-encoded-with-utf-8-with-excel/fb21388a-74c4-44b5-b1b4-665faf3dd81a – jabaa Jun 23 '22 at 09:32
  • 1
    This might help also -> https://stackoverflow.com/questions/13859218/nodejs-how-to-make-function-fs-writefile-write-with-bom – Keith Jun 23 '22 at 09:34
  • 1
    Or https://stackoverflow.com/questions/41849028/csv-in-utf-8-and-microsoft-excel It also mentions the problem with the BOM. – jabaa Jun 23 '22 at 09:35
  • Oh Nice! Thanks all a lot! now I know I need to add UTF-8 BOM and its works as @jabaa answer! – Yuu Jun 23 '22 at 09:50

1 Answers1

0

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

Yuu
  • 159
  • 1
  • 10