---
title: "Detect the language of text in C#"
url: https://recursiveai.org/onenlp/cookbook/language-detection/
description: "Identify a string’s language locally with the model included in OneNLP."
version: "1.0.0"
---

# Detect the language of text in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/language-detection/language-detection.zip)

Identify a string’s language locally with the model included in OneNLP.

```shell
dotnet add package RecursiveAI.OneNlp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneNlp;

Console.WriteLine(Nlp.DetectLanguage(
    "Bonjour, comment allez-vous ?").Code);
```

The detector returns a language; `.Code` prints its language code.

```text
fr
```

Unzip [the example](https://recursiveai.org/onenlp/cookbook/language-detection/language-detection.zip), then `dotnet run`.

## The full program

The download’s `Program.cs` uses the same visible input as the snippet at the top. No input files are needed.

The download’s `Program.cs` is the snippet above.

`dotnet run` prints:

```text
fr
```

Null input raises `ArgumentNullException`; input beyond `Nlp.MaxLength` raises `ArgumentOutOfRangeException`.

## Variations

**Test text with no letters.** Digits and punctuation alone return `Language.Unknown`, not a guessed language. This is a standalone replacement for `Program.cs`.

```csharp
using RecursiveAI.OneNlp;

Console.WriteLine(Nlp.DetectLanguage("123?!") == Language.Unknown);
```

```text
True
```

## Related recipes

- [Named entity recognition in C#](https://recursiveai.org/onenlp/cookbook/named-entity-recognition/)
- [Tokenize text in C#](https://recursiveai.org/onenlp/cookbook/tokenize-text/)
- [Split text into sentences in C#](https://recursiveai.org/onenlp/cookbook/split-sentences/)
- [Lemmatize text in C#](https://recursiveai.org/onenlp/cookbook/lemmatize-text/)

---

## The short answer

Call `Nlp.DetectLanguage(text)` and read the returned `Language.Code`. The French greeting in the snippet at the top prints `fr`. You don’t need a language-specific model package for detection: the identifier is embedded in `RecursiveAI.OneNlp`. It identifies natural languages, not programming languages, and it doesn’t translate the input.

## How to detect language from text?

Pass the original string to the detector rather than guessing from a few accented characters. The [language identification documentation](https://recursiveai.org/onenlp/docs/language-id/) describes a classifier using character sequences and script information. Its supported set contains 75 languages. That is the detector’s set, not a promise that every language has an entity or lemmatization model.

`Nlp.DetectLanguage` returns the most likely supported language. It isn’t a general “is this valid French?” test. In particular, related languages share words, and names or isolated words may not provide the context your application needs. Test the kinds of strings you actually receive before using the answer to route important work. Preserve the original text alongside its detected code so a later processing stage can reconsider the choice.

Digits, whitespace and punctuation are ignored. When there are no letters of a supported script, the answer is `Language.Unknown`; the variation above shows that case explicitly. Treat it as a separate routing outcome rather than interpreting it as English. Calling other high-level NLP verbs can have different fallback behavior, so keep identification separate when an unknown result matters.

The lower-level `LanguageDetector` in `RecursiveAI.OneNlp.LanguageId` also exposes `Confidences`, `Restrict` and `DetectSpans`. Confidence candidates are restricted to languages that use the text’s script, and their probabilities sum to one. These are model probabilities over its candidates, not an assurance that an arbitrary input belongs to a supported language. `Restrict` is appropriate when your application genuinely knows the allowed languages, not simply to force an answer you prefer.

For the most likely candidates and their probabilities, call `LanguageDetector.Default.Confidences`. This standalone program prints the first three candidates for the same greeting:

```csharp
using RecursiveAI.OneNlp.LanguageId;
using System.Globalization;

var candidates = LanguageDetector.Default.Confidences(
    "Bonjour, comment allez-vous ?");
foreach (var candidate in candidates.Take(3))
    Console.WriteLine($"{candidate.Language.Code}: " +
        candidate.Confidence.ToString("F4", CultureInfo.InvariantCulture));
```

```text
fr: 0.9986
en: 0.0005
nl: 0.0002
```

## How do I detect the language of a string in C#?

The complete program is the same small call shown at the top. Its only input is the visible greeting, so you can replace that literal with a string from your application without preparing a file or obtaining a service key. The published package contains the detection model; running the program does not fetch weights.

For a document containing more than one language, one whole-string result cannot describe every passage. The documented `DetectSpans` method returns language runs with offsets covering the text. Its switching penalty controls how readily it changes language between words. That is a different decision from choosing a single label for an entire document.

Detection does not automatically install downstream language models. If you next need entities or lemmas, add the appropriate package described in [model packages](https://recursiveai.org/onenlp/docs/model-packages/). Keep these deployment decisions explicit: a successful language ID result alone does not mean a model-dependent operation will succeed.
