B3nedikt / restring

Restring is a android library to replace string resources dynamically
Apache License 2.0
319 stars 31 forks source link

Toolbar and Menu options are not translated in AppCompat 1.4.1 #105

Closed nglauber closed 2 years ago

nglauber commented 2 years ago

I created a new project in Android Studio Chipmunk | 2021.2.1

The project has now the following dependencies:

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'

    implementation 'dev.b3nedikt.restring:restring:5.2.0'
    implementation 'dev.b3nedikt.viewpump:viewpump:4.0.10'
    implementation 'dev.b3nedikt.reword:reword:4.0.2'
}

I also created the maps for English and Portuguese.

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        Restring.init(this)
        ViewPump.init(RewordInterceptor)

        Restring.putStrings(Locale.forLanguageTag("pt-BR"), PortugueseRestringsRepository.strings)
        Restring.putStrings(Locale.ENGLISH, EnglishRestringsRepository.strings)
    }
}

and as described in the docs, I added this:

class MainActivity : AppCompatActivity() {
    //...
    private val appCompatDelegate: AppCompatDelegate by lazy {
        ViewPumpAppCompatDelegate(
            baseDelegate = super.getDelegate(),
            baseContext = this,
            wrapContext = Restring::wrapContext
        )
    }
    override fun getDelegate(): AppCompatDelegate {
        return appCompatDelegate
    }
    //...
}

The texts in fragments layouts are working fine, but the title and menu option are no displaying the correct text. As you can see below, the title and menu option are in English and the rest of the texts are in Portuguese.

2022-05-27 11 26 39

Is this might be happening because the title is set by the navigation library?

val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
B3nedikt commented 2 years ago

I think this might be the reason, it is hard to say without a complete example though. Can you provide a link to the repository of the project from the video above? If not just fork the sample and change it to match your code as close as possible. Then I can have a look at it ;)

nglauber commented 2 years ago

Thanks for replying @B3nedikt. Here's the link for the repo: https://github.com/nglauber/DemoPhilology

B3nedikt commented 2 years ago

You're welcome :)

Regarding the menu options: You are currently setting these with the MenuInflater which is not supported. The supported way would be to either update them in code or set the menu through the layout XML like demonstrated here: Reword example

Regarding the Toolbar title: The navigation library is calling Toolbar.setTitle with the CharSequence parameter not with the id here. This is currently not supported as well. You can however also just set this in code or on the layout XML.

Does this help you? If you need support for MenuInflater or setting the Toolbar title as a CharSequence this would be technically possible, just a lot of work to implement. If you would need this feel free to implement it, I accept PRs for my libraries ;)