Giter Site home page Giter Site logo

mlitwiniuk / localized_country_select Goto Github PK

View Code? Open in Web Editor NEW

This project forked from karmi/localized_country_select

36.0 3.0 31.0 73 KB

Rails plugin for localized "country_select" helper with Rake task for downloading locales from Unicode.org's CLDR

Home Page: http://www.restafari.org/localized-country-select.html

License: MIT License

Ruby 100.00%

localized_country_select's Introduction

LocalizedCountrySelect

Rails plugin to provide support for localized <select> menu with country names and for storing country information as country code (eg. ‘es’), not name (eg. ‘Spain’), in the database.

Uses the Rails internationalization framework (I18n, rails-i18n.org) for translating the names of countries. Requires Rails 2.2 (released November 21st, 2008) or later versions. Country names are loaded from hashes in plugin directory, according to I18n.locale value.

You can easily translate country codes in your application like this:

<%= I18n.t @user.country, :scope => 'countries' %>

Comes with a Rake task rails import:country_select LOCALE=de for importing country names from Unicode.org’s CLDR repository (www.unicode.org/cldr/data/charts/summary/root.html) Don’t forget to restart the application when you add new locale.

ActionView helper code is adapted from Rails’ default country_select plugin (previously in core). See github.com/rails/country_select/tree/master/lib/country_select.rb

Rails 5 / Rails 6

In your Gemfile add:

gem 'localized_country_select', '>= 0.10.2'

Rails 3 / Rails 4

In your Gemfile add:

gem 'localized_country_select', '= 0.9.11'

Rails 2.3

No longer supported, but you can still use old version of gem. In environment.rb add

config.gem 'localized_country_select', :version => '0.0.1'

Example

Show full country names:

<%= localized_country_select(:user, :country, [], {include_blank: 'Please choose...'}) %>

will become:

<select name="user[country]" id="user_country">
<option value="">Please choose...</option>
<option disabled="disabled" value="">-------------</option>
<option value="AF">Afghanistan</option>
...
<option value="ZW">Zimbabwe</option>
</select>

Show only country codes:

<%= localized_country_select(:user, :country, [], {include_blank: 'Please choose...', description: :abbreviated}) %>

will become:

<select name="user[country]" id="user_country">
<option value="">Please choose...</option>
<option disabled="disabled" value="">-------------</option>
<option value="AF">AF</option>
...
<option value="ZW">ZW</option>
</select>

for the en locale.

You can exclude countries by code using the exclude option (a single code or an array of country codes):

localized_country_select(:user, :country, [], {exclude: [:ZZ, :US]})

Formtastic and SimpleForm

Gem supports (via alias_method) both formtastic and simple_form :country input

Other resources

Copyright © 2008 Karel Minarik (www.karmi.cz), released under the MIT license

localized_country_select's People

Contributors

bbenno avatar campeterson avatar dmitry-ilyashevich avatar donaldpiret avatar felixbuenemann avatar freegenie avatar jheuing avatar johnnyshields avatar karmi avatar michaelmwu avatar mlitwiniuk avatar scharfie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

localized_country_select's Issues

Missing and duplicated countries

I'm noticing that after I've imported the english names, few countries (like austria, australia, and possible others) are missing. And various (uk, us, and others) are duplicated. This problem happens also for other languages

Geocoder dependency?

Why does this appear in Railtie.rb? It forces me to add geocoder as a dependency, otherwise the my doesn't start...

require 'geocoder/models/active_record'

Does not work with Rails 5.2.0.rc1

This commit breaks the use of the method ActionView::Helpers::Tags::base#value.

I think calling #value with no arguments fixes it, but object needs to be passed for all previous versions of Rails

diff --git a/lib/localized_country_select.rb b/lib/localized_country_select.rb
index b2351ac..1ee748b 100644
--- a/lib/localized_country_select.rb
+++ b/lib/localized_country_select.rb
@@ -93,19 +93,18 @@ module ActionView
       end
       alias_method :country_options_for_select, :localized_country_options_for_select
 
     end
 
     module ToCountrySelectTag
       def to_localized_country_select_tag(priority_countries, options, html_options)
         html_options = html_options.stringify_keys
         add_default_name_and_id(html_options)
-        value = value(object)
         content_tag("select",
           add_options(
             localized_country_options_for_select(value, priority_countries, options).html_safe,
             options, value
           ), html_options
         )
       end
     end

broken url

The README's url referencing Unicode.org's CLDR repository is dead.

It actually seems that also the internal references are corrupted:

$ rails import:country_select LOCALE=de
... getting the HTML file for locale 'de'
[!] Invalid locale name 'es'! Not found in CLDR (404 Not Found)

I don't know what the original content of www.unicode.org/cldr/data/charts/summary/root.html was but I would suggest that it was moved to https://unicode-org.github.io/cldr-staging/charts/37/summary/root.html

How use this gem with simple form

Hi,

I am using simple form to display countries in "german","french","english","dutch" language. But i didn't find it to use propely in readme doc.

I have created file using this command successfully.

      rake import:country_select LOCALE=de

      rake import:country_select LOCALE=fr

     rake import:country_select LOCALE=en

How i can use it properly to display countries names to display in any above mentioned language?

   = f.input :country, :as => :country       

above line give me an error.

      "undefined method `map' for "translation missing: en.countries":String"

How i can display names properly with by passing options "de" ,"en","fr"?

like

       = f.input :country, :as => :country ,:language => :de      

Please help.

Thanks

undefined method

I have the following error: undefined method `map' for "translation missing: en.countries":String

Rails 4 support

I'm testing my app with your gem (0.9.2) and rails 4.0.0.beta1.

And I get this error

wrong number of arguments(4 for 0)

when I do : <%= ba.localized_country_select(:pays, ["CH", "LI"], include_blank: true) %>

Trace look like :

localized_country_select (0.9.2) lib/localized_country_select.rb:60:in initialize' localized_country_select (0.9.2) lib/localized_country_select.rb:60:innew'
localized_country_select (0.9.2) lib/localized_country_select.rb:60:in localized_country_select' localized_country_select (0.9.2) lib/localized_country_select.rb:107:inlocalized_country_select'
actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:38:in block in capture' actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:200:inwith_output_buffer'
actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:38:in capture' actionpack (4.0.0.beta1) lib/action_view/helpers/form_helper.rb:1827:inblock in fields_for_nested_model'

Let hpricot go

Hpricot is not maintained for over 2 years now. It's about time to let it go and replace it by some still maintained library.

Official gem release

I'm creating this as a separate issue for tracking purposes. We were discussing it previously after Issue #2 was closed.

Last comment from @asymmetric

I think that instead of renaming it, you might want to release it with your username as prefix, like
mlitwiniuk-localized_country_select. Most gems are doing it this way, and you make it easier for
people to find your gem when they look for localized_country_select.

Rake import doesn't fetch any country

$ rake import:country_select LOCALE=en
... getting the HTML file for locale 'en'
... parsing the HTML file

... writing the output

---
Written values for the 'en' into file: /home/user/project/config/locales/countries.en.rb

$ cat config/locales/countries.en.rb 
#encoding: UTF-8
{ :en => {

    :countries => {
    } 

  }
}

Problem with display of options and :include_blank

I'm using localized_country_select with activeadmin/formtastic and at first the countries wouldn't show up in the drop-down menu; they were just sitting there in the source code as one big string. I've overcome this by changing

class InstanceTag
      def to_localized_country_select_tag(priority_countries, options, html_options)
        html_options = html_options.stringify_keys
        add_default_name_and_id(html_options)
        value = value(object)
        content_tag("select",
          add_options(
            country_options_for_select(value, priority_countries,options),
            options, value
          ), html_options
        )
      end
    end

to

class InstanceTag
      def to_localized_country_select_tag(priority_countries, options, html_options)
        html_options = html_options.stringify_keys
        add_default_name_and_id(html_options)
        value = value(object)
        content_tag("select",
          add_options(
            country_options_for_select(value, priority_countries,options),
            options, value
          ).html_safe, html_options               # added .html_safe
        )
      end
    end

However, this only works as long as I don't use :include_blank.
That's sufficient for my purposes at the moment, but I thought you may want to fix it anyway (or tell me what I did wrong to get the problem in the first place).

Some country translations are wrong imported

In english there are too few and some captions are concatenated.
ie

{ :en => {

    :countries => {
            :GB => "U.K.UK",
            :GS => "South Georgia & South Sandwich IslandsSouth Georgia and the South Sandwich Islands",
            :HM => "Heard & McDonald IslandsHeard Island and McDonald Islands",
            :UM => "U.S. Outlying IslandsU.S. Minor Outlying Islands",
            :US => "U.S.US",
            :VC => "St. Vincent & GrenadinesSaint Vincent and the Grenadines",
    }

  }
}

License missing from gemspec

RubyGems.org doesn't report a license for your gem. This is because it is not specified in the gemspec of your last release.

via e.g.

  spec.license = 'MIT'
  # or
  spec.licenses = ['MIT', 'GPL-2']

Including a license in your gemspec is an easy way for rubygems.org and other tools to check how your gem is licensed. As you can imagine, scanning your repository for a LICENSE file or parsing the README, and then attempting to identify the license or licenses is much more difficult and more error prone. So, even for projects that already specify a license, including a license in your gemspec is a good practice. See, for example, how rubygems.org uses the gemspec to display the rails gem license.

There is even a License Finder gem to help companies/individuals ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec. This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue with a nice message. In either case, I'll follow up. Thanks for your time!

Appendix:

If you need help choosing a license (sorry, I haven't checked your readme or looked for a license file), GitHub has created a license picker tool. Code without a license specified defaults to 'All rights reserved'-- denying others all rights to use of the code.
Here's a list of the license names I've found and their frequencies

p.s. In case you're wondering how I found you and why I made this issue, it's because I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too, and make issues for gemspecs not specifying a license as a public service :). See the previous link or my blog post about this project for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.