I'm making an android app with multi-language support.
I have around 30 language to support my app.
I'm playing within 2 Case.
case 1 . First case is creating values folder of every language in res folder.
case 2. Second case Get every language content with a key from server
e.g. TXT_HELLO = HELLO if user choose English
TXT_HELLO. = HOLA. if user choose Spanish
Suppose from language list I select Spanish during language change
then server give me list of every Spanish content with a same key
[Key] = [Value]
e.g. TXT_HELLO = Hola
TXT_GOOD = Bueno
& then I save this value with key is session & all key also in my Constant class
Similarly In Case if I select English:
[Key] = [Value]
e.g. TXT_HELLO = Hello
TXT_GOOD = good. as Soon
This is how my kotlin code work.
fun getLanguageValue(key : String)->{
return sharedPre.getString(key)
}
txtViewHello.text = getLanguageValue(MyConstant.TXT_HELLO)
cons and pros of case 1 .
pros : It handle by system and good memory management
cons : Language only can add on new release apk. if some word is wrong then can't be correct without release.
cons and pros of case 2 .
pros : if some word is wrong then can correct online. No need to release new APK or can add more language at any time.
cons : It handle by me and initialization of objects is more complex.
So please suggest me what is best approach & what is best practice to change language.
You could attempt to write your own language switcher that does this, but I'd advise against it. This is basically fighting the system, and is very hard to handle properly.
The best way to handle multiple languages is to use resource qualifiers, and make the system do the hard work for you. It will also match the user's expectation that your app will be in the same language as the entire rest of their phone.
Related
I am trying to build an android app that the user can enter a string, and a list emoji related to that string would show up. (Just like Venmo app) For example:
case 1: User enters "pizz", and in the list there would be "π", note that the users enter "pizz", not pizza!
case 2: User enters "rabb", and in the list there would be "π" and "π°", note that the users enter "rabb", not rabbit!
What would be a good data structure and algorithm for this problem?
A trie is what your looking for. From Wikipedia
A trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is a kind of search treeβan ordered tree data structure ...
A trie is similar to a HashMap<K,V>, you can perform a lookup with keys and get a value. The difference is that you can also search by prefix. Given a prefix, it will find all the key-value pairs in the structure that have that prefix. It's basically the data structure for generating search suggestions.
General Idea:
Trie<String, String> t = new Trie<String, String>();
t.insert("pizza", "π");
t.insert("rabbit1", "π");
t.insert("rabbit2", "π°");
// then later...
t.findByPrefix("rabb"); // [π,π°]
Unfortunately, tries are too generic and are not present in any popular data structure libraries (like Java Collections Framework or Google Guava, for example). You'd have to implement one yourself or find an existing implementation and modify it.
I'd recommend:
Learning the theory. Watch this video. There are many more on YouTube that will teach you the basics. You can also search google for "N-way trie" and read notes about it.
Taking this class TrieST and modifying it. It's very similar (or already perfect) for what you need: http://algs4.cs.princeton.edu/52trie/TrieST.java.html see specifically thekeysWithPrefix method.
i was wondering if there is a chance to create comparable properties for objects defined by the user itself.
Following case: In my android app, the user creates a object "car", this object has predefined properties like color, size, doors, engine and so on ... but now, the user wants to add an individual property like "length" ... for that the user gets a plus button under the view to add this property ... now he can type in the wanted property, but what he dont want is to define the type of the input!
The users thinks "hey, its pretty obvious that length is expressed with a number so why i have to choose the type for this?"
I dont want to limit the user if i give them only predefined propertys.
I thought about saving every new parameter as a string, but then the values arent comparable anymore ... "900" is bigger than "1000" in a string comparison and so on. And i want to filter data and do queries later.
I dont disagree at all with the idea to let the user choose which type the field is, but i dont want to ask them too much in an android application.
If this all is not possible, how can i smartly get the information for the type from the user?
How can i handle this problem? Can someone give me a hint or keyword to search for?
Natural sorting might be the thing you are looking for. It will solve the problem with numbers.
I am implementing a preference activity so I can let choices to the user about what my app can and can't do and to define a user profile so I can change the way I request data.
I have one part where I need to show places to the user (restaurant, stores, etc) so I would like to be able to have a "range widget".
Let's say :
1 = poor
2 = cheap
3 = average
4 = rich
I want the user being able to say I want only store between 1 and 3 so it would need to be a widget with a double "circle" allowing the user to create a range.
Any idea of something like this existing ? (I am pretty sure I already seen something like this in the past but I can't remember where)
Take a look at RangeBar. It sounds exactly like what you need
just for curious, i got a question!
My app is almost ready.. I have implemented bilingual (English/Tamil) when users select thr preferred language in Settings then whole app gets converted into that language (i used custom Locale). Everything works fine.
My question is, Can we do the same to SQlite database? which fetch data automatically based on locale from table
"country-en"or"country-ta" ? is there any way? i heard that thr is a method SQLiteOpenHelper.onConfigure(setLocale()); to set locale in sqlitedatabase. i want to know how it works!
The method you describe above is not related to language localization for the client. It's related to locale options on the database (for example how to treat string comparisons with accented characters).
Anyway, if you think of it... what does localizing a database mean? You can either localize the structure or the data. Localizing the structure (table names...) doesn't make sens because the user is not aware of it. Localizing the data doesn't make sense either, because that means that if the users changes language settings, next time he uses your app he won't see his data!
If the DB only contains static data and you need to provide a different DB for different languages, you could use localization to lookup for the database filename.
Of course, yes, you can.
When you do your queries, just append "en" or "ta".
Something like "SELECT FROM Country_" + yourLocale + "..." - if you want to use two different tables.
OR you could use one single table with an integer field "Language" and you pass an int (0 = english, 1 = tamil - only for alphabetical order, which is easier to remind)...
Something like "... WHERE ... AND Language = 0"
I am new to both Android and Stack Overflow. I have started developing and Android App and I am wondering two things:
1) Is it possible to parametrize a TextView? Lets say I want to render a text message which states something like: "The user age is 38". Lets suppose that the user age is the result of an algorithm. Using some typical i18n framework I would write in my i18n file something like "The user age is {0}". Then at run time I would populate parameters accordingly. I haven't been able to figure out how to do this or similar approach in Android.
2) Let's suppose I have a complex object with many fields. Eg: PersonModel which has id, name, age, country, favorite video game, whatever. If I want to render all this information into a single layout in one of my activities the only way I have found is getting all needed TextViews by id and then populate them one by one through code.
I was wondering if there is some mapping / binding mechanism in which I can execute something like: render(myPerson, myView) and that automatically through reflection each of the model properties get mapped into each of the TextViews.
If someone has ever worked with SpringMVC, Im looking for something similar to their mechanism to map domain objects / models to views (e.g. spring:forms).
Thanks a lot in advanced for your help. Hope this is useful for somebody else =)
bye!
In answer to #1: You want String.format(). It'll let you do something like:
int age = 38;
String ageMessage = "The user age is %d";
myTextView.setText(String.format(ageMessage, age));
The two you'll use the most are %d for numbers and %s for strings. It uses printf format if you know it, if you don't there's a quicky tutorial in the Formatter docs.
For #2 I think you're doing it the best way there is (grab view hooks and fill them in manually). If you come across anything else I'd love to see it.