← 全部工具

detect-file-encoding-and-language

热度 65 更新于 效率与自动化

Charset Detector - Detect the encoding and language of text files - Use it in the browser, with Node.js, or via CLI

npmauto-collected

安装

npm
npm install -g detect-file-encoding-and-language

通过 npm 安装。

Detect-File-Encoding-And-Language

Functionality

Determine the encoding and language of text files!

  • Detects 40 languages as well as the appropriate encoding
  • Available as CLI, in Node.js and in the browser
  • Supports .txt, .srt, .sub, .html, .csv, .tsv
  • Works best with large inputs
  • Completely free, no API key required

For reliable encoding and language detection, use files containing at least 500 words of coherent text. Smaller inputs can work as well but the results might be less accurate and in some cases incorrect.

Live Demo

Feel free to test the functionality of this NPM package here. Upload your own files and see if the encoding and language are detected correctly!

Installation

npm install detect-file-encoding-and-language

Usage

Via CDN

// index.html
<body>
  <input type="file" id="my-input-field" />
  <script src="https://unpkg.com/detect-file-encoding-and-language/umd/language-encoding.min.js"></script>
  <script src="app.js"></script>
</body>

// app.js
document.getElementById("my-input-field").addEventListener("change", (e) => {
  const file = e.target.files[0];
  languageEncoding(file).then((fileInfo) => console.log(fileInfo));
  // Possible result: { language: english, encoding: UTF-8, confidence: { encoding: 1, language: 1 } }

If you don't want to use a CDN feel free to [download the source code](https://github.com/gignupg/Detect-File-Encoding-and-Language/wiki/Downloading-the-Source-Code)!

### In React

// App.js import languageEncoding from "detect-file-encoding-and-language"; export default function App() { function inputHandler(e) { const file = e.target.files[0]; languageEncoding(file).then((fileInfo) = console.log(fileInfo)); // Possible result: { language: english, encoding: UTF-8, confidence: { encoding: 1, language: 1 } } } return <input type="file" onChange={inputHandler} /; }


### In Node
#### File

// server.js const languageEncoding = require("detect-file-encoding-and-language"); const pathToFile = "/home/username/documents/my-text-file.txt"; languageEncoding(pathToFile).then((fileInfo) = console.log(fileInfo)); // Possible result: { language: japanese, encoding: Shift-JIS, confidence: { encoding: 0.94, language: 0.94 } }


#### Buffer

// server.js const languageEncoding = require("detect-file-encoding-and-language"); const content = Buffer.from("file content"); languageEncoding(content).then((fileInfo) = console.log(fileInfo)); // Possible result: { language: japanese, encoding: Shift-JIS, confidence: { encoding: 0.94, language: 0.94 } }