> This page is for version Latest (default).
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/nemo/guardrails/latest/llms.txt
> - v0.25.0: https://docs.nvidia.com/nemo/guardrails/v0.25.0/llms.txt
> - v0.24.1: https://docs.nvidia.com/nemo/guardrails/v0.24.1/llms.txt
> - v0.24.0: https://docs.nvidia.com/nemo/guardrails/v0.24.0/llms.txt
> - v0.23.0: https://docs.nvidia.com/nemo/guardrails/v0.23.0/llms.txt
> - v0.22.0: https://docs.nvidia.com/nemo/guardrails/v0.22.0/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.

# nemoguardrails.library.utils.language_detection

Shared language-detection utilities for rails.

Detection is powered by fast-langdetect (the fastText `lid.176` model),
which supports the 176 language codes listed in `FASTTEXT_LANGUAGES`:
[https://fasttext.cc/docs/en/language-identification.html](https://fasttext.cc/docs/en/language-identification.html)

## Module Contents

### Functions

| Name                                                                                          | Description                                          |
| --------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| [`detect_language`](#nemoguardrails-library-utils-language_detection-detect_language)         | Detect the language of `text` using fast-langdetect. |
| [`get_refusal_message`](#nemoguardrails-library-utils-language_detection-get_refusal_message) | Return the refusal message for `lang`.               |

### Data

[`DEFAULT_REFUSAL_MESSAGES`](#nemoguardrails-library-utils-language_detection-DEFAULT_REFUSAL_MESSAGES)

[`SUPPORTED_LANGUAGES`](#nemoguardrails-library-utils-language_detection-SUPPORTED_LANGUAGES)

[`_DETECTORS`](#nemoguardrails-library-utils-language_detection-_DETECTORS)

[`log`](#nemoguardrails-library-utils-language_detection-log)

### API

```python
nemoguardrails.library.utils.language_detection.detect_language(
    text: str,
    max_length: typing.Optional[int] = None
) -> typing.Optional[str]
```

Detect the language of `text` using fast-langdetect.

Returns the ISO code of the most likely language (one of
`FASTTEXT_LANGUAGES`), or `None` when detection is unavailable
(fast-langdetect not installed) or fails.

```python
nemoguardrails.library.utils.language_detection.get_refusal_message(
    lang: str,
    custom_messages: typing.Optional[typing.Dict[str, str]] = None
) -> str
```

Return the refusal message for `lang`.

Preference order: a custom message for the language, the built-in default
for the language, a custom English message, then the built-in English one.

```python
nemoguardrails.library.utils.language_detection.DEFAULT_REFUSAL_MESSAGES: Dict[str, str] = {'en': "I'm sorry, I can't respond to that.", 'es': 'Lo siento, no puedo respond...
```

```python
nemoguardrails.library.utils.language_detection.SUPPORTED_LANGUAGES: FrozenSet[str] = frozenset(DEFAULT_REFUSAL_MESSAGES)
```

```python
nemoguardrails.library.utils.language_detection._DETECTORS: Dict[Optional[int], Any] = {}
```

```python
nemoguardrails.library.utils.language_detection.log = logging.getLogger(__name__)
```