11

I do have a lot of language specific resources. There's one point in my Android apps where I do get a resource value and need to translate this value into the matching id. The value is not necessarily in the language specific file for the current language (en/de/...). It's somewhere in there ... and it's unique.

After reading the docs and this thread "How to get a resource id with a known resource name?" I thought that getIdentifier(String name, String defType, String defPackage) is the correct way to go but I can't get it to work. The result is always "0".

This code is part of an activity and I'm on Android 2.2.

Is it possible that Android doesn't take all resource files into account and searches just in the current language specific one?

For an example (current language is "de"):

File: values-en/strings.xml
<resources>
   <string name="txt_afirsttext">A first text</string>
   <string name="txt_asecondtext">A second text</string>
</resources>

File: values-de/strings.xml
<resources>
   <string name="txt_afirsttext">Ein erster Text</string>
   <string name="txt_asecondtext">Ein zweiter Text</string>
</resources>

// Now I want to find the ID to this "en" text ...
String testValue = "A second text";
int i = this.getResources().getIdentifier(testValue, "strings", this.getPackageName());

// ... and translate it to the actual "de" language
String translatedValue = this.getResources().getString(i);

To make things clear. I don't want to miss-use string resources as a database. It's only one part that occurs on very rare situations.

Community
  • 1
  • 1
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85

2 Answers2

29

You are using the getIdentifier wrong. You don't specify the text but the identifier of the text, in your case that would be txt_afirsttext or txt_asecondtext.

int i = this.getResources().
             getIdentifier("txt_asecondtext", "string", this.getPackageName());

I think there's no way to locate a String resource identifier by its content. It would be like passing a byte stream to locate a drawable.

Does your application really need this weird way of getting resources? I'm quite sure you could use a better approach to dynamically load resources, though your first paragraph describes your scenario as a mysterious one :)

edit: Answer fixed. Thanks to tdroza for the correction.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
Maragues
  • 37,861
  • 14
  • 95
  • 96
  • Thanks for your answer. Seems that I need to go for an additional table for all the language specific strings. – Harald Wilhelm Oct 11 '10 at 16:21
  • Oops, sorry did to fast. The problem is that I do get, at one special part, external data. This transfered data has no keys it's just a value in one of 10th of languages. I even don't know the language. It's just the value I get. I do have a list of possible values and attached keys. As these values are needed for the UI as well they are part of the various "values-xx" directories. My idea was to save a second data store for thousands of text pairs and do a lookup of the values in the resources. Ok, bad idea. I go for another table. Thanks for your help. – Harald Wilhelm Oct 11 '10 at 16:28
  • Have you found a solution more suitable then using a table? (might as well replace built-in android string localization altogether) – Rafael Nobre May 18 '12 at 21:29
11

One minor but important correction to the code example above: the resource type must be singular not plural. i.e. "string" instead of "strings" - same goes for drawable, id, array etc.

E.g.

int i = this.getResources().getIdentifier(testValue, "string", this.getPackageName());
tdroza
  • 111
  • 1
  • 3
  • 1
    The statement that the resource type should be singular is not trye in all cases. When you are searching for a plural resource, make sure to use "plurals" as the resource type. This has cost me quite some time to discover ;) – Peter Sep 24 '14 at 14:12