Localization icon indicating copy to clipboard operation
Localization copied to clipboard

Not working in combination with dagger hilt

Open Berki2021 opened this issue 4 years ago • 3 comments

I wanted to use this library in combination with dagger-hilt, but that does not seem to work. To be more specific:

There is a dagger-hilt annotation called @ApplicationContext, with which we can inject the application context inside every possible non-android-class. Here is an example:

class MyRepository(
    @ApplicationContext private val context: Context
) {
   fun getData() =  myService.collection(context.getString(R.string.stuff)).get().await()
}

Now comes the problem: When switching languages, this ApplicationContext does not seem to update. For example, let's say we have two versions of the above string:

German "R.string.stuff" -> Hallo English "R.string.stuff" -> Hello

When you now change the language from german to english in Fragment with (requireActivity as LocaleAwareCompatActivity).updateLocal(Locale.EN), then the following happens:

Calling requireContext().getString(R.string.stuff) in Fragment gives "Hello" (language was updated correctly). But inside my Repository "R.string.stuff" is still "Hallo" (language was not updated correctly)

Berki2021 avatar Dec 30 '21 16:12 Berki2021

I have sample code for Dagger Hilt implementation

See

ApplicationContext was injected with @ApplicationContext into StoryProvider and also change the language from HiltFragmentWithViewModel (Fragment). This work properly (see HiltDependencyInjectionTest UI test for this implementation)

Feel free to tell me more information if part of your code that different from my sample code

akexorcist avatar Dec 30 '21 21:12 akexorcist

I have sample code for Dagger Hilt implementation

See

* [HiltFragmentWithViewModel](https://github.com/akexorcist/Localization/blob/master/app/src/main/java/com/akexorcist/localizationapp/hilt/HiltFragmentWithViewModel.kt)

* [StoryProvider](https://github.com/akexorcist/Localization/blob/master/app/src/main/java/com/akexorcist/localizationapp/hilt/StoryProvider.kt)

ApplicationContext was injected with @ApplicationContext into StoryProvider and also change the language from HiltFragmentWithViewModel (Fragment). This work properly (see HiltDependencyInjectionTest UI test for this implementation)

Feel free to tell me more information if part of your code that different from my sample code

OK I will test this later and tell you if it worked.

EDIT:

I think I know where the problem lies. I didn't test this, but what about classes that live as Singletons and have an applicationContext injected into them? Classes such as Repositories annotated with @Singleton.

I observed the following: When I change the language, the applicationContext inside my singleton classes doesn't get updated. But when I now restart the app, the applicationContext is updated because the app has been newly created.

Might this be the Issue @akexorcist ?

Berki2021 avatar Dec 30 '21 21:12 Berki2021

Since AndroidX AppCompat has per-app language preferences for backward compatibility. Please migrate this library to AndroidX for more stability, compatibility, and longer support from Google team.

See Migrate to AndroidX guide

Thank you for your support

akexorcist avatar Jun 21 '22 21:06 akexorcist