I have a Flutter App with Firestore as a backend for storing dynamic data shown in the app. I am adding localization support for multiple languages using the flutter_localizations
library as mentioned here.
My server data is loaded from Firestore and saved in a sqlite database. The data is loaded to class object and shown in a Flutter widget.
How do I restructure my class models, database and logic to show correct string based on app locale?
What I came up with so far:
Firestore:
- songs (collection)
-- QWSAWERFCUT (id)
-- title: Smells Like Teen Spirit
-- title_hi: स्मेल्स लाइक टीन स्पिरिट
-- title_mr: स्मेल्स लाइक टीन स्पिरिट
-- ....
Sqflite database:
db.execute(
"CREATE TABLE $activityTable("
"$colId TEXT PRIMARY KEY , "
"$colTitle TEXT, "
"$colTitle_hi TEXT, "
"$colTitle_mr TEXT, "
....
)
Dart Class:
class Activity {
String id;
String title;
String title_hi;
String title_mr;
...
// Function to get locale-specific data for each field
String getTitle(Locale locale){
if(locale==Locale('hi')) {
return title_hi;
}
else if(locale==Locale('mr')) {
return title_mr;
}
else{
return title;
}
}
}
Do I need to add each field in multiple languages to my Firestore, Database, class object and special function for each field to get locale-specific data, or is there a more efficient way of doing this?