Aerostack
memory

ai-language-detect Edge Function — Ai

AI

Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function ai-language-detect Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/ai-language-detect
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "ai-language-detect",
  args: {
    "text": "example_text",
    "topN": 1
  }
})

ai-language-detect — Detect the language of a text string

Identifies the natural language of text using character trigram frequency analysis. Supports 13 languages with no external API calls.


Supported Languages

English, Spanish, French, German, Portuguese, Italian, Dutch, Polish, Russian, Chinese, Japanese, Korean, Arabic.


API

POST /api/ai-language-detect

Request body

Field Type Required Default Description
text string Text to detect the language of
topN number 1 Number of language candidates to return, ranked by confidence

Success response (200)

{
  "success": true,
  "data": {
    "detected": [
      { "language": "English", "code": "en", "confidence": 0.82 }
    ],
    "primary": { "language": "English", "code": "en", "confidence": 0.82 }
  }
}

Error responses

Code HTTP When
INVALID_INPUT 400 Missing or empty text
INTERNAL_ERROR 500 Unexpected error

Usage

cURL
curl -X POST "$FUNCTION_URL" \
  -H "Content-Type: application/json" \
  -d '{"text": "Bonjour le monde, comment allez-vous?", "topN": 3}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ text: userMessage }),
});
const { data } = await response.json();
if (data.primary.code !== 'en') {
  // Route to multilingual prompt
}
Direct import (Node / Bun / Deno)
import { aiLanguageDetect } from '@aerostack/functions/ai-language-detect';

const { primary } = aiLanguageDetect({ text: 'Hola mundo' });
console.log(primary.language); // "Spanish"

Use Cases

  • Auto-routing user messages to language-specific system prompts before calling an LLM.
  • Tagging ingested documents with their language for multilingual search and filtering.
  • Validating that user-submitted content matches the expected language for a service.

Notes

  • Detection uses character trigram frequency analysis — no API keys or network calls required.
  • Confidence is reduced for texts shorter than 20 characters.
  • For very short inputs, increase text length for reliable detection.
  • Confidence values are normalised to [0, 1].

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

ai
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More AI Functions

Browse AI Functions →

Frequently asked questions

What does the ai-language-detect function do? +

ai-language-detect is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the ai-language-detect function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-language-detect ``` It will be live on Cloudflare Workers in seconds.

What runtime does ai-language-detect use? +

ai-language-detect runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the ai-language-detect function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.