Skip to content

Issues generating a select languages list #12

Description

@dankohn

Thanks very much for assembling the list of languages.

We needed a select tag of common languages, but with English and Spanish shown first. We also wanted to list both Mandarin and Cantonese, and remove the parentheticals. We used iso_639_3 to get a code for Cantonese. Others may find this useful:

# app/application_helper.rb

  def our_languages
    # Fix names, add Cantonese, and bring English and Spanish to the front
    LanguageList::COMMON_LANGUAGES.map do |language|
      [language.name.gsub('Modern Greek (1453-)', 'Greek')
        .gsub('Tonga (Tonga Islands)', 'Tongan')
        .gsub('Chinese', 'Chinese Mandarin'),
       language.iso_639_3]
    end.push(['Chinese Cantonese', 'yue']).sort.promote(%w(Spanish spa))
      .promote(%w(English eng))
  end

  class ActionView::Helpers::FormBuilder
    # http://stackoverflow.com/a/2625727/1935918
    include ApplicationHelper
    include ActionView::Helpers::FormTagHelper
    include ActionView::Helpers::FormOptionsHelper

    def language_select(name, options = { include_blank: true })
      select_tag "#{@object_name}[#{name}]",
                 options_for_select(our_languages,
                                    selected: @object.send(name)), options
    end

  class Array
    def promote(promoted_element)
      return self unless (found_index = find_index(promoted_element))
      unshift(delete_at(found_index))
    end
  end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions