force Time.format(...) to use local resources in android - android

android.text.format.Time uses System resources for getting month names, week days, etc. Now, I want to use my app in a locale that translations for that locale does not exist by default. Suppose I want to set custom names for weekdays. So, how can I use Time.format(...) method? How can I force it to use my resources instead of System's.
Any help will be appreciated, thanks.

You can define custom formats using either android.text.format.DateFormat or java.text.SimpleDateFormat. The latter lets you pass a custom DateFormatSymbols object, which should allow you to define your own names for months and weekdays, etc.

Related

gender-dependent locale String resources Android

I'm implementing an app in Hebrew, and I like it to be user-friendly in such way that at the first time the user logs on, there will be a question "Are you a male or a female?". After answering this question, I want most of the strings to be gender-dependent
(E.g. in Hebrew the question "Would you like some coffee?" will be
תרצה לשתות קפה?
for a male, and -
תרצי לשתות קפה?
for a female)
Meanwhile my app supports English and Unisex-Hebrew Locales, so I'm using String resources (like R.string.somevalue) and I know how to handle values-he and values-en.
Let's say I can ask for is_male() and is_locale_hebrew() at anytime, I saw this answer but it won't help my case since there are a hell lot of strings in my working-already app and I want the solution to add only xml files (hopefully) with the less needed change in my "Activity"s code.
I thought maybe overloading the parser that looks for the xml files will do the magic, but I have no clue where to start from.
My question divides into two parts:
A. How can I implement gender-dependnt String-resources?
B. (Opt) Some of the string-resources are good as unisex right now, is there any option to avoid copying those resources to the 2 new gender-dependent files and just add a default behavior of "if you don't find a string resource at values-he-male search for it in values-he"?
Thanks in advance!
Re'em
Same question for me. I plan to have (in addition to the default string.xml in English) iw (for male) and iw_fe (for female).
When the user selects his/her gender, I will change the locale (Set Locale programmatically )
As for using default values in Hebrew, I am still clueless. For now I will simply copy the Hebrew XML and change the required entries, leaving all the rest intact.
HTH
Noam

Where to store string values?In strings.xml or in constants class?

In android, we can store string values either in strings.xml file or in some constants class as static final variable.Is there some reason for selecting one over another in some circumstances?
In a nutshell:
value for use in code: use always constants class.Advantage: codes remain integrated and your package can be utilized in other projects/contexts. You can not do that with string.xml as it is not transported with your package.
value for display in UI: use string.xml. Advantage: you can use localization to display translated texts.
Some situation may arise when both option appears viable. You will have to then decide where are its related values are stored.
As a general rule, use the strings.xml because android uses that XML to enable translating your app into different languages, which it can't do with strings that are hardcoded.
The official android guide on localization say the following;
Move all strings into strings.xml
As you build your apps, remember not to hard code any string. Instead
declare all of your strings as resources in a default strings.xml file
which makes it easy to update and localize. Strings in strings.xml
file can be extracted, translated and integrated back into your app
(with appropriate qualifiers) without any changes to compiled code.
If you generate images with text, put those strings in strings.xml as
well, and regenerate the images after translation.
Strings that are not going to be displayed to the user in any way needn't be stored in the XML, because they will never need translating, and you probably don't want the android system tampering with them in ways you might not know about during runtime.
If the string value is used to display in UI store in Strings.xml Otherwise keep it in code. There can be JSONTags, Key for different api/Thirdparty libraries.These kind of things should be kept in code itself.
strings.xml it is used for localization and needs a context to retrieve the content of a String. If you need a java constant to be accessed in different classes, you a public static final String member. If the string is a message for the user you should use strings.xml
If strings represent text readable by user, and which could potentially be translated to other languages (names of buttons, labels, notification/error messages, etc.) then they should be in strings.xml (actually, it can be any file name you like, not just "strings").
If string is some constant which is used in the app internally (bundle/intent keys, fragments tags, etc.) they should be declared in class
It depends, if it is a text string that will be translated or displayed to the user then for 118n sake, you will want to put in into strings.xml.
However, if the string is something like a server url or api code then you'll want to store those in code as a public static final String

How can I have both res/values-pt_PT and res/values-pt_BR on Android?

I keep getting an error when creating folders for internationalization. But the errors appear just for folders with the name like values-xx_XX. If I have values-xx everything is ok, but like I asked in the title I want to make 2 separate folders for the (aprox.)the same language: values-pt_PT,values-pt_BR. How can I do that without getting any errors? Note: The error is not specified anywhere, the eclipse is just marking the folder with a red cross and doesn't allow me to run the project.
Every hint is appreciated. Thank you! :)
Use the format values-xx-rXX instead of values-xx_XX. In this instance you should use values-pt-rPT and values-pt-rBR.
See http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
The language is defined by a two-letter ISO 639-1 language code,
optionally followed by a two letter ISO 3166-1-alpha-2 region code
(preceded by lowercase "r").
I haven't done it myself, but after a quick search here, I found Setting region based Local in android where they suggest using the constructor as you did:
Locale locale = new Locale("ar","SA"); //(language,country)
But also, if you wish to pass it as one parameter, you'd still leverage the lowercase "r" as in the "values" folder, like this:
Locale locale = new Locale("ar-rSA");
Hope it helps :)

Performance at working with strings android

Could somebody tell me what is better in terms of performance?
Is it better to save 2 strings at string.xml, like 'abc' and 'abc:'
Or should I save only the first one and concatenate ':' when needed at Java coding ???
Very difficult to answer depending on what your strings will represent and what you need to append. Localization is also an issue, for example...
Dog // English
Chien // French
Hund // German
Using string resources allows you to create different resource files depending on the locale of the device and Android will automatically use the right localized string resource file. If all you need to do is append a single character such as : then you'll double every string for every language.
If you choose to only save the basic strings and append the character using code, then the code will be universal and you'll simply need to append the character to whatever localized word - potentially a lot more efficient.
Both from storage perspective and performance you should save only "abc";
getting extra data from disk takes far longer as some quick in-memory actions.
storing the same data twice is bad practice in general
If you have to concatenate multiple strings you should use StringBuilder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html
It's much faster then using '+' or '.concat()'

How to set different locales in android?

In my application, I need to display strings according to user locale selection. So, I put my strings.xml in values-en, values-ko, etc. How can I set locale us, australia i.e; values-en_US, values-en_AU? But it's throwing an error? Can any one tell me how to set these locales in my code?
Use res/values-en-rUS/ (replacing the _ with -r).
I am not clear with your question but if you are not sure as what name you need to for a specified locale refer to these links
http://developer.android.com/intl/de/reference/java/util/Locale.html
http://groups.google.com/group/android-developers/web/localizing-android-apps-draft
Android by default will take the resources from respective language that has been selected on the device/stimulator.
There is International Standard for Locale.....like values-en for English....values-ko for Korean...You cant modify the standard..It would work if you keep the folder name as values-ko.

Categories

Resources