Skip to content

V2 SDK #14

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 16 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
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [ 11, 17 ]
java-version: [ 11, 17, 21, 24 ]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java-version }}
distribution: 'adopt'
distribution: 'temurin'
cache: 'maven'
- name: Build with Maven
env:
Expand All @@ -23,9 +23,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java-release: [ 17 ]
java-release: [ 21, 24 ]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up JDK ${{ matrix.java-release }}
uses: oracle-actions/setup-java@v1
with:
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://medium.com/@jtbsorensen/publish-your-artifact-to-the-maven-central-repository-using-github-actions-15d3b5d9ce88
name: Publish package to the Maven Central Repository
on:
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Maven Central Repository
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
gpg-passphrase: GPG_PASSPHRASE
- name: Publish package
run: mvn -P release --batch-mode deploy -DskipTests
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# linux
*~

.env.local

# java
*.class
.gradle

# packages
*.jar
Expand All @@ -24,4 +27,4 @@ target

# idea
.idea
*.iml
*.iml
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add this dependency to your `pom.xml`:
<dependency>
<groupId>com.detectlanguage</groupId>
<artifactId>detectlanguage</artifactId>
<version>1.1.0</version>
<version>2.0.0</version>
</dependency>
```

Expand All @@ -30,7 +30,7 @@ repositories {
}

dependencies {
compile 'com.detectlanguage:detectlanguage:1.1.0'
compile 'com.detectlanguage:detectlanguage:2.0.0'
}
```

Expand All @@ -46,9 +46,6 @@ Before using Detect Language API client you have to setup your personal **API ke

```java
DetectLanguage.apiKey = "YOURAPIKEY";

// Enable secure mode (SSL) if passing sensitive information
// DetectLanguage.ssl = true;
```

### Language detection
Expand All @@ -59,14 +56,13 @@ List<Result> results = DetectLanguage.detect("Hello world");
Result result = results.get(0);

System.out.println("Language: " + result.language);
System.out.println("Is reliable: " + result.isReliable);
System.out.println("Confidence: " + result.confidence);
System.out.println("Score: " + result.score);
```

### Simple detection
### Language code detection

```java
String language = DetectLanguage.simpleDetect("Hello world");
String language = DetectLanguage.detectCode("Hello world");
```

### Batch detection
Expand All @@ -80,6 +76,18 @@ String[] texts = {
List<List<Result>> results = DetectLanguage.detect(texts);
```

### Getting your account status

```java
AccountStatusResponse accountStatus = DetectLanguage.getAccountStatus();
```

### Getting list supported languages

```java
LanguageInfo[] languages = DetectLanguage.getLanguages();
```

## Requirements

- [gson](http://code.google.com/p/google-gson/)
Expand Down Expand Up @@ -113,13 +121,3 @@ Please use appropriately tagged github [issues](https://github.com/detectlanguag
### Release

Done using the [Sonatype Nexus UI](https://oss.sonatype.org/).

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Write your code **and tests**
4. Ensure all [tests](#testing) still pass
5. Commit your changes (`git commit -am 'Add some feature'`)
6. Push to the branch (`git push origin my-new-feature`)
7. Create new pull request
32 changes: 0 additions & 32 deletions build.gradle

This file was deleted.

9 changes: 9 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tools]
java = "openjdk-21"
maven = "3"

[env]
_.file = ".env.local"

[tasks]
test = "mvn test"
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.11.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
Expand Down
77 changes: 34 additions & 43 deletions src/main/java/com/detectlanguage/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,45 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.lang.reflect.Type;

public class Client {

public static final String CHARSET = "UTF-8";

private static final String AGENT = "detectlanguage-java";
private static final String CHARSET = "UTF-8";

public Client() {
}

public <T> T execute(String method, Map<String, Object> params,
Class<T> responseClass) throws APIError {
URL url = buildUrl(method);
String query = buildQuery(params);
public <T> T get(String path, Type responseType) throws APIError {
return execute("GET", path, null, null, responseType);
}

public <T> T post(String path, String payload, Type responseType) throws APIError {
return execute("POST", path, null, payload, responseType);
}

private <T> T execute(String method, String path, Map<String, Object> params,
String payload, Type responseType) throws APIError {
URL url = buildUrl(path, params);

try {
HttpURLConnection conn = createPostConnection(url, query);
HttpURLConnection conn = createConnection(url);

conn.setDoOutput(true);
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", "application/json");

if (payload != null) {
OutputStream output = null;
try {
output = conn.getOutputStream();
output.write(payload.getBytes(CHARSET));
} finally {
if (output != null) {
output.close();
}
}
}

try {
// trigger the request
Expand All @@ -47,7 +69,7 @@ public <T> T execute(String method, Map<String, Object> params,
body = getResponseBody(conn.getErrorStream());
}

return processResponse(responseClass, body);
return processResponse(responseType, body);
} finally {
conn.disconnect();
}
Expand All @@ -56,7 +78,7 @@ public <T> T execute(String method, Map<String, Object> params,
}
}

private <T> T processResponse(Class<T> responseClass, String body)
private <T> T processResponse(Type responseType, String body)
throws APIError {

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Expand All @@ -69,20 +91,15 @@ private <T> T processResponse(Class<T> responseClass, String body)
}

try {
return gson.fromJson(body, responseClass);
return gson.fromJson(body, responseType);
} catch (JsonSyntaxException e) {
throw new APIError("Server error. Invalid response format.", 9999);
}
}

private String getProtocol() {
return DetectLanguage.ssl ? "https" : "http";
}

private URL buildUrl(String path, Map<String, Object> params) {
String url = String.format(
"%s://%s/%s/%s",
getProtocol(),
"https://%s/%s/%s",
DetectLanguage.apiHost,
DetectLanguage.apiVersion,
path);
Expand All @@ -98,31 +115,6 @@ private URL buildUrl(String path, Map<String, Object> params) {
}
}

private URL buildUrl(String path) {
return buildUrl(path, null);
}

private HttpURLConnection createPostConnection(
URL url, String query) throws IOException {
HttpURLConnection conn = createConnection(url);

conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", String.format(
"application/x-www-form-urlencoded;charset=%s", CHARSET));

OutputStream output = null;
try {
output = conn.getOutputStream();
output.write(query.getBytes(CHARSET));
} finally {
if (output != null) {
output.close();
}
}
return conn;
}

private HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(DetectLanguage.timeout);
Expand All @@ -133,7 +125,6 @@ private HttpURLConnection createConnection(URL url) throws IOException {

conn.setRequestProperty("User-Agent", AGENT + '/' + version);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Charset", CHARSET);
conn.setRequestProperty("Authorization", "Bearer " + DetectLanguage.apiKey);

return conn;
Expand Down
Loading