Comments (2)
I've just spent two hours configuring all the required things, but I've made the following approach:
- Create a simple service class that would have a transaltion method (i.e.
String safeTranslate(String key)
). - Use DI/Service Locator to get said service in class
- Call
safeTranslate(key)
every time I need a translation
Example:
- Creating
TranslateService
I use get_it
and injectable
as a service locator in my app.
import 'package:flutter_translate/flutter_translate.dart' as globalTranslate;
import 'package:injectable/injectable.dart';
/// Implementation that is registered in GetIt service locator
@RegisterAs(TranslateService)
@injectable
class TranslateServiceImpl implements TranslateService {
@override
String safeTranslate(String key) {
try{
return globalTranslate.translate(key);
} catch(Exception){
return "translation.error";
}
}
}
/// Abstract translate service
abstract class TranslateService {
String safeTranslate(String key);
}
- Use DI/Service locator in tested class
At this point, you need to configure GetIt
- go through their tutorials on pages linked above and all should be safe and sound. Two notes that are not mentioned in tutorials:
- Make sure to add
test/**
source folder inbuild.yaml
- run
flutter packages pub run build_runner watch test
once configured to generate test dependencies
import 'package:get_it/get_it.dart';
...
/// Class that will be tested
class TestedClass {
/// Instance of TranslateService that is provided by GetIt (service locator)
final TranslateService _translateService = GetIt.instance.get();
// Some method that uses translation
String doSomeStuff() {
return _translateService.safeTranslate("translation_key")
}
}
- Testing a class that requires translation
- Create
your_class_test.dart
file undertest
folder
/// Important: GetIt needs to be initialized in a top level function
@injectableInit
void configureInjection(String environment) =>
$initGetIt(GetIt.instance, environment: environment);
void main() {
configureInjection("Some env value");
FlutterTest.test("Date formatter: today date", (){
/// Translation service should be located once class is created
final TestedClass testedClass = TestedClass();
String safeTranslation = testedClass.doSomeStuff(); <--- no exception here
...
});
from flutter-translate.
@VytorCalixto you can call Localization.load(<String,dynamic>{});
before tests and error won't be thrown
from flutter-translate.
Related Issues (20)
- Update packages HOT 4
- Parent Widget not updated HOT 2
- Add information for the deprecated flutter_translate_gen HOT 4
- No MaterialLocalizations found , If change language to Soomaali (so) HOT 2
- reverse back when change language HOT 1
- Language changes, then falls back to default language
- Possible to translate a specific widget? HOT 1
- Simplify configuration & initialization
- Implement a missing key strategy HOT 1
- Implement remote (http) localization loader HOT 1
- universal_io, Question about? HOT 1
- Why flutter_translate_gen has been archived? HOT 1
- Wiki updates HOT 4
- Upgrade the package to use the latest version of "intl" HOT 8
- Update to docs
- Pluralization feature not working for static keys HOT 5
- Localization Issue with OS language changes HOT 1
- Test for web platform is not working HOT 1
- Not fiend flutter_localizations GlobalMaterialLocalizations error HOT 1
- en.json missing from assets HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter-translate.