Galou Minisini

05/04/2023, 6:56 AM
Hi! I need to rename a feature and control when the new name will be visible with a feature flag. That name is being shown to the user several times, in several different resource string. Instead of having all the strings with the word
in it I want to now have the word
. Is there an easy way to do it without having to do a if/else statement in front of every string? Like for example having two string file and setup once in the app or maybe once in every screen to take this string file or the other one depending on feature flag value. One way would be for every string having a placeholder instead of
but it will still add quite a bite of complexity within the code

Muhammad Utbah

05/04/2023, 7:44 AM
One way to achieve this is to use string resources with placeholders, as you mentioned. Instead of hardcoding the string "foo" or "buzz" in your code, you can define a string resource with a placeholder like this: <string name="my_feature_text">This is a %s feature!</string> Then in your code, you can use the
method to retrieve the string resource and substitute the placeholder with the appropriate value depending on the feature flag: val featureFlag = /* retrieve feature flag value */ val textResId = if (featureFlag) R.string.my_feature_text_buzz else R.string.my_feature_text_foo val text = getString(textResId, if (featureFlag) "buzz" else "foo") This way, you only need to define two string resources with different placeholders for each variant of the feature name, and the if/else statement is only needed once to retrieve the appropriate resource ID. Another option is to use a library like Flipper to manage feature flags, which can make it easier to switch between different feature flag configurations and manage the visibility of the new name in your app. With Flipper, you can define feature flags and toggle them on or off from a central dashboard, and use these flags to control the visibility of UI elements or other app behavior.