i18n_language_select 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/i18n_language_select.gemspec +25 -0
  8. data/lib/i18n_language_select/form_builder.rb +7 -0
  9. data/lib/i18n_language_select/form_helpers.rb +13 -0
  10. data/lib/i18n_language_select/import.rb +107 -0
  11. data/lib/i18n_language_select/instance_tag.rb +46 -0
  12. data/lib/i18n_language_select/railtie.rb +35 -0
  13. data/lib/i18n_language_select/version.rb +3 -0
  14. data/lib/i18n_language_select.rb +47 -0
  15. data/lib/locales/i18n_languages.af.yml +87 -0
  16. data/lib/locales/i18n_languages.ar.yml +90 -0
  17. data/lib/locales/i18n_languages.az.yml +90 -0
  18. data/lib/locales/i18n_languages.be.yml +78 -0
  19. data/lib/locales/i18n_languages.bg.yml +90 -0
  20. data/lib/locales/i18n_languages.bn.yml +90 -0
  21. data/lib/locales/i18n_languages.bs.yml +89 -0
  22. data/lib/locales/i18n_languages.ca.yml +90 -0
  23. data/lib/locales/i18n_languages.cs.yml +90 -0
  24. data/lib/locales/i18n_languages.cy.yml +89 -0
  25. data/lib/locales/i18n_languages.da.yml +90 -0
  26. data/lib/locales/i18n_languages.de.yml +90 -0
  27. data/lib/locales/i18n_languages.el.yml +90 -0
  28. data/lib/locales/i18n_languages.en.yml +93 -0
  29. data/lib/locales/i18n_languages.eo.yml +86 -0
  30. data/lib/locales/i18n_languages.es.yml +90 -0
  31. data/lib/locales/i18n_languages.et.yml +90 -0
  32. data/lib/locales/i18n_languages.eu.yml +88 -0
  33. data/lib/locales/i18n_languages.fa.yml +90 -0
  34. data/lib/locales/i18n_languages.fi.yml +90 -0
  35. data/lib/locales/i18n_languages.fr.yml +90 -0
  36. data/lib/locales/i18n_languages.ga.yml +90 -0
  37. data/lib/locales/i18n_languages.gl.yml +88 -0
  38. data/lib/locales/i18n_languages.gu.yml +90 -0
  39. data/lib/locales/i18n_languages.ha.yml +80 -0
  40. data/lib/locales/i18n_languages.hi.yml +90 -0
  41. data/lib/locales/i18n_languages.hr.yml +90 -0
  42. data/lib/locales/i18n_languages.hu.yml +90 -0
  43. data/lib/locales/i18n_languages.hy.yml +86 -0
  44. data/lib/locales/i18n_languages.id.yml +90 -0
  45. data/lib/locales/i18n_languages.ig.yml +43 -0
  46. data/lib/locales/i18n_languages.is.yml +90 -0
  47. data/lib/locales/i18n_languages.it.yml +90 -0
  48. data/lib/locales/i18n_languages.ja.yml +90 -0
  49. data/lib/locales/i18n_languages.ka.yml +87 -0
  50. data/lib/locales/i18n_languages.kk.yml +87 -0
  51. data/lib/locales/i18n_languages.km.yml +86 -0
  52. data/lib/locales/i18n_languages.kn.yml +90 -0
  53. data/lib/locales/i18n_languages.ko.yml +90 -0
  54. data/lib/locales/i18n_languages.lo.yml +90 -0
  55. data/lib/locales/i18n_languages.lt.yml +90 -0
  56. data/lib/locales/i18n_languages.lv.yml +90 -0
  57. data/lib/locales/i18n_languages.mg.yml +44 -0
  58. data/lib/locales/i18n_languages.mk.yml +90 -0
  59. data/lib/locales/i18n_languages.ml.yml +90 -0
  60. data/lib/locales/i18n_languages.mn.yml +86 -0
  61. data/lib/locales/i18n_languages.mr.yml +90 -0
  62. data/lib/locales/i18n_languages.ms.yml +87 -0
  63. data/lib/locales/i18n_languages.mt.yml +90 -0
  64. data/lib/locales/i18n_languages.my.yml +84 -0
  65. data/lib/locales/i18n_languages.ne.yml +85 -0
  66. data/lib/locales/i18n_languages.nl.yml +90 -0
  67. data/lib/locales/i18n_languages.pa.yml +84 -0
  68. data/lib/locales/i18n_languages.pl.yml +90 -0
  69. data/lib/locales/i18n_languages.pt.yml +90 -0
  70. data/lib/locales/i18n_languages.ro.yml +90 -0
  71. data/lib/locales/i18n_languages.ru.yml +90 -0
  72. data/lib/locales/i18n_languages.si.yml +84 -0
  73. data/lib/locales/i18n_languages.sk.yml +90 -0
  74. data/lib/locales/i18n_languages.sl.yml +90 -0
  75. data/lib/locales/i18n_languages.so.yml +81 -0
  76. data/lib/locales/i18n_languages.sq.yml +86 -0
  77. data/lib/locales/i18n_languages.sr.yml +90 -0
  78. data/lib/locales/i18n_languages.sv.yml +90 -0
  79. data/lib/locales/i18n_languages.sw.yml +87 -0
  80. data/lib/locales/i18n_languages.ta.yml +90 -0
  81. data/lib/locales/i18n_languages.te.yml +90 -0
  82. data/lib/locales/i18n_languages.th.yml +90 -0
  83. data/lib/locales/i18n_languages.tr.yml +90 -0
  84. data/lib/locales/i18n_languages.uk.yml +90 -0
  85. data/lib/locales/i18n_languages.ur.yml +88 -0
  86. data/lib/locales/i18n_languages.uz.yml +84 -0
  87. data/lib/locales/i18n_languages.vi.yml +90 -0
  88. data/lib/locales/i18n_languages.yi.yml +74 -0
  89. data/lib/locales/i18n_languages.yo.yml +78 -0
  90. data/lib/locales/i18n_languages.zh.yml +90 -0
  91. data/lib/locales/i18n_languages.zu.yml +87 -0
  92. data/lib/tasks/import_languages.rake +25 -0
  93. data/prune_languages.rb +49 -0
  94. metadata +193 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df6121d4d84dbe1fcc23a1c5bdb93b9b198d0293
4
+ data.tar.gz: b170555f0fc346668bfec51437e62c9862daf726
5
+ SHA512:
6
+ metadata.gz: 3a3558c23d4ca5a7bbb88775fe90121be5e04d19b1d729a1e1c33bb75d20b8833f802cc4184a1f0fc6dc31a6976f29870739e8f737798f22e79474c396d011b5
7
+ data.tar.gz: 45624fd65356eae7b3c03ca48c5dfb26df0457937dfae6f9ffc7ea366deeae8ebbd22d92ddb98b02eaea43a03c22de12c71a0e6e325fbaea85d12ef546d4ac08
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i18n_language_select.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Adam St. John
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # I18nLanguageSelect
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'i18n_language_select'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install i18n_language_select
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/i18n_language_select/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'i18n_language_select/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i18n_language_select"
8
+ spec.version = I18nLanguageSelect::VERSION
9
+ spec.authors = ["Adam St. John"]
10
+ spec.email = ["astjohn@gmail.com"]
11
+ spec.homepage = "https://github.com/Sitata/i18n_language_select"
12
+ spec.summary = "I18n language select helper"
13
+ spec.description = "A simple language code select helper that works with I18n translations."
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "threach"
25
+ end
@@ -0,0 +1,7 @@
1
+ module I18nLanguageSelect
2
+ module FormBuilder
3
+ def language_code_select(method, priority_languages = nil, options = {}, html_options = {})
4
+ @template.language_code_select(@object_name, method, priority_languages, options.merge(object: @object), html_options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module I18nLanguageSelect
2
+ module FormHelpers
3
+ def language_code_select(object_name, method, priority_languages = nil, options = {}, html_options = {})
4
+ if Rails::VERSION::MAJOR >= 4
5
+ instance_tag = ActionView::Helpers::Tags::Select.new(object_name, method, self, [], options, html_options)
6
+ instance_tag.to_language_code_select_tag(priority_languages, html_options)
7
+ else
8
+ instance_tag = ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object))
9
+ instance_tag.to_language_code_select_tag(priority_languages, html_options, options)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,107 @@
1
+ require 'yaml'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'threach'
5
+
6
+ # Imports ISO 639-1 country codes
7
+ # It parses a HTML file from Unicode.org for given locale and saves the
8
+ # Rails' I18n hash in the plugin +locale+ directory
9
+ module I18nLanguageSelect
10
+ class Import
11
+ attr_accessor :output_dir
12
+ attr_reader :locales
13
+
14
+ def initialize(locales = nil, namespace=nil)
15
+ @namespace = namespace || I18nLanguageSelect::NAMESPACE
16
+ @locales = if locales
17
+ locales.split(",")
18
+ else
19
+ fetch_locales
20
+ end
21
+ if @locales && @locales.length
22
+ puts "Gathering languages from unicode.org using the namespace '#{@namespace}' and the locales: #{@locales.join(', ')}"
23
+ else
24
+ puts "Unable to gather languages. No locales specified. Either specifiy locales using the environment variable LOCALES, or install the i18n gem."
25
+ end
26
+ end
27
+
28
+ def process
29
+ @locales.threach(4, :each_with_index) do |locale, index|
30
+ puts "Processing: #{locale}"
31
+ # ----- Get the CLDR HTML --------------------------------------------------
32
+ begin
33
+ doc = Nokogiri::HTML(open("http://www.unicode.org/cldr/charts/latest/summary/#{locale}.html"))
34
+ rescue => e
35
+ puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
36
+ return if index == @locales.length - 1
37
+ end
38
+
39
+ languages = search_for_languages(doc)
40
+ if languages.keys.length
41
+ # ----- Parse the HTML with Nokogiri ----------------------------------------
42
+ output = { locale => { @namespace => languages } }
43
+ # ----- Write the parsed values into file ---------------------------------
44
+ write_for(locale, output)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def search_for_languages(doc)
52
+ result = {}
53
+ doc.search("//tr").each do |row|
54
+ if row_is_language?(row)
55
+ code_cell = row.search("td[@class='g']").last
56
+ value_cell = row.search("td[@class='v']").first
57
+
58
+ if value_cell
59
+ r = /([A-Za-z\-_0-9]+)/ # to sanitize the language code
60
+ match = r.match(code_cell.inner_text)
61
+ code = match[0] if match
62
+ name = value_cell.inner_text
63
+ # if code and name are the same, then translation doesn't exist and best to
64
+ # not include anything so developer can choose own fallback.
65
+ result.update(code.to_s => name.to_s) unless code.to_s == name.to_s
66
+ end
67
+ end
68
+ end
69
+ # result.sort_by { |key, value| key }.to_h works in Ruby 2.x
70
+ Hash[result.sort_by { |key, value| key }]
71
+ end
72
+
73
+ def row_is_language?(row)
74
+ primary = row.search("td[@class='n']")
75
+ secondary = row.search("td[@class='g']")
76
+
77
+ has_locale_cell = primary.first && primary.first.inner_text =~ /^Locale Display Names$/
78
+ primary_has_language_cell = primary[1] && primary[1].inner_text =~ /^Languages/
79
+ secondary_has_language_cell = secondary.first && secondary.first.inner_text =~ /^Languages/
80
+
81
+ has_locale_cell && (primary_has_language_cell || secondary_has_language_cell)
82
+ end
83
+
84
+ def fetch_locales
85
+ puts "Fetching available locales from unicode.org."
86
+ begin
87
+ doc = Nokogiri::HTML(open("http://www.unicode.org/cldr/charts/latest/summary/root.html"))
88
+ doc.css("p a").map{|link| link['href']}.map do |url|
89
+ m = /([A-za-z]+).html/.match(url)
90
+ m[1] if m && m[1] != "root"
91
+ end.compact.uniq
92
+ rescue => e
93
+ puts "[!] Unable to fetch unitcode root for locales (#{e})."
94
+ []
95
+ end
96
+ end
97
+
98
+
99
+ def write_for(locale, output)
100
+ puts "\n... writing the output"
101
+ filename = File.join(Dir.pwd, "#{@namespace}.#{locale}.yml")
102
+ File.rename(filename, filename + ".OLD") if File.exists?(filename) # Rename by appending 'OLD' if file exists
103
+ File.open(filename, "w+") { |f| f << output.to_yaml }
104
+ puts "\n---\nWritten values for the '#{locale}' into file: #{filename}\n"
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,46 @@
1
+ module I18nLanguageSelect
2
+ module InstanceTag
3
+
4
+ def to_language_code_select_tag(priority_languages, html_options = {}, options = {})
5
+ # Rails 4 stores options sent when creating an InstanceTag.
6
+ # Let's use them!
7
+ options = @options if defined?(@options)
8
+
9
+ language_code_select(priority_languages, options, html_options)
10
+ end
11
+
12
+ # Adapted from Rails language_select. Just uses language codes instead of full names.
13
+ def language_code_select(priority_languages, options, html_options)
14
+ selected = object.send(@method_name) if object.respond_to?(@method_name)
15
+
16
+ languages = ""
17
+
18
+ if options.present? and options[:include_blank]
19
+ option = options[:include_blank] == true ? "" : options[:include_blank]
20
+ languages += "<option>#{option}</option>\n"
21
+ end
22
+
23
+ if priority_languages
24
+ languages += options_for_select(priority_languages, selected)
25
+ languages += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
26
+ end
27
+
28
+ languages = languages + options_for_select(language_translations, selected)
29
+
30
+ html_options = html_options.stringify_keys
31
+ add_default_name_and_id(html_options)
32
+
33
+ content_tag(:select, languages.html_safe, html_options)
34
+ end
35
+
36
+ def language_translations
37
+ I18n.t(I18nLanguageSelect.configuration.namespace.to_s).map do |pairing|
38
+ [pairing[1], pairing[0]] if pairing[0] && pairing[1]
39
+ end.compact.sort_by do |pairing|
40
+ pairing[0] # sort by language name alphabetical
41
+ end
42
+ end
43
+
44
+
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ module I18nLanguageSelect
2
+ class Railtie < Rails::Railtie
3
+ initializer 'i18n_language_select' do |app|
4
+ I18nLanguageSelect::Railtie.instance_eval do
5
+
6
+ unless I18nLanguageSelect.configuration.custom
7
+ which_locals = if I18nLanguageSelect.configuration.available_locales_only
8
+ app.config.i18n.available_locales
9
+ else
10
+ nil
11
+ end
12
+ pattern = pattern_from(which_locals)
13
+ add("locales/i18n_languages.#{pattern}.yml")
14
+ end
15
+ end
16
+ end
17
+
18
+ rake_tasks do
19
+ load "tasks/import_languages.rake"
20
+ end
21
+
22
+ protected
23
+
24
+ def self.add(pattern)
25
+ files = Dir[File.join(File.dirname(__FILE__), '../', pattern)]
26
+ I18n.load_path.concat(files)
27
+ end
28
+
29
+ def self.pattern_from(args)
30
+ array = Array(args || [])
31
+ array.blank? ? '*' : "{#{array.join ','}}"
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module I18nLanguageSelect
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,47 @@
1
+ require "i18n_language_select/version"
2
+ require "i18n_language_select/form_helpers"
3
+ require "i18n_language_select/instance_tag"
4
+ require "i18n_language_select/form_builder"
5
+ require "i18n_language_select/import"
6
+ require 'i18n_language_select/railtie' if defined?(Rails)
7
+
8
+ module I18nLanguageSelect
9
+
10
+ class << self
11
+
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def configuration=(config)
17
+ @configuration = config
18
+ end
19
+
20
+ def configure
21
+ yield(configuration)
22
+ end
23
+
24
+ end
25
+
26
+ class Configuration
27
+ attr_accessor :namespace, # namespace of locales for language options
28
+ :custom, # if true, do not load locales from gem
29
+ :available_locales_only # if true and custom is false, only load app's locales from gem
30
+
31
+ def initialize
32
+ self.namespace = "i18n_languages"
33
+ self.custom = false
34
+ self.available_locales_only = true
35
+ end
36
+ end
37
+
38
+
39
+ end
40
+
41
+ ActionView::Base.send(:include, I18nLanguageSelect::FormHelpers)
42
+ if Rails::VERSION::MAJOR >= 4
43
+ ActionView::Helpers::ActiveModelInstanceTag.send(:include, I18nLanguageSelect::InstanceTag)
44
+ else
45
+ ActionView::Helpers::InstanceTag.send(:include, I18nLanguageSelect::InstanceTag)
46
+ end
47
+ ActionView::Helpers::FormBuilder.send(:include, I18nLanguageSelect::FormBuilder)
@@ -0,0 +1,87 @@
1
+ ---
2
+ af:
3
+ i18n_languages:
4
+ af: Afrikaans
5
+ ar: Arabies
6
+ az: Azerbeidjans
7
+ be: Wit-Russies
8
+ bg: Bulgaars
9
+ bn: Bengaals
10
+ bs: Bosnies
11
+ ca: Katalaans
12
+ cs: Tsjeggies
13
+ cy: Wallies
14
+ da: Deens
15
+ de: Duits
16
+ el: Grieks
17
+ en: Engels
18
+ eo: Esperanto
19
+ es: Spaans
20
+ et: Estnies
21
+ eu: Baskies
22
+ fa: Persies
23
+ fi: Fins
24
+ fr: Frans
25
+ ga: Iers
26
+ gl: Galisies
27
+ gu: Goedjarati
28
+ ha: Hausa
29
+ hi: Hindi
30
+ hr: Kroaties
31
+ ht: Haïtiaans
32
+ hu: Hongaars
33
+ hy: Armeens
34
+ id: Indonesies
35
+ ig: Igbo
36
+ is: Yslands
37
+ it: Italiaans
38
+ ja: Japannees
39
+ ka: Georgies
40
+ kk: Kazaks
41
+ km: Khmer
42
+ kn: Kannada
43
+ ko: Koreaans
44
+ la: Latyn
45
+ lo: Lao
46
+ lt: Litaus
47
+ lv: Letties
48
+ mg: Malgassies
49
+ mi: Maori
50
+ mk: Masedonies
51
+ ml: Malabaars
52
+ mn: Mongools
53
+ mr: Marathi
54
+ ms: Maleis
55
+ mt: Maltees
56
+ my: Birmaans
57
+ ne: Nepalees
58
+ nl: Nederlands
59
+ ny: Nyanja
60
+ pa: Pandjabi
61
+ pl: Pools
62
+ pt: Portugees
63
+ ro: Roemeens
64
+ ru: Russies
65
+ si: Sinhala
66
+ sk: Slowaaks
67
+ sl: Sloweens
68
+ so: Somalies
69
+ sq: Albanees
70
+ sr: Serwies
71
+ st: Suid-Sotho
72
+ su: Sundanees
73
+ sv: Sweeds
74
+ sw: Swahili
75
+ ta: Tamil
76
+ te: Telugu
77
+ tg: Tadzjieks
78
+ th: Thai
79
+ tr: Turks
80
+ uk: Oekraïens
81
+ ur: Oerdoe
82
+ uz: Oezbeeks
83
+ vi: Viëtnamees
84
+ yi: Jiddisj
85
+ yo: Yoruba
86
+ zh: Sjinees
87
+ zu: Zoeloe