I wonder if there is a way to read out the locale values of all existing values directories.
Let's say I've got the following directories under my res-Directory
[...]
values
values-de
values-nl
[...]
Now I need a method to get back the information that there is a locale of de and nl existing for the directory values.
Is there any way, if yes how?
Any help will be appreciated.
Regards,
Christian
Well, ideally, your app neither knows nor cares what resource sets you have. That is the whole point behind resource sets, after all -- to insulate your app from changes in resources.
That being said, one possibility is to write a script that is part of your build process that generates a file with your requested data (e.g., XML file containing the roster of resource sets) that you then read in at runtime.
Or, arrange to have a "magic value" in each set. For example, in res/values-de/strings.xml, you could have a lang_de string, and in res/values-nl/strings.xml, you could have a lang_nl string. Then, you can use reflection to iterate over your string resources and find those matching the lang_ pattern. This may be significantly slower than the first option, particularly if you have lots of string resources.
I know of no way to interrogate the system to find out what resource sets are defined.
Related
I uploaded local photos in drawable files. The photos are approximately 3 mb in size. Its showing this error. However The problem panel shows analizing for 15 minutes till now and it is still showing it. What could be the possible reason for such errors.
Resource name must start with a small case letter or an underscore('_').
For more rules regarding resource naming convention, you can refer to the below-mentioned medium article.
https://medium.com/#AkhilDad/a-designers-guide-for-naming-android-assets-f790359d11e5
There are a few conventions used in resources:
For resources that exist as separate files, they must be
lower_case_underscore_separated. The appt tool makes sure that your
files are only lower-case, because using mixed case can cause issues
on case-insensitive filesystems.
For resources declared only in values/... (attributes, strings, etc)
the convention is generally mixedCase.
There is a convention used sometimes to tag names with a
"classification" to have simple namespaces. This is for example
where you see things like layout_width and layout_alignLeft. In a
layout file the attributes for both the View and the parent layout
management are mixed together, even though they are different
owners. The "layout_*" convention ensures that there are no
conflicts between these names and it is easy to understand which
entity the name impacts.
For more information here is the complete discussion.
Are there conventions on how to name resources?
Resource name must start with letter. You can't start image name with digits.
The reason you can't have a resource with a numeric name is because variable names cannot start with numbers.
The error is actually because the resource name cannot start with a number. Resource name should start with a letter or underscore(_).
I create folder values-ru (for any region). But with using Ukrainian language or Belarus i have an error.
android.content.res.Resources$NotFoundException: String resource ID #0x7f0d0037
Tell me, why i got an error. Does i must create values-ru-uk and etc. for all region?
You should define all your strings in the default (source) language, regardless of what translations you're adding.
These goes in: values/strings.xml.
If this is missing (or the string is missing in that file) then I expect that's why you get an error.
You can then add as many language files as you need: e.g:
values-uk/strings.xml. (Ukrainian)
values-be/strings.xml. (Belarusian)
or specific regions thereof: e.g:
values-ru-rUA/strings.xml. (Russian in Ukraine)
Disclaimer: I'm not an Android developer, but I've written localisation software that generates strings files.
I am new here.
How do we go about separating the string values for different categories(fragments,activity, etc.)
For now I put everything in one file,that is the values/strings.xml. When the program gets too big, they are all jumbled together and hard to differentiate.
any advice?
thanks,
techfang
The filename is arbitrary. You can name your strings files as you want strings_my_activity for example
I'll try to give each string a meaningful name, haha. Then, have them listed in section, use newlines to separate them.
You can have multiple string resource files, so it is perfectly allowable for you to have (for example):
res/
strings.xml
main_activity_strings.xml
main_fragment_strings.xml
sub_fragment_strings.xml
The files themselves can have any allowable name really. What is important is what is in the file. So any entry ends up resolving to R.string.xxxxxxx
How desirable this approach is, is of course another question. You may find you want to keep 'global' strings (such as OK, Cancel, etc.) in the top-level strings.xml file.
I have two values directories - one for English and one default.
if the resource are located in any of those folders, i can use it.
But if there is no resource in default values directory and locale is other than English, it causes ResourceNotFoundException.
I understand why this happens and why Android was built this way (to prevent ambiguous resource usage in case there are more than one non-default resource files with this resource).
But is there any way to force Android use, for example, English resource bundle if resource couldn't be found?
The only option you have is to make sure the /res/values/strings.xml file contains every string in English but you must also have /res/values-no/strings.xml which has the Norwegian strings.
If /res/values-no/strings.xml doesn't have the resource it will drop back to /res/values/strings.xml. It's the only way to do it.
If an application label is localised, I have a requirement to retrieve all localisation variants. Effectively I need to know if the android:label points to a string reference and, if so, all the locale values present in the string resources. Any ideas?
I don't have a full answer but here's what I came up with.
Localization - Here is the official documentation for android localization. You can ensure that your android:label points to a string reference if you have it point to a key in the /res/values/strings.xml. Specific localization strings can be overridden by using qualifiers on the directory, for instance for French versions you can put them in /res/values-fr/strings.xml. Any values not found in the French version will default to the previous unqualified file.
I'm not sure about getting all the locale strings for a specific resource but there is getAssets().getLocales() which will return a string array of the locales the asset manager has resources for (I'd assume for any resource, not just a particular resource)
Lastly, if you don't need to do this at run time you could try to write a script/program to examine all the localization directories under /res and compile a list of all resources with their localized values.