1

I have a list spinner item in English. When user choose an item, press Confirm it will save value for selectedItem in spinner to SQLite as a password. Now the app support other language, the value in spinner change base on the language but it's must return true when that value equal with English value save in database.

Ex: Spinner list item in English: Flag, Hat, Table.

Spinner list item in German: Flagge, Hut, Tabelle.

When user choose Flag, it save Flag in database as password. When app loads again, user choose Flag (in English) or Flagge (in German), it must return that is the right pass. If we have multi DB for multi languge, we must update all DB with the right translation for the value in spinner but it's offline app. Is there any solution ?

  • Can you share some code and also explain one thing to me, the problem is: You need o save the selected item in english and then when you load the value you need to show the item in other language? – GhostDerfel Feb 28 '14 at 02:58

3 Answers3

2

Use string resources with language qualifiers.

for example

res/strings.xml

<strings>
    <string name="flag">Flag</string>
    <string name="computer">Computer</string>
    <string name="table">Table</string>
</strings>

res/strings-de.xml

<strings>
    <string name="flag">Flagge</string>
    <string name="computer">Computer</string>
    <string name="table">Tabelle</string>
</strings>

Calling the context.getResources().getString("flag") method will return the String that belongs to the language the user has selected in the system settings. If you do not provide strings for the user's language, it will just use the strings from the regular strings.xml.

Use the string's identifier int (for example, R.string.flag) as the key for your spinner adapter to make sure the database key is the same on every language.

Mark Buikema
  • 2,483
  • 30
  • 53
  • 1
    but I need to save and load value from sqlite database – user3342507 Feb 28 '14 at 14:16
  • How do I retrieve the key from flag? onItemSelected returns flagge, what should I do next? The string's identifier int (for example, R.string.flag) for as much as I know isn't persistent across launches/update installs. Should you be leveraging string-array resource? In the end spinner isn't about the list, but the map of text values, mapped to ids? How do I retrieve the key from flag? onItemSelected returns flagge, what should I do next? The string's identifier int (eg, R.string.flag) for as much as I know isn't persistent across launches/update installs. Should we leverage array res? – Bato-Bair Tsyrenov Apr 25 '19 at 21:55
  • In the end spinner isn't about the list, but the map of text values, mapped to ids? @Mark Buikema – Bato-Bair Tsyrenov Apr 25 '19 at 21:55
0

you can try to manage this situation with a flag

  if (Locale.getDefault().getDisplayLanguage().equals("Deutsch")) {
       //put flag here
    }

After that see what flag language is and make your action

Also if you have resources for other language put it into the correct folder like:

values-de
drawable-de

etc

Lucian Novac
  • 1,255
  • 12
  • 18
  • I use sqlite database, so you mean that I create each database for each language ? – user3342507 Feb 28 '14 at 14:37
  • you can create other table by name tablename_de maintain your name and add at the end _de if you detect de language – Lucian Novac Feb 28 '14 at 14:41
  • but user only choose one time and so we have translate, insert that value to each difference language-database ? the app is offline and cannot user google translate – user3342507 Feb 28 '14 at 15:53
0

I don`t know if this is the best approach but was a solution I used in one of my projects.

At first I have created a table to store data:

CREATE TABLE my_other_table
( id INTEGER PRIMARY KEY AUTOINCREMENT,
  potato TEXT);

Then I created a table to store the translations:

CREATE TABLE translations
(id INTEGER,
language TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY(id) REFERENCES my_other_table(id));

On my database helper class I have a method to retrieve the right values:

public List<MyObject> getValues() {
        List<MyObject> myObjects = new ArrayList<MyObject>();
        openDatabase();
        Cursor cursor = database.query(DBHelper.TABLE_FOR_MY_OBJECT,arrayWithAllMyColumns, null,null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Challange challange = cursorToChallange(cursor);
            Cursor descriptionCursor = database.query(DBHelper.TABLE_TRANSLATIONS, new String[]{DBHelper.COLUMN_VALUE}, DBHelper.COLUMN_LANGUAGE+" = ?",new String[]{Locale.getDefault().toString()}, null, null, null);
            myObject.setDescription(descriptionCursor.getString(0));
            myObjects.add(myObject);
            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();
        return myObjects;
    }

The code above is just to have an idea, if you need more help just ask...

GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • I have edit my question. How I get value from user then translate it and update to table TRANSLATION ? It's offline app. – user3342507 Feb 28 '14 at 16:13