public
Description: rails plugin to store countries as ISO codes
Homepage:
Clone URL: git://github.com/koke/iso_countries.git
iso_countries / lib / iso_countries.rb
100644 59 lines (50 sloc) 1.581 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# IsoCountries
 
require "rubygems"
require "gettext"
stubs = %w(activesupport activerecord actionpack actionmailer activeresource)
stubs.each do |stub|
  require stub
end
 
require "country_list"
require "iso/countries/form_helpers"
require "iso/countries/country_field"
ActiveRecord::Base.send :include, ISO::Countries::CountryField
 
module ISO
  module Countries
    module ClassMethods
      GetText.bindtextdomain "iso_countries", :path => "#{File.dirname(__FILE__)}/../locale"
      GetText.locale = 'en'
      
      # Sets the language for country translation
      def set_language(lang)
        @@language = lang
        GetText.bindtextdomain "iso_countries", :path => "#{File.dirname(__FILE__)}/../locale"
        GetText.locale = lang
      end
      
      # Gets te current translation language
      def language
        @@language || "en"
      end
      
      # Wrapper to get country name from country code. +code+ can be a symbol or a string containing the country code.
      def get_country(code)
        _(COUNTRIES[code.to_sym]) rescue nil
      end
      
      # Wrapper to get country code from country name.
      def get_code(name)
        if COUNTRIES.value?(name)
          COUNTRIES.each_pair do |k,v|
            if v.eql?(name)
              return k.to_s
            end
          end
        end
      end
          
      # Returns an array with all the available country codes
      def country_codes
        COUNTRIES.keys.map { |key| key.to_s }
      end
    end
    
    class << self
      include ClassMethods
    end
  end
end