Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: referring to ressources in custom xml

i have a multilingual android app, where i have put the different translations in the strings.xml in the respective directory.

now i also have a custom xml file, where i would like to reference texts like this:

<?xml version="1.0" encoding="UTF-8"?>
<rooms>
    <room title="@+string/localizedtext" />
</rooms>

now when i read the title attribute in my code, i obviously get the unresolved string "@+string/localizedtext" like it is. is it possible to somehow resolve this link to the localized text automatically?

thanks!

like image 606
clamp Avatar asked Nov 24 '10 14:11

clamp


People also ask

How do you get Android resources to use in your code?

Accessing Resources in Code When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.

What is resource in XML?

xsd). The XML elements represent resource data when you save a project in the XML format. Resources is a child of the Project element. For more information, see Project Elements and XML Structure.

Can integers be stored as resources in XML?

Integer. An integer defined in XML. Note: An integer is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine integer resources with other simple resources in the one XML file, under one <resources> element.


2 Answers

Almost a year later:

    public static String getStringResource(Context context, String thingie) {
        try {
            String[] split = thingie.split("/");
            String pack = split[0].replace("@", "");
            String name = split[1];
            int id = context.getResources().getIdentifier(name, pack, context.getPackageName());
            return context.getResources().getString(id);
        } catch (Exception e) {
            return thingie;
        }
    }

That'll do it.

like image 164
RJ M Avatar answered Nov 15 '22 19:11

RJ M


This might seem like a broad answer but I believe it'll clarify a lot of things for people who spent hours looking for it (I'm one of them).

The short answer is yes, you can use references in custom XML, not just for strings, but that's the example I use, for ease of understanding.

Considering the context:

  • res/values/strings.xml
    (Default strings, usually en-US for convenience but that's up to the developer)
    <resources>
        <string name="sample_string">This is a sample string.</string>
    </resources>
  • res/values-fr/strings.xml
    (Localized french strings)
    <resources>
        <string name="sample_string">Ceci est un exemple de chaîne</string>
    </resources>
  • res/xml/test.xml
    (Custom XML file)
    <!-- @string/sample_string identifies both
         the default and french localized strings,
         the system settings determine which is used at runtime.
    -->
    <test>
        <sample name="sampleName" text="@string/sample_string"/>
    </test>
  • src/com/example/app/TestXmlParser.java
    //Omitted imports for clarity.

    public class testXmlParser {
        public static final String ns = null;

        public int parse(XmlResourceParser parser) throws XmlPullParserException,
                                                          IOException{
            while(parser.next() != XmlPullParser.END_DOCUMENT){
                if(parser.getEventType() == XmlPullParser.START_TAG){
                    if(parser.getName().equalsIgnoreCase("sample")){

                        // This is what matters, we're getting a
                        // resource identifier and returning it.
                        return parser.getAttributeResourceValue(ns, "text", -1);    
                    }
                }
            }
            return -1;
        }

Use String getText(int id) to obtain the string corresponding to id (localized, if available).

Using the example above it would amount to replace :

//Return the resource id
return parser.getAttributeResourceValue(ns, "text", -1);

with :

//Return the localized string corresponding to the id.
int id = parser.getAttributeResourceValue(ns, "text", -1);
return getString(id); 
like image 22
kyis Avatar answered Nov 15 '22 20:11

kyis