translate method

Future<String?> translate({
  1. required String path,
  2. required String? responsePattern(
    1. Response
    ),
  3. required HttpMethod method,
  4. required dynamic bodyPattern(
    1. String,
    2. Locale
    ),
  5. required Map<String, dynamic> headers,
  6. required Locale target,
  7. required bool forceRefresh,
  8. required Duration cacheDuration,
  9. required String text,
})

Implementation

Future<String?> translate(
    {required String path,
    required String? Function(Response) responsePattern,
    required HttpMethod method,
    required Function(String, Locale) bodyPattern,
    required Map<String, dynamic> headers,
    required Locale target,
    required bool forceRefresh,
    required Duration cacheDuration,
    required String text
  }) async {
  try {
    Response response;
    Map<String, dynamic> body = bodyPattern(text, target);
    Options _head = buildCacheOptions(cacheDuration, forceRefresh: forceRefresh);
    _head.headers = headers;

    switch (method) {
      case HttpMethod.get:
        response = await _dio.get(path,
          queryParameters: body,
          options: _head
        );
      break;
      case HttpMethod.post:
        response = await _dio.post(path,
          data: body,
          options: _head
        );
      break;
    }
    return responsePattern(response);
  } catch (e) {
    print(e);
  }
  return text;
}