|
| 1 | +// Copyright 2026 DeepL SE (https://www.deepl.com) |
| 2 | +// Use of this source code is governed by an MIT |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +package com.deepl.api; |
| 5 | + |
| 6 | +import com.google.gson.annotations.SerializedName; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.Map; |
| 9 | +import org.jetbrains.annotations.Nullable; |
| 10 | + |
| 11 | +/** |
| 12 | + * Information about a language supported by the DeepL API for a given resource, as returned by |
| 13 | + * {@link DeepLClient#getLanguagesForResource(String, java.util.List)}. |
| 14 | + */ |
| 15 | +public class LanguageSupport { |
| 16 | + /** Include value: also return beta languages and features. */ |
| 17 | + public static final String INCLUDE_BETA = "beta"; |
| 18 | + /** Include value: also return features that rely on third-party service providers. */ |
| 19 | + public static final String INCLUDE_EXTERNAL = "external"; |
| 20 | + |
| 21 | + @SerializedName("lang") |
| 22 | + private final String code; |
| 23 | + |
| 24 | + private final String name; |
| 25 | + |
| 26 | + private final String status; |
| 27 | + |
| 28 | + @SerializedName("usable_as_source") |
| 29 | + private final boolean usableAsSource; |
| 30 | + |
| 31 | + @SerializedName("usable_as_target") |
| 32 | + private final boolean usableAsTarget; |
| 33 | + |
| 34 | + @Nullable private final Map<String, LanguageFeature> features; |
| 35 | + |
| 36 | + /** |
| 37 | + * Initializes a new LanguageSupport object. |
| 38 | + * |
| 39 | + * @param code BCP-47 language code, for example "de", "en-US" or "zh-Hans". |
| 40 | + * @param name Name of the language in English. |
| 41 | + * @param status Support status of the language, for example "stable" or "beta". |
| 42 | + * @param usableAsSource Whether the language can be used as a source language. |
| 43 | + * @param usableAsTarget Whether the language can be used as a target language. |
| 44 | + * @param features Map of feature name to {@link LanguageFeature}. |
| 45 | + */ |
| 46 | + public LanguageSupport( |
| 47 | + String code, |
| 48 | + String name, |
| 49 | + String status, |
| 50 | + boolean usableAsSource, |
| 51 | + boolean usableAsTarget, |
| 52 | + @Nullable Map<String, LanguageFeature> features) { |
| 53 | + this.code = code; |
| 54 | + this.name = name; |
| 55 | + this.status = status; |
| 56 | + this.usableAsSource = usableAsSource; |
| 57 | + this.usableAsTarget = usableAsTarget; |
| 58 | + this.features = features; |
| 59 | + } |
| 60 | + |
| 61 | + /** @return BCP-47 language code, for example "de", "en-US", "pt-BR" or "zh-Hans". */ |
| 62 | + public String getCode() { |
| 63 | + return code; |
| 64 | + } |
| 65 | + |
| 66 | + /** @return Name of the language in English. */ |
| 67 | + public String getName() { |
| 68 | + return name; |
| 69 | + } |
| 70 | + |
| 71 | + /** @return Support status of the language, for example "stable" or "beta". */ |
| 72 | + public String getStatus() { |
| 73 | + return status; |
| 74 | + } |
| 75 | + |
| 76 | + /** @return <code>true</code> if the language can be used as a source language. */ |
| 77 | + public boolean isUsableAsSource() { |
| 78 | + return usableAsSource; |
| 79 | + } |
| 80 | + |
| 81 | + /** @return <code>true</code> if the language can be used as a target language. */ |
| 82 | + public boolean isUsableAsTarget() { |
| 83 | + return usableAsTarget; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return Map of feature name to {@link LanguageFeature} for the features this language supports |
| 88 | + * with the requested resource; empty if it supports no optional features. |
| 89 | + */ |
| 90 | + public Map<String, LanguageFeature> getFeatures() { |
| 91 | + return features == null ? Collections.emptyMap() : Collections.unmodifiableMap(features); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Checks whether this language supports the given feature for the requested resource. |
| 96 | + * |
| 97 | + * @param feature Feature name, for example {@link LanguageFeature#FEATURE_FORMALITY}. |
| 98 | + * @return <code>true</code> if the feature is supported. |
| 99 | + */ |
| 100 | + public boolean supports(String feature) { |
| 101 | + return features != null && features.containsKey(feature); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String toString() { |
| 106 | + return name + " (" + code + ")"; |
| 107 | + } |
| 108 | +} |
0 commit comments