]> git.openstreetmap.org Git - rails.git/blobdiff - lib/locale.rb
Don't order by id when getting last records
[rails.git] / lib / locale.rb
index e10f066329a0fd4e126d1416c55271f86fbc564b..5931301b5af6bf46f3291aa1b86418226b6b6857 100644 (file)
@@ -10,12 +10,16 @@ class Locale < I18n::Locale::Tag::Rfc4646
       preferred.expand & self
     end
 
-    def preferred(preferred)
-      candidates(preferred).first
+    def preferred(preferred, options = { :default => Locale.default })
+      candidates(preferred).first || options[:default]
     end
 
     def expand
-      map(&:candidates).flatten.uniq << Locale.default
+      List.new(reverse.each_with_object([]) do |locale, expanded|
+                 locale.candidates.uniq.reverse_each do |candidate|
+                   expanded << candidate if candidate == locale || expanded.exclude?(candidate)
+                 end
+               end.reverse.uniq)
     end
   end
 
@@ -28,17 +32,23 @@ class Locale < I18n::Locale::Tag::Rfc4646
   end
 
   def self.available
-    @available ||= List.new(I18n.available_locales)
+    @available ||= List.new(I18n.available_locales).reject!(&:invalid?)
+  end
+
+  def invalid?
+    I18n.t("activerecord.models.acl", :locale => self, :fallback => false, :raise => true).nil?
+  rescue I18n::MissingTranslationData
+    true
   end
 
   def candidates
-    [self.class.new(language, script, region, variant),
-     self.class.new(language, script, region),
-     self.class.new(language, script, nil, variant),
-     self.class.new(language, script),
-     self.class.new(language, nil, region, variant),
-     self.class.new(language, nil, region),
-     self.class.new(language, nil, nil, variant),
-     self.class.new(language)]
+    List.new([self.class.new(language, script, region, variant),
+              self.class.new(language, script, region),
+              self.class.new(language, script, nil, variant),
+              self.class.new(language, script),
+              self.class.new(language, nil, region, variant),
+              self.class.new(language, nil, region),
+              self.class.new(language, nil, nil, variant),
+              self.class.new(language)])
   end
 end