GithubHelp home page GithubHelp logo

Comments (2)

xzhorikx avatar xzhorikx commented on August 14, 2024 3

I've just spent two hours configuring all the required things, but I've made the following approach:

  1. Create a simple service class that would have a transaltion method (i.e. String safeTranslate(String key)).
  2. Use DI/Service Locator to get said service in class
  3. Call safeTranslate(key) every time I need a translation

Example:

  1. 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);
}
  1. 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 in build.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")
  }
}
  1. Testing a class that requires translation
  • Create your_class_test.dart file under test 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.

IlyaMax avatar IlyaMax commented on August 14, 2024 1

@VytorCalixto you can call Localization.load(<String,dynamic>{}); before tests and error won't be thrown

from flutter-translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.