-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add detect_language and get_languages functions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # CLAUDE.md | ||
|
|
||
| ## Project Overview | ||
|
|
||
| DeepL.jl is a Julia SDK for the DeepL translation API. It provides functions to translate text, detect languages, and list supported languages. | ||
|
|
||
| ## Quick Reference | ||
|
|
||
| ```bash | ||
| # Install dependencies | ||
| julia --project=. -e 'using Pkg; Pkg.instantiate()' | ||
|
|
||
| # Run tests (requires DEEPL_API_KEY env var) | ||
| julia --project=. -e 'using Pkg; Pkg.test()' | ||
| ``` | ||
|
|
||
| ## Project Structure | ||
|
|
||
| ``` | ||
| src/ | ||
| DeepL.jl # Main module — exports, constants, includes | ||
| client.jl # HTTP helpers (post_request, get_request) | ||
| translate.jl # translate_text + language constant lists | ||
| detect.jl # detect_language | ||
| languages.jl # get_languages | ||
| test/ | ||
| runtests.jl # Full test suite using @testset | ||
| ``` | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| - `DEEPL_API_KEY` — required for all API calls and tests | ||
| - `DEEPL_API_URL` — optional override (defaults to `https://api.deepl.com/v2`) | ||
|
|
||
| ## Code Conventions | ||
|
|
||
| - **Multiple dispatch:** define the same function for `String` and `Vector{String}` signatures | ||
| - **Pair syntax:** support `source => target` as a language pair argument (e.g., `"EN" => "DE"`) | ||
| - **Nullable types:** use `Optional{T} = Union{T, Nothing}` | ||
| - **Input validation:** guard clauses at the top of public functions; throw `ArgumentError` for invalid inputs | ||
| - **Errors:** `handle_api_error()` in `client.jl` checks HTTP status codes | ||
| - **Exports:** only public API functions are exported from the module (`translate_text`, `detect_language`, `get_languages`) | ||
| - **Imports:** use qualified imports (`using HTTP: HTTP`, `using JSON: JSON`) | ||
| - **Docstrings:** all public functions have Julia-style docstrings | ||
|
|
||
| ## CI | ||
|
|
||
| GitHub Actions runs on push/PR to `main` with Julia 1.11. The `main` environment provides the `DEEPL_API_KEY` secret. | ||
|
|
||
| ## Commit Style | ||
|
|
||
| Conventional commits: `feat:`, `fix:`, `chore:`, `test:`, `docs:` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Public API | ||
| # ---------- | ||
|
|
||
| """ | ||
| detect_language(text::AbstractString) | ||
|
|
||
| Detect the language of the given text using the DeepL API. The env variable `DEEPL_API_KEY` must be set. | ||
|
|
||
| Since the DeepL API does not provide a dedicated language detection endpoint, this function | ||
| uses the translate endpoint without specifying a source language and extracts the detected | ||
| language from the response. | ||
|
|
||
| # Arguments | ||
| - `text::AbstractString`: The text whose language should be detected. | ||
|
|
||
| # Returns | ||
| - `String`: The detected language code (e.g. "DE", "EN", "FR"). | ||
| """ | ||
| function detect_language(text::AbstractString) | ||
| isempty(text) && throw(ArgumentError("text must not be empty")) | ||
|
|
||
| body = Dict("text" => [text], "target_lang" => "EN") | ||
|
|
||
| response = post_request("/translate", body) | ||
| handle_api_error(response) | ||
| result = JSON.parse(String(response.body)) | ||
| return result["translations"][1]["detected_source_language"] | ||
| end | ||
|
|
||
| """ | ||
| detect_language(text::Vector{<:AbstractString}) | ||
|
|
||
| Detect the language of each text in the given vector using the DeepL API. The env variable `DEEPL_API_KEY` must be set. | ||
|
|
||
| # Arguments | ||
| - `text::Vector{<:AbstractString}`: The texts whose languages should be detected. | ||
|
|
||
| # Returns | ||
| - `Vector{String}`: The detected language codes (e.g. ["DE", "EN", "FR"]). | ||
| """ | ||
| function detect_language(text::Vector{<:AbstractString}) | ||
| isempty(text) && throw(ArgumentError("text must not be empty")) | ||
|
|
||
| body = Dict("text" => text, "target_lang" => "EN") | ||
|
|
||
| response = post_request("/translate", body) | ||
| handle_api_error(response) | ||
| result = JSON.parse(String(response.body)) | ||
| return [t["detected_source_language"] for t in result["translations"]] | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Public API | ||
| # ---------- | ||
|
|
||
| """ | ||
| get_languages(type::String="source") | ||
|
|
||
| Retrieve the list of languages supported by the DeepL API. The env variable `DEEPL_API_KEY` must be set. | ||
|
|
||
| # Arguments | ||
| - `type::String`: The type of languages to retrieve. Must be `"source"` or `"target"`. Defaults to `"source"`. | ||
|
|
||
| # Returns | ||
| - `Vector{Dict{String, Any}}`: A list of language objects, each containing: | ||
| - `"language"`: The language code (e.g. `"DE"`, `"EN"`, `"EN-GB"`). | ||
| - `"name"`: The human-readable language name (e.g. `"German"`, `"English"`). | ||
| - `"supports_formality"`: Whether formality options are available (target languages only). | ||
| """ | ||
| function get_languages(type::String="source") | ||
| type in ("source", "target") || throw(ArgumentError("type must be \"source\" or \"target\", got \"$type\"")) | ||
|
|
||
| response = get_request("/languages"; params=Dict("type" => type)) | ||
| handle_api_error(response) | ||
| return JSON.parse(String(response.body)) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.