Skip to content

Switch to v3 api #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 5 commits into from
Aug 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## v2.0.0

### Added
- `context` to all API methods

### Changed
- Switched to v3 API which uses updated language detection model
- Language detection results contains `Language` and `Score`
- `UserStatus()` renamed to `AccountStatus()`
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ You can get it by signing up at https://detectlanguage.com
## Installation

```
go get -u github.com/detectlanguage/detectlanguage-go@v1.0.1
go get -u github.com/detectlanguage/detectlanguage-go@v2.0.0
```

### Configuration

```go
client := detectlanguage.New("YOUR API KEY")
dl := detectlanguage.New("YOUR API KEY")
```

## Usage

### Language detection

```go
detections, err := client.Detect("Buenos dias señor")
detections, err := dl.Detect(context.TODO(), "Dolce far niente")

if err != nil {
fmt.Fprintln(os.Stderr, "error detecting language:", err)
Expand All @@ -36,16 +36,15 @@ if err != nil {
}

fmt.Fprintln(os.Stdout, "Language:", detections[0].Language)
fmt.Fprintln(os.Stdout, "Reliable:", detections[0].Reliable)
fmt.Fprintln(os.Stdout, "Confidence:", detections[0].Confidence)
fmt.Fprintln(os.Stdout, "Score:", detections[0].Score)
```

### Single language code detection

If you need just a language code you can use `DetectCode`. It returns first detected language code.

```go
language, err := client.DetectCode("Buenos dias señor")
language, err := dl.DetectCode(context.TODO(), "Dolce far niente")

if err != nil {
fmt.Fprintln(os.Stderr, "error detecting language:", err)
Expand All @@ -64,7 +63,7 @@ To use batch detection just pass multiple texts to `DetectBatch` method.

```go
texts := []string{"labas rytas", "good morning"}
results, err := client.DetectBatch(texts)
results, err := dl.DetectBatch(context.TODO(), texts)

if err != nil {
fmt.Fprintln(os.Stderr, "error detecting language:", err)
Expand All @@ -79,10 +78,10 @@ fmt.Fprintln(os.Stdout, "Second text language:", detections[1][0].Language)
### Getting your account status

```go
result, err := client.UserStatus()
result, err := dl.AccountStatus(context.TODO())

if err != nil {
fmt.Fprintln(os.Stderr, "error getting user status:", err)
fmt.Fprintln(os.Stderr, "error getting account status:", err)
os.Exit(1)
return
}
Expand All @@ -100,7 +99,7 @@ fmt.Fprintln(os.Stdout, "Date:", result.Date)
### Getting list supported languages

```go
languages, err := client.Languages()
languages, err := dl.Languages(context.TODO())

if err != nil {
fmt.Fprintln(os.Stderr, "error getting languages list:", err)
Expand Down
12 changes: 7 additions & 5 deletions user.go → account.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package detectlanguage

// UserStatusResponse is the resource containing account status information
type UserStatusResponse struct {
import "context"

// AccountStatusResponse is the resource containing account status information
type AccountStatusResponse struct {
Date string `json:"date,omitempty"`
Requests int `json:"requests"`
Bytes int `json:"bytes"`
Expand All @@ -12,8 +14,8 @@ type UserStatusResponse struct {
Status string `json:"status"`
}

// UserStatus fetches account status
func (c *Client) UserStatus() (out *UserStatusResponse, err error) {
err = c.get(nil, "user/status", &out)
// AccountStatus fetches account status
func (c *Client) AccountStatus(ctx context.Context) (out *AccountStatusResponse, err error) {
err = c.get(ctx, "account/status", &out)
return
}
5 changes: 3 additions & 2 deletions user_test.go → account_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package detectlanguage_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUserStatus(t *testing.T) {
response, err := client.UserStatus()
func TestAccountStatus(t *testing.T) {
response, err := client.AccountStatus(context.TODO())

if assert.NoError(t, err) {
assert.NotEmpty(t, response.Date)
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const defaultUserAgent = "detectlanguage-go/" + Version
const defaultTimeout = 10 * time.Second

var apiBaseURL = &url.URL{
Scheme: "https", Host: "ws.detectlanguage.com", Path: "/0.2/",
Scheme: "https", Host: "ws.detectlanguage.com", Path: "/v3/",
}

// A Client provides an HTTP client for DetectLanguage API operations.
Expand Down
47 changes: 14 additions & 33 deletions detect.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,38 @@
package detectlanguage

import "context"

// DetectRequest contains language detection request params
type DetectRequest struct {
Query string `json:"q"`
}

// DetectResponse is a resource containing language detection response
type DetectResponse struct {
Data *DetectResponseData `json:"data"`
}

// DetectResponseData contains language detection response data
type DetectResponseData struct {
Detections []*DetectionResult `json:"detections"`
}

// DetectionResult is single language detection result
type DetectionResult struct {
Language string `json:"language"`
Reliable bool `json:"isReliable"`
Confidence float32 `json:"confidence"`
Language string `json:"language"`
Score float64 `json:"score"`
}

// DetectBatchRequest contains batch language detection request params
type DetectBatchRequest struct {
Query []string `json:"q"`
}

// DetectBatchResponse is a resource batch containing language detection response
type DetectBatchResponse struct {
Data *DetectBatchResponseData `json:"data"`
}

// DetectBatchResponseData contains batch language detection response data
type DetectBatchResponseData struct {
Detections [][]*DetectionResult `json:"detections"`
}

// Detect executes language detection for a single text
func (c *Client) Detect(in string) (out []*DetectionResult, err error) {
var response DetectResponse
err = c.post(nil, "detect", &DetectRequest{Query: in}, &response)
func (c *Client) Detect(ctx context.Context, in string) (out []*DetectionResult, err error) {
var response []*DetectionResult
err = c.post(ctx, "detect", &DetectRequest{Query: in}, &response)

if err != nil {
return nil, err
}

return response.Data.Detections, err
return response, err
}

// DetectCode executes language detection for a single text and returns detected language code
func (c *Client) DetectCode(in string) (out string, err error) {
detections, err := c.Detect(in)
func (c *Client) DetectCode(ctx context.Context, in string) (out string, err error) {
detections, err := c.Detect(ctx, in)

if err != nil {
return "", err
Expand All @@ -66,13 +47,13 @@ func (c *Client) DetectCode(in string) (out string, err error) {

// DetectBatch executes language detection with multiple texts.
// It is significantly faster than doing a separate request for each text indivdually.
func (c *Client) DetectBatch(in []string) (out [][]*DetectionResult, err error) {
var response DetectBatchResponse
err = c.post(nil, "detect", &DetectBatchRequest{Query: in}, &response)
func (c *Client) DetectBatch(ctx context.Context, in []string) (out [][]*DetectionResult, err error) {
var response [][]*DetectionResult
err = c.post(ctx, "detect-batch", &DetectBatchRequest{Query: in}, &response)

if err != nil {
return nil, err
}

return response.Data.Detections, err
return response, err
}
12 changes: 6 additions & 6 deletions detect_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package detectlanguage_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDetect(t *testing.T) {
detections, err := client.Detect("labas rytas")
detections, err := client.Detect(context.TODO(), "labas rytas")

if assert.NoError(t, err) {
assert.Equal(t, "lt", detections[0].Language)
assert.True(t, detections[0].Reliable)
assert.Greater(t, detections[0].Confidence, float32(0))
assert.Greater(t, detections[0].Score, float64(0))
}
}

func TestDetectCode(t *testing.T) {
code, err := client.DetectCode("labas rytas")
code, err := client.DetectCode(context.TODO(), "labas rytas")

if assert.NoError(t, err) {
assert.Equal(t, "lt", code)
}
}

func TestDetectCodeFailure(t *testing.T) {
code, err := client.DetectCode("")
code, err := client.DetectCode(context.TODO(), " ")

assert.EqualError(t, err, "Language not detected")
assert.Equal(t, code, "")
}

func TestDetectBatch(t *testing.T) {
query := []string{"labas rytas", "good morning"}
detections, err := client.DetectBatch(query)
detections, err := client.DetectBatch(context.TODO(), query)

if assert.NoError(t, err) {
assert.Equal(t, "lt", detections[0][0].Language)
Expand Down
6 changes: 4 additions & 2 deletions languages.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package detectlanguage

import "context"

// Language is the resource representing language
type Language struct {
Code string `json:"code"`
Name string `json:"name"`
}

// Languages retrieves the list of supported languages
func (c *Client) Languages() (out []*Language, err error) {
err = c.get(nil, "languages", &out)
func (c *Client) Languages(ctx context.Context) (out []*Language, err error) {
err = c.get(ctx, "languages", &out)
return
}
3 changes: 2 additions & 1 deletion languages_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package detectlanguage_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLanguages(t *testing.T) {
languages, err := client.Languages()
languages, err := client.Languages(context.TODO())

if assert.NoError(t, err) {
assert.NotEmpty(t, languages[0].Code)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package detectlanguage

// Version is the API client version
const Version = "1.0.1"
const Version = "2.0.0"