Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Multi-Language Translator In Flutter

Flutter is well known for its compatibility among other mobile development platforms that why it is one of the most readily growing platforms, and the main reason behind it is the Flutter has come up with every feature and functionality which is required to develop a fully-fledged app. As you know an app is called best app if the user of it does need to access another resource to get its task done and flutter is here to meet all parameters

In this article, we are going to discuss a small technique for Multilanguage translation there are different packages available in the pub.dev and we are going to discuss one of them.

We would implement a technique for translator using this package :

translator | Dart Package
Free Google Translate API for Dart See it in pub: https://pub.dartlang.org/packages/translator and GitHub…pub.dev

You may be in the dilemma that if there is a localization available then why we need to use another package for language translation so the answer is

A free and unlimited Google Translate API for Dart.

You can use it for translate strings and text for educational purpose.

if you need to change a small no of things then it is best to do so instead of using a heavy localization package.

Implementation

First, we need to add this package in pubspec.yaml file.

translator:

then you need to import in the file you are writing your code

import ‘package:translator/translator.dart’;

then you need to create an instance of it

GoogleTranslator translator = GoogleTranslator();

now we need to understand how can we translate our input, Using translate method passing the args from and to designates the language from the text you’re typing and the language to be translated

translator.translate("I love Brazil!", from: 'en', to: 'pt').then((s) {
print(s);
});

or you can omit from language and it’ll auto-detect the language of the source text

translator.translate("Hello", to: 'es').then(print);

also, pass the value to a var using await

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.

The returned value is a Translation an object which holds the translation stuff

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)

You can use the extension method directly on the string too

print(await "example".translate(to: 'pt'));
// prints exemplo

There is a translate and print method that prints directly

translator.translateAndPrint("This means 'testing' in chinese", to: 'zh-cn');
// prints 这意味着用中文'测试
'

now we will use a method to translate given input

here in the given above example, you can see translator.translate is being used to translate the text, it only requires your input and the language in which you want to convert your input.

https://gist.github.com/shivanchalaeologic/a1c44b4957f3a56aaa014b747085efd7#file-multi_lang_translator-dart

As in the above example, you can see that a map of languages is being used to translate every time when the user selects specific language that translator methods execute.

As you can see in this above video it is translating this word in available languages.

Conclusion

This blog is about especially translating things to a small extent as it is best for educational purposes but when it comes to managing the whole app localization is best. However, this package is best if you are required to translate anything quickly on a small scale.


Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.💙

If this article has helped you a bit and found interesting please clap!👏


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.

Leave comment

Your email address will not be published. Required fields are marked with *.