Ok, bad title, cause that is not possible. But it was the goal I wanted to achieve and there are some solutions for it. In Android development the strings.xml file is used to store all Strings in, so you can add support for multiple languages or at least have one place where you can change all your text.
Problem
In my case I had the following “problem” illustrated in this example.
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Flappy Bird</string> <string name="disclaimer_message">All content in Flappy Bird was borrowed from Nintendo.\n\n All rights of used media belong to Nintendo. The creator of Flappy Bird can't be hold repsonsible for any addiction. Play Flappy Bird at your own risk.\n\n Have fun!</string> </resources> |
I reused the same strings.xml in several applications and had to change the application name in both the app_name and disclaimer message. Using vi with something like :%s/Flappy Bird/Floppy Bird/g worked for me, but I wanted a better solution.
Using String formatting
One solution was to use string formatting in strings.xml. By using String formats we only have to change the application name in one place.
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Flappy Bird</string> <string name="content_owner">Nintendo</string> <string name="disclaimer_message">All content in %1$s was borrowed from %2$s.\n\n All rights of used media belong to %2$s. The creator of %1$s can't be hold repsonsible for any addiction. Play %1$s at your own risk.\n\n Have fun!</string> </resources> |
But using string formatting also means you have to change your Java code.
1 2 3 4 5 6 7 |
... String app_name = getResources.getString(R.string.app_name) String content_owner = getResources.getString(R.string.content_owner) String format = getResources().getString(R.string.disclaimer_message); String disclaimer_message = String.format(format, app_name, content_owner); ... |
This is a solution which works great if you reuse a lot of strings, like in a text adventure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- items --> <string name="item_beer">bottle of beer</string> <string name="item_chicken">rubber chicken</string> <string name="item_golden_key">golden key</string> ... <!-- actions --> <string name="action_eat_neg">You cannot eat a %1$s. You silly boy!</string> <string name="action_eat_pos">You ate the %1$s. You wish there is a toilet nearby.</string> <string name="action_open_neg">You try to open %1$s. No luck there!</string> <string name="action_open_pos">You successfully opened %1$s.</string> ... </resources> |
But for my goal it was a little bit of an overkill. I am lazy by nature, so I looked further for another solution, cause I didn’t want to change my Java code.
Using XML Entities
In XML/HTML you have several predefined well-known character entities, like < and > to display a < or > character. But it is also possible to declare new entities in your XML file. This case you don’t have to change your Java code and only make a couple of changes to your XML file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE resources [ <!ENTITY appname "Flappy Bird"> <!ENTITY contentowner "Nintendo"> ]> <resources> <string name="app_name">&appname;</string> <string name="disclaimer_message">All content in &appname; was borrowed from &contentowner;.\n\n All rights of used media belong to &contentowner;. The creator of &appname; can't be hold repsonsible for any addiction. Play &appname; at your own risk.\n\n Have fun!</string> </resources> |
Now I can reuse my strings.xml in other projects and simply change the defined entities.