From 05e0a11458b4f703a42fb08778df1320cf13d983 Mon Sep 17 00:00:00 2001 From: Andy Grundman Date: Sat, 11 Dec 2010 18:44:42 +0000 Subject: [PATCH] ICU collation support git-svn-id: file:///home/awy/mirror/slim/7.6/trunk/server@31616 62299810-d8cb-41fd-93b7-d32162e5a4a4 --- Slim/Utils/SQLiteHelper.pm | 58 +++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/Slim/Utils/SQLiteHelper.pm b/Slim/Utils/SQLiteHelper.pm index fbf6ae7350a..9de373349b1 100644 --- a/Slim/Utils/SQLiteHelper.pm +++ b/Slim/Utils/SQLiteHelper.pm @@ -151,10 +151,60 @@ sub on_connect_do { return $sql; } -# Built-in perllocale collation will sort using Unicode Collation Algorithm -# on systems with a properly installed locale. This appears to be fine on Linux, -# but not under OSX for some reason. -sub collate { 'COLLATE perllocale ' } +my $hasICU; +my $currentICU = ''; +sub collate { + # Use ICU if built into DBD::SQLite + if ( !defined $hasICU ) { + $hasICU = (DBD::SQLite->can('compile_options') && grep /ENABLE_ICU/, DBD::SQLite::compile_options()); + } + + if ($hasICU) { + my $lang = $prefs->get('language'); + + my $collation + = $lang eq 'CS' ? 'cs_CZ' + : $lang eq 'DA' ? 'da_DK' + : $lang eq 'DE' ? 'de_DE' + : $lang eq 'EN' ? 'en_US' + : $lang eq 'ES' ? 'es_ES' + : $lang eq 'FR' ? 'fr_FR' + : $lang eq 'HE' ? 'he_IL' + : $lang eq 'IT' ? 'it_IT' + : $lang eq 'JA' ? 'ja_JP' + : $lang eq 'NL' ? 'nl_NL' + : $lang eq 'NO' ? 'no_NO' + : $lang eq 'PL' ? 'pl_PL' + : $lang eq 'PT' ? 'pt_PT' + : $lang eq 'RU' ? 'ru_RU' + : $lang eq 'SV' ? 'sv_SE' + : $lang eq 'ZH_CN' ? 'zh_CN' + : 'en_US'; + + if ( $currentICU ne $collation ) { + if ( !Slim::Schema->hasLibrary() ) { + # XXX for i.e. ContributorTracks many_to_many + return "COLLATE $collation "; + } + + my $dbh = Slim::Schema->dbh; + + eval { $dbh->do("SELECT icu_load_collation('$collation', '$collation')") }; + if ( $@ ) { + $log->error("SQLite ICU collation $collation failed: $@"); + return 'COLLATE perllocale '; + } + + $currentICU = $collation; + } + + return "COLLATE $currentICU "; + } + + # Fallback to built-in perllocale collation to sort using Unicode Collation Algorithm + # on systems with a properly installed locale. + return 'COLLATE perllocale '; +} =head2 randomFunction()