jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.34k stars 1.62k forks source link

internacionalization _getSimilarLanguageTranslation doesn't work well #2065

Open randomjoho opened 2 years ago

randomjoho commented 2 years ago

Describe the bug internacionalization.dart _getSimilarLanguageTranslation

 final translationsWithNoCountry = Get.translations
       .map((key, value) => MapEntry(key.split("_").first, value));

[en,en-US] => [en]

if change to

final translationsWithNoCountry = Map<String,Map<String, String>>(); 
    for(final lang in Get.translations.keys){
      final first = lang.split("_").first;
      if(translationsWithNoCountry.containsKey(first)){
        translationsWithNoCountry[first]!.addAll(Get.translations[lang]!);
      }else{
        translationsWithNoCountry[first] = Get.translations[lang]!;
      }
    }

Reproduction code NOTE: THIS IS MANDATORY, IF YOUR ISSUE DOES NOT CONTAIN IT, IT WILL BE CLOSED PRELIMINARY)

example:

import 'dart:ui'; 
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';

void main() {
  test('Test', () {
    Get
      ..appendTranslations(_Messages().keys)
      ..locale = const Locale.fromSubtags(
        languageCode: 'en',
        countryCode: 'US',
      );

     print('why [${'newOne'.tr}]');

  });
}

class _Messages extends Translations {
  @override
  Map<String, Map<String, String>> get keys => {
        'en': {
          'hello': 'Hello World',
          'newOne': 'why why why',
        },
        'en_US': {
          'hello': 'Hello World',
        },
      };
}

To Reproduce Steps to reproduce the behavior:

  1. Run in test

Expected behavior before why [newOne] after why [why why why]

Screenshots If applicable, add screenshots to help explain your problem.

Flutter Version: Enter the version of the Flutter you are using

Getx Version: Enter the version of the Getx you are using

Describe on which device you found the bug: ex: Moto z2 - Android.

Minimal reproduce code Provide a minimum reproduction code for the problem

rfm4j commented 2 years ago

Hi @randomjoho

I had this same problem, I'm adding a PR to support this feature.

You can try it in your code by using my version beffore it's merged. Just add this to your pubspec.yaml

  get:
    git:
      url: https://github.com/rfmanuj/getx.git
      ref: "#2065"
randomjoho commented 2 years ago

@rfmanuj Thanks !