Bendangelo Blog

Using whichlang.cr for language detection

Apr 13, 2025 1 minute read

whichlang.cr provides Crystal bindings for Rust’s whichlang language detection library. It detects the language of a given text.

Supported languages

  • Arabic (Ara)
  • Chinese (Cmn)
  • German (Deu)
  • English (Eng)
  • French (Fra)
  • Hindi (Hin)
  • Italian (Ita)
  • Japanese (Jpn)
  • Korean (Kor)
  • Dutch (Nld)
  • Portuguese (Por)
  • Russian (Rus)
  • Spanish (Spa)
  • Swedish (Swe)
  • Turkish (Tur)
  • Vietnamese (Vie)
  • Unknown (for undetected text)

Requirements

  • Linux or Mac
  • Rust 1.56 or later

Installation

Add this to your shard.yml:

dependencies:  
  whichlang:  
    github: bendangelo/whichlang.cr  

Then run:

shards install  

Usage

require "whichlang"  

input = "this is the best day ever"  
Whichlang.detect(input) # => Whichlang::Lang::Eng  

# Returns nil for empty or undetectable text  
input = " "  
Whichlang.detect?(input) # => nil  

That’s it. You now have language detection in your Crystal app.

Related Posts