Skip to content

Locales

Li-Wei Yap edited this page Jan 13, 2023 · 5 revisions

Locales

Literals in string resources

object LocaleHelper {
    // Taken from: https://stackoverflow.com/questions/9475589/how-to-get-string-from-different-locales-in-android
    //
    // Probably no need to set android.bundle.language.enableSplit to false in app/build.gradle? (https://developer.android.com/guide/app-bundle/configure-base#disable_config_apks)
    // Because if you try to use the locale for which there are no corresponding resources
    // (either within the app or system), then the default string resources (res/values/strings.xml) of the app will be utilized.
    // (https://stackoverflow.com/a/40986218/12367873)
    @JvmStatic
    fun getLocalizedResources(context: Context, desiredLocale: Locale): Resources {
        val customConfig = Configuration(context.resources.configuration)
        customConfig.setLocale(desiredLocale)
        return context.createConfigurationContext(customConfig).resources
    }

    @JvmStatic
    fun getLocalizedResources(context: Context): Resources {
        return getLocalizedResources(context, Locale.US)
    }
}

Usage:

mCurrentDisplayedIntroSegmentTextView.setText(LocaleHelper.getLocalizedResources(requireContext()).getQuantityString(R.plurals.pauseduration_text, (int) (viewModel.getPauseDurationInMilliSecs()/1000), viewModel.getPauseDurationInMilliSecs()/1000));

Links:

Adding German to the app

See the following resources as well:

Old

This should probably be low-priority, because we would be doubling resources (texts, narration audio, character images) for all locales, but only the German ones stand to benefit.

  • We could improve upon the images by making the character names part of the XML instead of the images. But I seem to recall having to stretch some of the character names manually with Gimp in the past to ensure that everything looks nice. In other words, the font size is not exactly constant across all character names, so it is not so straightforward, i.e. extensive testing is required. The updated One Night Ultimate Werewolf app makes use of an extremely small font size.
  • The creators of the One Night Ultimate Werewolf app were aware of the duplication of resources that serves only selected locales and attempted to circumvent this by creating a duplicate app altogether.
  • Without creating a duplicate app for German, I feel that the costs would outweigh the benefits, as we stand to lose out on the lightweightness of the app, which at this very moment is significant (in comparison to similar apps out there).

New

Because we use app bundles, devices will download only the code and resources they require to run your app, so for language resources, a user’s device downloads only your app’s language resources that match the one or more languages currently selected in the device’s settings.

We can implement on-demand language downloads using the Play Feature Delivery Library, as detailed in the second link above. This would require us to handle user requests for uninstalls and network error handling. But before we can do any of this, we need to first address the point above that the character images and texts should be split, so that the duplicated resources to be downloaded would include only texts and audio, and not images.