How can I configure sqflite locale? Or how can I sort results by Chinese character? #942
-
I want to sort my query results by Chinese character Pinyin, using Flutter and sqflite. For example: I have found out a solution that use
But I didn't find So how can I call configure sqflite's locale just like native android? Or, how can I sort my query reuslts by Chinese character pinyin in sqflite? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Indeed there is not current way to call this specific Android API so I'm not sure how it could be exposed. You will likely see similar behaviors on other platforms (ios) that you will need to solve. Currently I solve this in my own applications (as i have similar issue in French) by adding an extra sort field where I store, in my case a lower case version removing diacritics (https://pub.dev/packages/diacritic). Maybe something like https://pub.dev/packages/pinyinizer could provide a solution for Pinyin. If you indeed need an android solution only using setLocale I could add an extra API, document it as Android only that take a locale String (zh-CN, en-US), whatever can be easily parsed to a locale on Android. It would throw an Unimplemented exception on other platforms. |
Beta Was this translation helpful? Give feedback.
-
Ok your requirements look fine for me. Let me work on that. If you have any clue on the best way to document, parse and format the locale (for example en-US vs en_US, I'd like the input locale being a simple string), your input is welcome! |
Beta Was this translation helpful? Give feedback.
-
@Aoi-hosizora I have publish sqflite 2.2.5-0 that suppors calling setLocale. Let me know if that works for you. setLocale on AndroidAndroid has a specific setLocale API that allows sorting localized field according to a locale using query like: SELECT * FROM Test ORDER BY name COLLATE LOCALIZED ASC There is an extra Android only API to specify the locale to use: await database.setLocale('fr-FR'); This API must be called during onConfigure (each time you open the database). The specified IETF BCP 47 language tag var db = await openDatabase(path,
onConfigure: (db) async {
await db.androidSetLocale('zh-CN');
},
version: 1,
onCreate: (db, v) async {
await db.execute('CREATE TABLE Test(name TEXT)');
});
// Localized sorting.
var result = await db.query('Test', orderBy: 'name COLLATE LOCALIZED ASC')); |
Beta Was this translation helpful? Give feedback.
@Aoi-hosizora I have publish sqflite 2.2.5-0 that suppors calling setLocale.
Let me know if that works for you.
setLocale on Android
Android has a specific setLocale API that allows sorting localized field according to a locale using query like:
There is an extra Android only API to specify the locale to use:
This API must be called during onConfigure (each time you open the database). The specified IETF BCP 47 language tag
string (en-US, zh-CN, fr-FR, zh-Hant-TW, ...) must be as defined in
Locale.forLanguageTag
in Android/Java documentation.