|
| 1 | +--- |
| 2 | +title: 'Common use cases [BETA]' |
| 3 | +description: "Pseudocode examples for common language and feature lookup patterns using the v3/languages endpoints." |
| 4 | +--- |
| 5 | + |
| 6 | +This page shows how to use the `/v3/languages` endpoints for common integration tasks. Examples are written |
| 7 | +as pseudocode and are product-agnostic unless otherwise noted. |
| 8 | + |
| 9 | +For background on how features and feature dependency types work, see the |
| 10 | +[overview](/api-reference/languages/retrieve-supported-languages-by-product). |
| 11 | + |
| 12 | +<Note> |
| 13 | + The examples below do not account for language pair exceptions. For most integrations this is fine — exceptions |
| 14 | + are rare and the API handles them gracefully by disabling the unsupported feature rather than failing. If you |
| 15 | + need precise feature support for specific language pairs, see |
| 16 | + [Handling language pair exceptions](#handling-language-pair-exceptions) at the end of this page. |
| 17 | +</Note> |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Populate source and target language dropdowns |
| 22 | + |
| 23 | +A single call to `GET /v3/languages` returns all languages for a product. Filter by `usable_as_source` and |
| 24 | +`usable_as_target` to populate each dropdown separately. |
| 25 | + |
| 26 | +``` |
| 27 | +GET /v3/languages?product=translate_text |
| 28 | +
|
| 29 | +languages = response |
| 30 | +
|
| 31 | +source_options = languages.filter(l => l.usable_as_source) |
| 32 | +target_options = languages.filter(l => l.usable_as_target) |
| 33 | +
|
| 34 | +render source_dropdown(source_options) |
| 35 | +render target_dropdown(target_options) |
| 36 | +``` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Show formality options only when supported |
| 41 | + |
| 42 | +`formality` is a target-only feature. Check the selected target language's `features` array — no need to look |
| 43 | +at the source language. |
| 44 | + |
| 45 | +``` |
| 46 | +GET /v3/languages?product=translate_text |
| 47 | +
|
| 48 | +languages = response |
| 49 | +target = languages.find(l => l.lang == selected_target_lang) |
| 50 | +
|
| 51 | +if target.features.includes("formality"): |
| 52 | + show formality_selector // e.g. ["default", "more", "less"] |
| 53 | +else: |
| 54 | + hide formality_selector |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## Check if a glossary can be used for a given language pair |
| 60 | + |
| 61 | +`glossary` is a source-and-target feature — both languages must support it. |
| 62 | + |
| 63 | +``` |
| 64 | +GET /v3/languages?product=translate_text |
| 65 | +
|
| 66 | +languages = response |
| 67 | +
|
| 68 | +source = languages.find(l => l.lang == source_lang) |
| 69 | +target = languages.find(l => l.lang == target_lang) |
| 70 | +
|
| 71 | +glossary_allowed = source.features.includes("glossary") |
| 72 | + and target.features.includes("glossary") |
| 73 | +``` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## List target languages that accept glossaries from a given source language |
| 78 | + |
| 79 | +Filter to targets where both the source and target support the `glossary` feature. |
| 80 | + |
| 81 | +``` |
| 82 | +GET /v3/languages?product=translate_text |
| 83 | +
|
| 84 | +languages = response |
| 85 | +source_lang = "en" |
| 86 | +
|
| 87 | +source = languages.find(l => l.lang == source_lang) |
| 88 | +
|
| 89 | +if not source.features.includes("glossary"): |
| 90 | + return [] // source doesn't support glossary at all |
| 91 | +
|
| 92 | +targets_with_glossary = languages |
| 93 | + .filter(l => l.usable_as_target) |
| 94 | + .filter(l => l.features.includes("glossary")) |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Show writing style options for the Write product |
| 100 | + |
| 101 | +`writing_style` is a target-only feature on the `write` product. Check the target language's `features` array. |
| 102 | + |
| 103 | +``` |
| 104 | +GET /v3/languages?product=write |
| 105 | +
|
| 106 | +languages = response |
| 107 | +target = languages.find(l => l.lang == selected_target_lang) |
| 108 | +
|
| 109 | +if target.features.includes("writing_style"): |
| 110 | + show writing_style_selector |
| 111 | +else: |
| 112 | + hide writing_style_selector |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Determine feature support programmatically |
| 118 | + |
| 119 | +Use `/v3/languages/products` to drive feature checks at runtime — without hardcoding which features are |
| 120 | +target-only or source-and-target into your client. |
| 121 | + |
| 122 | +``` |
| 123 | +GET /v3/languages/products |
| 124 | +GET /v3/languages?product=translate_text |
| 125 | +
|
| 126 | +products = first response |
| 127 | +languages = second response |
| 128 | +
|
| 129 | +product = products.find(p => p.name == "translate_text") |
| 130 | +source = languages.find(l => l.lang == source_lang) |
| 131 | +target = languages.find(l => l.lang == target_lang) |
| 132 | +
|
| 133 | +for feature in product.features: |
| 134 | + supported = true |
| 135 | + if feature.required_on_source and not source.features.includes(feature.name): |
| 136 | + supported = false |
| 137 | + if feature.required_on_target and not target.features.includes(feature.name): |
| 138 | + supported = false |
| 139 | +``` |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## Handling language pair exceptions |
| 144 | + |
| 145 | +In rare cases, feature support for a specific pair differs from what the individual language objects indicate. |
| 146 | +The `/v3/languages/exceptions` endpoint exposes these cases. When an exception exists for a pair, its `features` |
| 147 | +array is authoritative — use it directly instead of intersecting the individual language `features` arrays. |
| 148 | + |
| 149 | +The example below shows a full glossary pair check that accounts for exceptions: |
| 150 | + |
| 151 | +``` |
| 152 | +GET /v3/languages?product=translate_text |
| 153 | +GET /v3/languages/exceptions?product=translate_text |
| 154 | +
|
| 155 | +languages = first response |
| 156 | +exceptions = second response |
| 157 | +
|
| 158 | +exception = exceptions.find(e => e.source_lang == source_lang && e.target_lang == target_lang) |
| 159 | +
|
| 160 | +if exception: |
| 161 | + features = exception.features |
| 162 | +else: |
| 163 | + source = languages.find(l => l.lang == source_lang) |
| 164 | + target = languages.find(l => l.lang == target_lang) |
| 165 | + features = intersect(source.features, target.features) |
| 166 | +
|
| 167 | +glossary_allowed = features.includes("glossary") |
| 168 | +``` |
| 169 | + |
| 170 | +The same pattern applies to any feature check — replace `"glossary"` with the feature you are checking. |
0 commit comments