Good practices of use of Database and String.xml

5

A database with several tables . One of them for example transporte with the following attributes:

  • Nombre
  • Descripción
  • Precio

At first the application will be available in two languages.

What to save in the Database? That is, the description must be in Spanish and English. Then in the BD, in the description field, do I keep the name of the variable that will be stored in the string.xml file of each language? and in string.xml of each language the content will be written.

Otherwise the database should have one field for each multilingual attribute. For example:

  • Nombre_es
  • Nombre_en
  • Descripcion_es
  • Descripcion_en
  • Precio

But this second case I think complicates more the app because depending on the current language of the user will be made different SQL queries.

What is the best practice to develop in this type of case?

Edit

As an example I have put the content of 1 tabla of the BD but it will contain several relational tables. That's where the doubt comes from. To carry out some tasks, it will be necessary to consult the BD with its inner join and other commands.

    
asked by aldakur 15.04.2016 в 14:24
source

3 answers

0

If it were only about locating the user interface, Android already has support for localization using resources such as strings.xml

More information on Android localization: Localizing with Resources

But nevertheless, from what I understand, you need the data that you store in your database to be searchable. In this case it is best to have a multi-language scheme in your BD.

Example: (Ignore data types)

This way in the main table you would have the primary key and all those attributes that do not vary according to the language, such as prices, quantities dates, etc.

In an intermediate table between the main table and the language table you will have one row for each transport-language combination that contains the data that does vary by language, in your case Nombre and Descripcion

    
answered by 15.04.2016 / 15:54
source
0

To enter context, if you use strings.xml files defined for a language, you do not need a database to store some information, unless you want a different schema than the localization one.

I think that in this case you should take advantage of the support with which Android account to change the information of your UI based on the language configuration. The localization scheme requires that you create folders and files containing the text and its identifier in different languages as required, which will be supported by your application.

For example, if we assume that our application supports English, Spanish and Romanian languages, we should have the following folders ....

The default text folder would be:

res/values/strings.xml

The folder to add the texts in English:

res/values-en/strings.xml

would contain:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="nom">Crazy Dog</string>
    <string name="desc">Crazy dog Transportation, the best company</string>
</resources>

The folder to add the texts in Spanish is:

res/values-es/strings.xml

would contain:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="nom">Perro Loco</string>
    <string name="desc">Transportes Perro Loco, la mejor empresa</string>
</resources>

The folder to add the texts in Romanian:

res/values-ro/strings.xml

would contain:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="nom">Câine nebun</string>
    <string name="desc">companie de transport Câine nebun, cea mai buna companie.</string>
</resources>

You would have something similar to this in your project:

Simply those descriptions defined in your strings.xml use them where required in your application:

String strNombre = context.getResources().getString(R.string.nom);
textViewName.setText(strNombre);
String strDescripcion = context.getResources().getString(R.string.desc)  
textViewDesc.setText(strDescripcion);

The great advantage is that automatically, depending on the language defined in your operating system, it loads the texts defined in the file strings.xml of the corresponding language.

My point of view regarding your questions:

  

What to save in the Database? That is, the description should   be in Spanish and English. Then in the BD, in the description field

I do not think it convenient or necessary to combine the use of a database with strings.xml in this case. If you choose to use a database, you would use the same scheme used by "Android Localization". The database would contain tables texts and texts, which would contain the same field names.

Nombre
Descripcion
Precio

You simply decide which table to take the texts and the code needed to display them would not have to change.

I consider it a greater advantage instead of a single table with different field names:

Nombre_es
Nombre_en
Descripcion_es
Descripcion_en
Precio

That in code you would have to validate in which field to use, it would become unnecessarily more complex if in the future you had support for other languages ...

  

Do I save the name of the variable that will be stored in the file?   string.xml of each language?

It is not necessary to save the name of the variable in a database, the variable is the same within the strings.xml, but the content in each file would be in different languages.

In this case, the advantage of using Strings.xml over a database is first of all the quick access to them through the R class and the code needed for the implementation is minimal.

    
answered by 15.04.2016 в 20:49
0

There is also the option of integrating multi-language into a database, implementing the technique used by the Wordpress QTranslate plugin

In the fields, where you need to have them in more than one language:

  • Name
  • Description

For each language to be included, you can include it and identify it:

<!--:es-->Nombre producto<!--:--><!--:en-->Name of product<!--:-->
<!--:es-->Descipción del producto<!--:--><!--:en-->Description of product<!--:-->

The following function is used to extract a string with a specific language.

public static String getText(String text, String lang) {
    String r = "<!--:" + lang + "-->(.*?)<!--:-->";
    Pattern p = Pattern.compile(r);
    Matcher m = p.matcher(text);
    if (m.find( )) {
        return m.group(1);
    } else return "";
}

Its use in PseudoCode

String Name = getDataOfTable("productos","nombre") //cargar los datos desde la BD
Name = getText(Name,"es"); //para obtener la cadena en español
    
answered by 16.04.2016 в 13:53