+ # Return the shortest language for the current
+ # GetText locale
+ def Language.from_locale
+ lang = locale.language
+ if locale.country
+ str = lang + "_#{locale.country}"
+ if Lang2Locale.value?(str)
+ # Get the shortest key in Lang2Locale which maps to the given lang_country
+ return Lang2Locale.select { |k, v| v == str }.transpose.first.map { |v| v.to_s }.sort { |a, b| a.length <=> b.length }.first
+ end
+ end
+ # lang_country didn't work, let's try lan
+ if Lang2Locale.value?(lang)
+ # Get the shortest key in Lang2Locale which maps to the given lang
+ return Lang2Locale.select { |k, v| v == lang }.transpose.first.map { |v| v.to_s }.sort { |a, b| a.length <=> b.length }.first
+ end
+ # all else fail, return 'english'
+ return 'english'
+ end
+