Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
karmi committed Oct 22, 2008
0 parents commit e883f2a
Show file tree
Hide file tree
Showing 12 changed files with 805 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
*/.DS_Store
rdoc
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 changes: 42 additions & 0 deletions README.rdoc
@@ -0,0 +1,42 @@
= LocalizedCountrySelect

Rails plugin to provide support for localized <tt><select></tt> 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, http://rails-i18n.org) for translating the names of countries.
Country names are loaded from hashes in plugin directory, according to <tt>I18n.locale</tt> value.

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

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

Comes with a Rake task <tt>rake import:country_select 'de'</tt> for importing country names
from Unicode.org's CLDR repository (http://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 http://github.com/rails/country_select/tree/master/lib/country_select.rb

== Example

<%= localized_country_code_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>

for the <tt>en-US</tt> locale.

== Other resources

* http://github.com/rails/country_select (Default Rails plugin)
* http://github.com/russ/country_code_select (Stores country code, not name)


Copyright (c) 2008 Karel Minarik (www.karmi.cz), released under the MIT license
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the localized_country_select plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the localized_country_select plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'LocalizedCountrySelect'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end
5 changes: 5 additions & 0 deletions init.rb
@@ -0,0 +1,5 @@
# Require the plugin code
require 'localized_country_select'

# Load locales for countries from +locale+ directory into Rails
I18n.load_path += Dir[ File.join(File.dirname(__FILE__), 'locale', '*.{rb,yml}') ]
4 changes: 4 additions & 0 deletions install.rb
@@ -0,0 +1,4 @@
# Show the README text file
puts "\n\n"
puts IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
puts "\n"
81 changes: 81 additions & 0 deletions lib/localized_country_select.rb
@@ -0,0 +1,81 @@
# = LocalizedCountrySelect
#
# View helper for displaying select list with countries:
#
# localized_country_code_select(:user, :country)
#
# Works just like the default Rails' +country_select+ plugin, but stores countries as
# country *codes*, not *names*, in the database.
#
# You can easily translate country codes in your application like this:
# <%= I18n.t @user.country, :scope => 'countries' %>
#
# Uses the Rails internationalization framework (I18n) for translating the names of countries.
#
# Use Rake task <tt>rake import:country_select 'de'</tt> for importing country names
# from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
#
# Code adapted from Rails' default +country_select+ plugin (previously in core)
# See http://github.com/rails/country_select/tree/master/lib/country_select.rb
#
# TODO : Test coverage
# TODO : Solve priority countries

module LocalizedCountrySelect
class << self
# Returns array with codes and localized country names (according to <tt>I18n.locale</tt>)
# for <tt><option></tt> tags
def localized_countries_array
I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] }.sort
end
end
end

module ActionView
module Helpers

module FormOptionsHelper

# Return select and option tags for the given object and method, using +localized_country_options_for_select+
# to generate the list of option tags. Uses <b>country code</b>, not name as option +value+.
def localized_country_select(object, method, priority_countries = nil, options = {}, html_options = {})
InstanceTag.new(object, method, self, options.delete(:object)).
to_localized_country_select_tag(priority_countries, options, html_options)
end

# Returns a string of option tags for countries according to locale. Supply the country code in upper-case ('US', 'DE')
# as +selected+ to have it marked as the selected option tag.
# TODO : You can also supply an array of countries as +priority_countries+, so that they will be listed first.
def localized_country_options_for_select(selected = nil, priority_countries = nil)
country_options = ""
if priority_countries
country_options += options_for_select(priority_countries, selected)
country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
end
return country_options + options_for_select(LocalizedCountrySelect::localized_countries_array, selected)
end

end

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(
localized_country_options_for_select(value, priority_countries),
options, value
), html_options
)
end
end

class FormBuilder
def localized_country_select(method, priority_countries = nil, options = {}, html_options = {})
@template.localized_country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
end
end

end
end

0 comments on commit e883f2a

Please sign in to comment.