Module: FlagIcon

Defined in:
lib/flag_icon.rb,
lib/flag_icon/rails.rb,
lib/flag_icon/helpers.rb,
lib/flag_icon/railtie.rb,
lib/flag_icon/rails/engine.rb,
lib/flag_icon/rails/version.rb

Defined Under Namespace

Modules: Countries, Rails Classes: Railtie

Class Method Summary collapse

Class Method Details

.available_languagesObject

Define in your application_helper to override the available languages

Examples:

['English', :en], ['Spanish', :es]

#default

def available_languages
  Language.all.order(:language_locale).pluck(:language_locale)
end

Returns:

  • Array


87
88
89
# File 'lib/flag_icon/helpers.rb', line 87

def available_languages
  @languages ||= FlagIcon::Countries::LANGUAGES.keys
end

.country_icon(code, title: "Available in #{country_name(code)}") ⇒ Object

It returns HTML element with country icon and title

Examples:

country_icon('gr')

Parameters:

  • code

    -> country iso code

Returns:

  • String


64
65
66
# File 'lib/flag_icon/helpers.rb', line 64

def country_icon(code, title: "Available in #{country_name(code)}")
  "<span class='flag-icon flag-icon-#{code}' title='#{title}'></span>"
end

.country_name(code) ⇒ Object

It returns the country name

Examples:

country_name('gr') # => Greece

Parameters:

  • code

    -> country iso code

Returns:

  • String


16
17
18
# File 'lib/flag_icon/helpers.rb', line 16

def country_name(code)
  FlagIcon::Countries::NAMES[code.to_sym]
end

.grouped_select_language(popular: 'Popular', available: 'Available') ⇒ Object

It returns an hash of arrays that can be used in the select tag

Examples:

grouped_select_language(popular: 'Popular', available: 'Available')

Parameters:

  • popular (defaults to: 'Popular')

    -> title of the popular group

  • available (defaults to: 'Available')

    -> title of the available group

Returns:

  • Hash


43
44
45
46
47
48
# File 'lib/flag_icon/helpers.rb', line 43

def grouped_select_language(popular: 'Popular', available: 'Available')
  {
    popular => popular_languages,
    available => available_languages.map { |lang| [language_name(lang), lang] }
  }
end

.language_flag(code) ⇒ Object

It returns the country iso code

Examples:

language_flag('el') # => gr

Parameters:

  • code

    -> language locale

Returns:

  • String


25
26
27
# File 'lib/flag_icon/helpers.rb', line 25

def language_flag(code)
  FlagIcon::Countries::LANGUAGE_FLAGS[code.to_sym] || 'xx'
end

.language_icon(code, title: "Audio language - #{language_name(code)}") ⇒ Object

It returns HTML element with country icon and title

Examples:

language_icon('el')

Parameters:

  • code

    -> language locale

Returns:

  • String


55
56
57
# File 'lib/flag_icon/helpers.rb', line 55

def language_icon(code, title: "Audio language - #{language_name(code)}")
  "<span class='flag-icon flag-icon-#{language_flag(code)}' title='#{title}'></span>"
end

.language_name(code) ⇒ Object

It returns the language name

Examples:

language_name('el') # => Greek

Parameters:

  • code

    -> language locale

Returns:

  • String


7
8
9
# File 'lib/flag_icon/helpers.rb', line 7

def language_name(code)
  FlagIcon::Countries::LANGUAGES[code.to_sym]
end

Define in your application_helper to override the popular languages Default: [['English', :en], ['Spanish', :es]]

Examples:

def popular_languages
  [['English', :en], ['Spanish', :es]]
end

Returns:

  • Array


76
77
78
# File 'lib/flag_icon/helpers.rb', line 76

def popular_languages
  [['English', :en], ['Spanish', :es]]
end

.select_languageObject

It returns an array that can be used in the select tag

Examples:

select_language

Returns:

  • Array


33
34
35
# File 'lib/flag_icon/helpers.rb', line 33

def select_language
  available_languages.map { |lang| [language_name(lang), lang] }
end