0

The QA team on my project asked us to show the string keys instead of the actual values (as we show in any normal app), for testing purposes, but I don't know how to do it.

i.e.: <string name="BackButton">BACK</string>

They want to see BackButton instead of BACK

And they want for all strings on the strings.xml file (600 lines so far).

I found some answers like this:

int resID = getResources().getIdentifier(resourceName, "string", getPackageName());
textview.setText(resID);  

and this one

But even if I use the first example with getIdentifier, how would I do this for the whole project?

Project is on Android using Kotlin

BlitzkriegBlue
  • 101
  • 2
  • 14
  • 2
    you can try creating an alternative build config for QA, putting another set of string resources into that build type's resource directory, and then create script to replace everything between `>..<` to everything between `".."` in each line – Vladyslav Matviienko Jul 29 '19 at 09:13
  • Hi Vlad, that sounds like a good idea. Do you have any link recommendations for how to do such script? I've never done any script on android and don't even know where to start. Well, I've never done any scripting. =/ – BlitzkriegBlue Jul 29 '19 at 09:22
  • you actually need to do the script on the PC you are working on. – Vladyslav Matviienko Jul 29 '19 at 09:24

1 Answers1

4

As mentioned by Vladyslav in the comment I guess you can create a pre build task to modify your default strings.xml file but it can be risky because you'll override all your translations and if you're not using something like phraseapp to store them remotely, bad things might happen.

Otherwise, I guess you can do it dynamically. Create a Context extension like so

fun Context.getStringOrId(res: Int): String {
    return when {
        BuildConfig.DEBUG -> this.resources.getResourceName(res).split('/').last()
        else -> this.getString(res)
    }
}

In Debug you'll get the resource key or x.y.z.debug:string/key without the split You'll have to replace all your getString(res: Int): String by this method... I'm not a huge fan of this either.

Another idea could be to have the default strings.xml filled with

<string name="key">key</string>

And put the translations within values-<lang>-<country>/strings.xml But if your client is using a non supported locale, he'll see just the default keys..

MajorShepard
  • 403
  • 4
  • 14
  • Major, thanks so much for the help. I will try all the things you said and see which one sounds better for the situation. You were of great help, I really appreciate it! – BlitzkriegBlue Jul 29 '19 at 09:52