FlagIcon

Gem Version

Purpose

An attempt to map languages to country flags!

Approach

Matching flags to languages can be really tricky and even problematic sometimes because there is no direct association between languages and countries since the same language is spoken in many countries and many languages are spoken in a single country.

So for a title from Mexico which has spanish as the official language the spanish flag will appear.

If you notice any flags being mismatched please raise an issue or even better open a pull request

Installation

Add this line to your application's Gemfile:

gem 'flag_icon'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install flag_icon

Usage

There is a number of helpers available that will render country names, language names and flags given a country code or locale.

# It returns HTML element with country icon and title
#
# @param code -> language locale
# @example language_icon('el')
# @return String
language_icon
# It returns HTML element with country icon and title
#
# @param code -> country iso code
# @example country_icon('gr')
# @return String
country_icon
# It returns the language name
#
# @param code -> language locale
# @example language_name('el') # => Greek
# @return String
language_name
# It returns the country name
#
# @param code -> country iso code
# @example country_name('gr') # => Greece
# @return String
country_name
# It returns the country iso code
#
# @param code -> language locale
# @example language_flag('el') # => gr
# @return String
language_flag
# It returns an array that can be used in the select tag
#
# @example select_language
# @return Array
select_language
  # It returns an hash of arrays that can be used in the select tag
#
# @param popular -> title of the popular group
# @param available -> title of the available group
# @example grouped_select_language(popular: 'Popular', available: 'Available')
# @return Hash
grouped_select_language
# Define in your application_helper to override the popular languages
# Default: [['English', :en], ['Spanish', :es]]
#
# @example
#   def popular_languages
#     [['English', :en], ['Spanish', :es]]
#   end
# @return Array
popular_languages
# Define in your application_helper to override the available languages
# @example [['English', :en], ['Spanish', :es]] #default
# @example
#   def available_languages
#     Language.all.order(:language_locale).pluck(:language_locale)
#   end
# @return Array
available_languages

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the FlagIcon project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits