### Translate and Store with Async/Await Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Illustrates how to use `await` with the `translate` method to get the translated text and store it in a variable. This is a common pattern for handling asynchronous operations in Dart. ```dart var translation = await translator.translate("I would buy a car, if I had money.", from: 'en', to: 'it'); print(translation); // prints Vorrei comprare una macchina, se avessi i soldi. ``` -------------------------------- ### Instantiate GoogleTranslator Class Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Instantiate the GoogleTranslator class. Supports both default siteGT client and alternative extensionGT client. ```dart import 'package:translator/translator.dart'; void main() async { // Default siteGT client final translator = GoogleTranslator(); // Extension client (gtx) — alternative if default is blocked final translatorGtx = GoogleTranslator(client: ClientType.extensionGT); print(translator); // Instance of GoogleTranslator print(translatorGtx); // Instance of GoogleTranslator (extensionGT mode) } ``` -------------------------------- ### GoogleTranslator Class Initialization Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Instantiate the GoogleTranslator class. You can choose between the default siteGT client or the extensionGT client for different translation behaviors. ```APIDOC ## GoogleTranslator Initialization ### Description Instantiate the `GoogleTranslator` class. You can choose between the default siteGT client (`ClientType.siteGT`) or the extensionGT client (`ClientType.extensionGT`) for different translation behaviors. ### Usage ```dart import 'package:translator/translator.dart'; // Default siteGT client final translator = GoogleTranslator(); // Extension client (gtx) — alternative if default is blocked final translatorGtx = GoogleTranslator(client: ClientType.extensionGT); ``` ``` -------------------------------- ### Basic Translation Usage in Dart Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Demonstrates the basic usage of the GoogleTranslator class, including translating text from Russian to English and Dart to Polish. It shows both promise-based and async/await methods for translation. ```dart void main() async { final translator = GoogleTranslator(); final input = "Здравствуйте. Ты в порядке?"; translator.translate(input, from: 'ru', to: 'en').then(print); // prints Hello. Are you okay? var translation = await translator.translate("Dart is very cool!", to: 'pl'); print(translation); // prints Dart jest bardzo fajny! print(await "example".translate(to: 'pt')); // prints exemplo } ``` -------------------------------- ### Translate String Directly with Extension Method Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Use the `translate()` extension method on any string for quick translations. It handles instance creation and delegation internally. Ensure the `translator` package is imported. ```dart import 'package:translator/translator.dart'; void main() async { // Translate a string literal directly final result = await 'example'.translate(to: 'pt'); print(result); // exemplo print(result.text); // exemplo // Translate a variable final greeting = 'Good morning'; final translated = await greeting.translate(from: 'en', to: 'fr'); print(translated); // Bonjour // Inline in string interpolation print('Translated: ${await "Hello world".translate(to: 'de')}'); // Translated: Hallo Welt } ``` -------------------------------- ### String Extension Method for Translation Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Demonstrates the use of the extension method directly on a string for translation. This provides a more concise syntax for translating a string to a specified target language. ```dart print(await "example".translate(to: 'pt')); // prints exemplo ``` -------------------------------- ### Translate and Print Directly Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Introduces the `translateAndPrint` method, which simplifies the process by translating the given text and printing the result directly to the console without needing to store it in a variable first. ```dart translator.translateAndPrint("This means 'testing' in chinese", to: 'zh-cn'); // prints 这意味着用中文'测试' ``` -------------------------------- ### translateAndPrint(String text, {String from, String to}) Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt A convenience method that translates text and prints the result directly to the console. ```APIDOC ## translateAndPrint(String text, {String from, String to}) ### Description A convenience method that calls `translate()` internally and pipes the result directly to `print()`. Returns `void`. Useful for quick debugging or fire-and-forget logging scenarios. ### Parameters - **text** (String) - Required - The text to translate. - **from** (String) - Optional - The source language ISO 639-1 code. Defaults to 'auto' for auto-detection. - **to** (String) - Optional - The target language ISO 639-1 code. Defaults to 'en'. ### Returns - `void` ### Request Example ```dart import 'package:translator/translator.dart'; void main() { final translator = GoogleTranslator(); translator.translateAndPrint("This means 'testing' in Chinese", to: 'zh-cn'); // prints: 这意味着用中文"测试" translator.translateAndPrint('Hello', from: 'en', to: 'es'); // prints: Hola translator.translateAndPrint('Dart ist sehr cool!', from: 'de', to: 'ja'); // prints: Dartはとてもクールです! } ``` ``` -------------------------------- ### Translate Method with Explicit Source Language Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Shows how to use the `translate` method when the source language is explicitly known. This method takes the text, source language (`from`), and target language (`to`) as arguments. ```dart translator.translate("I love Brazil!", from: 'en', to: 'pt').then((s) { print(s); }); // prints Eu amo o Brasil! ``` -------------------------------- ### Translate Method with Auto-Detected Source Language Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Demonstrates translating text to a target language while omitting the source language, allowing the API to auto-detect the source language. This is useful when the source language is unknown. ```dart translator.translate("Hello", to: 'es').then(print); // prints Hola ``` -------------------------------- ### translate(String sourceText, {String from, String to}) Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Translate text asynchronously from a source language to a target language. Supports auto-detection of the source language. ```APIDOC ## translate(String sourceText, {String from, String to}) ### Description Translates `sourceText` from the language specified by `from` (ISO 639-1 code, defaults to `'auto'` for auto-detection) into the language specified by `to` (defaults to `'en'`). Returns a `Future`. Throws `LanguageNotSupportedException` if an unrecognised language code is passed, and `http.ClientException` on network or HTTP errors. ### Parameters - **sourceText** (String) - Required - The text to translate. - **from** (String) - Optional - The source language ISO 639-1 code. Defaults to 'auto' for auto-detection. - **to** (String) - Optional - The target language ISO 639-1 code. Defaults to 'en'. ### Returns - `Future` - An object containing the translated text and related information. ### Request Example ```dart import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(); // Auto-detect source language, translate to English final t1 = await translator.translate('Здравствуйте. Ты в порядке?', to: 'en'); print(t1.text); // Hello. Are you okay? // Explicit source language final t2 = await translator.translate( 'I would buy a car, if I had money.', from: 'en', to: 'it', ); print(t2.text); // Vorrei comprare una macchina, se avessi i soldi. // Using .then() callback style translator .translate('I love Brazil!', from: 'en', to: 'pt') .then((result) => print(result.text)); // Eu amo o Brasil! // Error handling for unsupported language codes try { await translator.translate('Hello', to: 'xx'); } on LanguageNotSupportedException catch (e) { print(e.msg); // xx is not a supported language. } } ``` ``` -------------------------------- ### Translate and Print Text Directly Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt A convenience method to translate text and print the result immediately. Useful for quick debugging. ```dart import 'package:translator/translator.dart'; void main() { final translator = GoogleTranslator(); translator.translateAndPrint("This means 'testing' in Chinese", to: 'zh-cn'); // prints: 这意味着用中文"测试" translator.translateAndPrint('Hello', from: 'en', to: 'es'); // prints: Hola translator.translateAndPrint('Dart ist sehr cool!', from: 'de', to: 'ja'); // prints: Dartはとてもクールです! } ``` -------------------------------- ### Check Language Support with LanguageList Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Use the static `LanguageList.contains()` method to verify if a language code or name is supported by the package. This is useful for validating user input before initiating a translation. It supports ISO 639-1 codes and language names (case-insensitive). ```dart import 'package:translator/src/langs/language.dart'; void main() { print(LanguageList.contains('ja')); // true (Japanese by code) print(LanguageList.contains('fr')); // true (French by code) print(LanguageList.contains('zh-cn')); // true (Simplified Chinese) print(LanguageList.contains('kke')); // false (unsupported code) print(LanguageList.contains('auto')); // true (auto-detection placeholder) } ``` -------------------------------- ### LanguageList.contains(String codeOrLang) Method Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt A static method that returns `true` if the given ISO 639-1 code or language name is supported by the package. Case-insensitive for language names. Useful for validating user input before calling `translate()`. ```APIDOC ### `LanguageList.contains(String codeOrLang)` — Check language support A static method that returns `true` if the given ISO 639-1 code or language name is supported by the package. Case-insensitive for language names. Useful for validating user input before calling `translate()`. ### Usage Examples ```dart import 'package:translator/src/langs/language.dart'; void main() { print(LanguageList.contains('ja')); // true (Japanese by code) print(LanguageList.contains('fr')); // true (French by code) print(LanguageList.contains('zh-cn')); // true (Simplified Chinese) print(LanguageList.contains('kke')); // false (unsupported code) print(LanguageList.contains('auto')); // true (auto-detection placeholder) } ``` ``` -------------------------------- ### baseUrl setter Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Allows overriding the default translation endpoint URL, which can be useful for accessing the API from different regions or if the default endpoint is unavailable. ```APIDOC ## baseUrl setter ### Description Sets an alternative base URL for the Google Translate HTTPS endpoint. Useful in regions where the default `translate.googleapis.com` is unavailable or rate-limited. The URL should be provided without a scheme (`https://` is added automatically). ### Usage ```dart import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(); // Switch to the Hong Kong endpoint translator.baseUrl = 'translate.google.com.hk'; final result = await translator.translate('friendship', from: 'en', to: 'es'); print(result.text); // amistad // Restore default translator.baseUrl = 'translate.googleapis.com'; } ``` ``` -------------------------------- ### Accessing Translation Object Properties Source: https://github.com/gabrielpacheco23/google-translator/blob/master/README.md Explains how to access the properties of the `Translation` object returned by the `translate` method. It shows how to retrieve the original source text, its detected language, the translated text, and its target language. ```dart var translation = await translator.translate('Translation', from: 'en', to: 'es'); print('${translation.source} (${translation.sourceLanguage}) == ${translation.text} (${translation.targetLanguage})'); // prints Translation (English) == Traducción (Spanish) ``` -------------------------------- ### String.translate() Extension Method Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt A Dart extension on String that allows calling `translate()` directly on any string literal or variable. Internally creates a `GoogleTranslator()` instance and delegates to its `translate()` method. Returns `Future`. ```APIDOC ## `String.translate({String from, String to})` — Extension method on String A Dart extension on `String` that allows calling `translate()` directly on any string literal or variable. Internally creates a `GoogleTranslator()` instance and delegates to its `translate()` method. Returns `Future`. ### Usage Examples ```dart import 'package:translator/translator.dart'; void main() async { // Translate a string literal directly final result = await 'example'.translate(to: 'pt'); print(result); // exemplo print(result.text); // exemplo // Translate a variable final greeting = 'Good morning'; final translated = await greeting.translate(from: 'en', to: 'fr'); print(translated); // Bonjour // Inline in string interpolation print('Translated: ${await "Hello world".translate(to: 'de')}'); // Translated: Hallo Welt } ``` ``` -------------------------------- ### Translation Result Object Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt An abstract class returned by every `translate()` call. Holds the translated text (`text`), the original input (`source`), the detected or specified source language (`sourceLanguage`), and the target language (`targetLanguage`). Both language fields are `Language` objects with `name` and `code` properties. `toString()` returns `text`, enabling direct use in string interpolation and `print()`. ```APIDOC ### `Translation` — Translation result object An abstract class returned by every `translate()` call. Holds the translated text (`text`), the original input (`source`), the detected or specified source language (`sourceLanguage`), and the target language (`targetLanguage`). Both language fields are `Language` objects with `name` and `code` properties. `toString()` returns `text`, enabling direct use in string interpolation and `print()`. ### Usage Examples ```dart import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(client: ClientType.extensionGT); final t = await translator.translate('perro', to: 'ru'); print(t.text); // собака print(t.source); // perro print(t.sourceLanguage.name); // Spanish print(t.sourceLanguage.code); // es print(t.targetLanguage.name); // Russian print(t.targetLanguage.code); // ru print('$t'); // собака (toString() returns text) // Concatenation using the + operator final a = await translator.translate('hello', to: 'fr'); final b = await translator.translate('world', to: 'fr'); print(a + ' ' + b); // bonjour monde } ``` ``` -------------------------------- ### Translate Text with GoogleTranslator Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Translate text asynchronously. Supports auto-detection of source language or explicit specification. Handles unsupported language codes and network errors. ```dart import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(); // Auto-detect source language, translate to English final t1 = await translator.translate('Здравствуйте. Ты в порядке?', to: 'en'); print(t1); // Hello. Are you okay? print(t1.source); // Здравствуйте. Ты в порядке? print(t1.sourceLanguage); // Russian print(t1.sourceLanguage.code); // ru print(t1.targetLanguage); // English print(t1.targetLanguage.code); // en // Explicit source language final t2 = await translator.translate( 'I would buy a car, if I had money.', from: 'en', to: 'it', ); print(t2.text); // Vorrei comprare una macchina, se avessi i soldi. // Using .then() callback style translator .translate('I love Brazil!', from: 'en', to: 'pt') .then((result) => print(result)); // Eu amo o Brasil! // Error handling for unsupported language codes try { await translator.translate('Hello', to: 'xx'); } on LanguageNotSupportedException catch (e) { print(e.msg); // xx is not a supported language. } } ``` -------------------------------- ### Override Google Translate Base URL Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Set an alternative base URL for the Google Translate endpoint. Useful for regions where the default URL is unavailable or rate-limited. ```dart import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(); // Switch to the Hong Kong endpoint translator.baseUrl = 'translate.google.com.hk'; final result = await translator.translate('friendship', from: 'en', to: 'es'); print(result); // amistad // Restore default translator.baseUrl = 'translate.googleapis.com'; } ``` -------------------------------- ### Language Identifier Object Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Represents a single language supported by Google Translate. Each instance carries an ISO 639-1 `code` (e.g., `'en'`, `'zh-cn'`) and a human-readable `name` (e.g., `'English'`, `'Chinese (Simplified)'`). Retrieved from a `Translation` result or directly via `LanguageList`. `toString()` returns `name`. ```APIDOC ### `Language` — Language identifier object Represents a single language supported by Google Translate. Each instance carries an ISO 639-1 `code` (e.g., `'en'`, `'zh-cn'`) and a human-readable `name` (e.g., `'English'`, `'Chinese (Simplified)'`). Retrieved from a `Translation` result or directly via `LanguageList`. `toString()` returns `name`. ### Usage Examples ```dart import 'package:translator/src/langs/language.dart'; import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(); final t = await translator.translate('Translation', to: 'es'); final lang = t.sourceLanguage; print(lang.code); // en print(lang.name); // English print(lang); // English (toString() returns name) } ``` ``` -------------------------------- ### Handle Translation Result Object Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt The `Translation` object contains the translated text, source, and language details. Its `toString()` method returns the translated text, allowing direct use in print statements or string interpolation. The `+` operator can be used for concatenating translated strings. ```dart import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(client: ClientType.extensionGT); final t = await translator.translate('perro', to: 'ru'); print(t.text); // собака print(t.source); // perro print(t.sourceLanguage.name); // Spanish print(t.sourceLanguage.code); // es print(t.targetLanguage.name); // Russian print(t.targetLanguage.code); // ru print('$t'); // собака (toString() returns text) // Concatenation using the + operator final a = await translator.translate('hello', to: 'fr'); final b = await translator.translate('world', to: 'fr'); print(a + ' ' + b); // bonjour monde } ``` -------------------------------- ### Access Language Details from Translation Object Source: https://context7.com/gabrielpacheco23/google-translator/llms.txt Retrieve language codes and names from the `sourceLanguage` and `targetLanguage` properties of a `Translation` object. The `Language` object's `toString()` method returns its name. ```dart import 'package:translator/src/langs/language.dart'; import 'package:translator/translator.dart'; void main() async { final translator = GoogleTranslator(); final t = await translator.translate('Translation', to: 'es'); final lang = t.sourceLanguage; print(lang.code); // en print(lang.name); // English print(lang); // English (toString() returns name) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.