Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues generating a select languages list #12

Open
dankohn opened this issue Aug 12, 2015 · 0 comments
Open

Issues generating a select languages list #12

dankohn opened this issue Aug 12, 2015 · 0 comments

Comments

@dankohn
Copy link

dankohn commented Aug 12, 2015

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant