0

I want to have an Android App with 2 languages: English and German. The app should be used by multiple users at the same device. Now I have a problem figuring out how to implement this. I know that basically I can/should the string resource files. However, there are 2 big drawbacks as why to I can't use them:

  1. I sometimes use the Strings from a Static context and you can't call those resources from a static context (without any 'hack')
  2. More importantly: I would like to write orders in a database and depending on the user's choice they should be displayed either in German or English. When I use String resources I can't do this because when e.g. a German user orders something it is being written as German into the database and when an English user wants to see the order in English it is not possible as it was only stored as a German string into the database (it is not only about orders, but also about other aspects where I need both German and English)

So my current workaround is to always have 2 versions of each of my 8 database tables. One in English and one in German. And every time I write something into the databases, I write it in the 2 database versions. Whenever something should be used from the database, I use an if-statement to ask for the currently used language and then make a database query either in the English or German tables.

Now my question is, whether you know a more convenient way of doing this or is this the way to go? Basically it is not difficult to impelement it like this. However, the coding effort is quite high as I always have to insert everything in 2 databases and always have to check at many positions the currently used language of the app an depending on that make database queries.

I'd appreciate every comment and suggestion from you and would be quite thankful for your help.

VanessaF
  • 515
  • 11
  • 36
  • Isn't the German alphabet a superset of the English alphabet? In other words, the German alphabet contains **all** the letters in the English alphabet plus some extra characters. If you are using Unicode characters then this shouldn't be a problem. – Abra Jan 30 '21 at 08:56
  • Just pass the current `Context` into the `static` context... and honestly, throwing complexity at a problem usually doesn't make it any less of a problem. UTF-8 can display English and German letters without the least problem. – Martin Zeitler Jan 30 '21 at 09:03
  • Thanks for the answers so far. The problem is not the alphabet but the 2 points I mentione in my posts. – VanessaF Jan 30 '21 at 09:04
  • That's one problem which you may have created by throwing complexity at a non-problem. – Martin Zeitler Jan 30 '21 at 09:05
  • @a_local_nobody: First of all I do not know how to not use static methods in my app. But more importantly this will not solve my 2. problem that I posted with the 2 language versions – VanessaF Jan 30 '21 at 09:06
  • @MartinZeitler: What do you mean by a non-problem? I wrote about the 2 problems that I have been facing and I also wrote about how I deal with them. I just wanted to ask if there is a more convenient way of doing this – VanessaF Jan 30 '21 at 09:08
  • well if you're inserting strings.xml values into a db, it shouldn't really matter which langauge gets saved in there, right ? – a_local_nobody Jan 30 '21 at 09:08
  • @a_local_nobody: If an English user uses the app and something is written in the database it will be english. So how can now a German user access this information in German if is only saved in English? – VanessaF Jan 30 '21 at 09:09
  • @a_local_nobody: YES – VanessaF Jan 30 '21 at 09:12
  • @MartinZeitler: It is not about "UTF-8 can display English and German letters without the least problem." This is not the problem at all. I have English and German users for the same app at the same device. The app is commonly used and if an English user 'writes' something in English into the database, the German user can only 'read' it in English and not in German – VanessaF Jan 30 '21 at 09:15
  • @VanessaF Simply load these string-resources from two different resources files - or provide one column per language; else you'd have to mess around with at least two table-names, or even worse: two databases. – Martin Zeitler Jan 30 '21 at 09:15
  • @MartinZeitler: Thanks for your answer. Basically I do not understand what you mean by saying "Simply load these string-resources from two different files "? The other solution with one column per language makes it more difficult I gues compared to my current solution with the 2 tables, as I have many entries in all those tables and I would have to be extremely carefull about the index positions in every query – VanessaF Jan 30 '21 at 09:17
  • The opposite is the case; one row with two columns will always have exactly the same ID. Better get rid of all that useless complexity. See the [KISS](https://en.wikipedia.org/wiki/KISS_principle) principle. – Martin Zeitler Jan 30 '21 at 09:19
  • @MartinZeitler: I already have 15 entries in the table and Java methods for inserting and updating take 15 parameters. This is already quite much. Doubling the entries in the database tables to 30 would not be convinient and the method calls in Java would consists of 30 parameters which makes it even harder to read – VanessaF Jan 30 '21 at 09:24
  • Further this would not change anything as to making an if question all the time to check which is the currently used language. What do you mean by "Simply load these string-resources from two different resources files"? Bascially I have an English and a German String resource file as suggested. But this does not solve the problem of different users at the same app at the same device. As said before, when an English user writes something into the db, the German user can't see the Germand version of it. – VanessaF Jan 30 '21 at 09:25
  • @a_local_nobody: Any idea how I can do this or what would you advice me to do? – VanessaF Jan 30 '21 at 13:52
  • @a_local_nobody: Yes of course, but maybe you have an idea? Or would you say that my currently used approach with two database tables tables for each language is the way to go? – VanessaF Jan 30 '21 at 17:37
  • @a_local_nobody: You wrote "if you're inserting strings.xml values into a db, it shouldn't really matter which langauge gets saved in there". What do you exactly mean by that? Normally I do not store string.xml files in a database but rather normal strings. Can I also store the individual file in the SQLite database? What advantage would this have? – VanessaF Jan 30 '21 at 17:46

2 Answers2

0
  1. In my opinion the need to get strings from static context tells about bad design of your app. Or maybe you have some specific case - if so you should post an example.
  2. You can simplify your solution - just create separate table for translations. This way you will have one database and 9 tables. So all tables that should contain some text in different languages will contain an id of the row of new table. And this table with translations will have languages as columns. The drawback of this solution is that you have to use queries with joins, but it shouldn't be difficult if you will use room.
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
  • 1) Sometine you nee to use static methods when you want to access methods without creating an instance of the class. 2) I do not understand what you mean by " just create separate table for translation. ". I have 2 tables versions for every table. However, this does not solve the problem of having multiple users at the same device – VanessaF Jan 30 '21 at 09:45
  • Further I do not use Room but SQLiteOpenHelper – VanessaF Jan 30 '21 at 09:49
  • 1. So you should pass context to those methods. And inside just get desired string from context (Context.getString(id)). 2. You have 2 databases, each one with 8 tables. But you can have one database with 9 tables, this additional table will contain all needed translations. And other tables will refer to it. – Kiryl Tkach Jan 30 '21 at 09:53
  • Thanks Kiryl for your answer. 1) You can't really natively access static string resources in Android. It is only possible with something that I consider to be a 'hack' (and I am not the only , see https://stackoverflow.com/questions/4391720/how-can-i-get-a-resource-content-from-a-static-context). 2) I still do not understand what you mean. If I have two databases with 8 tables (on db for each language) then I would need 1 database with 16 tables. Further, the items in the db are being dynamically created by the users. So I would have to dynamically translate everything wich is not possible – VanessaF Jan 30 '21 at 09:59
  • 1. Yeah, that solution is hacky. But if you have your own static methods as I told you can just pass context there as parameter when calling them from non-static methods. 2. Can you provide an example of any simple table that you have (what are the columns there)? – Kiryl Tkach Jan 30 '21 at 10:04
  • Thanks Kiryl for your answer ane effort. I really appreciate it. 1) I tried so many different options accessing String resource files from a static context and none of them worked properly without using a 'hack'. 2) One example is an Order Table with the following columns: 1. OrderId, 2. OrderItem, 3. OrderQuantitiy, 4. OrderDate, 5. OrderOption1, 6.OrderOption2, 7. OrderOption3, 8. OrderComment, 9.Order_OtherInformation – VanessaF Jan 30 '21 at 10:24
  • 2. So I guess OrderComment should be translatable in both languages. So instead of OrderComment you can use OrderCommentTranslationId, and this column will contain the id from the translations table, not the comment itself. – Kiryl Tkach Jan 30 '21 at 11:14
  • Thanks Kiryl for your comment and effort. I really appreciate it. I still do not understand why your approach is beneficial for me. Basically as far as I see it there is no advantage of applying it and I would still have the core problem with the dynamic generated entries. Further, I do not see how one table could include the information of 8 tables for translation. – VanessaF Jan 30 '21 at 11:48
  • How could your approach solve the problem with dynamically generated content that should be available immediately in both languages (as the app is used by different people on the same device) – VanessaF Jan 30 '21 at 13:52
  • Any further comments on my last comment? I'd appreciate every further comment from you. – VanessaF Jan 31 '21 at 09:57
  • That table with all translations will contain 3 columns - id, english and german. And all the other tables instead of storing text will store the id of the row of translations table. – Kiryl Tkach Feb 01 '21 at 16:40
  • Thanks for your answer. What do you mean by "all other tables" – VanessaF Feb 01 '21 at 18:06
  • All the other tables I mean yours 8 tables – Kiryl Tkach Feb 02 '21 at 13:17
  • Thanks Kiryl for your further comment and effort. I really appreciate it. But with your approach I would still have to use if-statements all the time to check the current language of the app. So I do not see any convincing benefit compared to my currently used approach with two language versions for each database table. Further, in your appraoch the single database tables would be difficult to read from 'outside' as there would only be references (id) as entries. – VanessaF Feb 02 '21 at 18:53
0

If you want add "Hindi" language then you can create \res\values-hi\arrays.xml and add your custom hindi strings and use it directly.

  1. For Hindi Language -

     <resources>
         <string name="app_name">ऐप्प का  नाम </string>
         <string name="Verify Otp">ओटीपी सत्यापित करें</string>
         <string name="Sign in">साइन इन करें</string>
          <string name="reigster">रजिस्टर</string>
     </resources>
    

you can create different type of value folder for different languages. or, you can use translate editor to add different language. Select -> Project -> App -> Translation Editor Add your strings key and translate your key in selected language. You can better understand after see below screenshot:- Translation Editor

  • Thanks for the answe Geetika. I have English and German users for the same app at the same device. The app is commonly used and if an English user 'writes' something in English into the database, the German user can only 'read' it in English and not in German. So this is bascially the problem that the entries are dynamically created and they should be available in both languages. I guess I have no other option instead of using separate database tables for every language (as I have already done) – VanessaF Jan 30 '21 at 11:50