twitter_cldr_js 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/Gemfile +12 -0
  2. data/History.txt +3 -0
  3. data/LICENSE +177 -0
  4. data/NOTICE +120 -0
  5. data/README.md +161 -0
  6. data/Rakefile +66 -0
  7. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_af.js +887 -0
  8. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_ar.js +887 -0
  9. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_ca.js +887 -0
  10. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_cs.js +887 -0
  11. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_da.js +887 -0
  12. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_de.js +887 -0
  13. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_el.js +887 -0
  14. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_en.js +887 -0
  15. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_es.js +887 -0
  16. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_eu.js +887 -0
  17. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_fa.js +887 -0
  18. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_fi.js +887 -0
  19. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_fil.js +887 -0
  20. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_fr.js +887 -0
  21. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_he.js +887 -0
  22. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_hi.js +887 -0
  23. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_hu.js +887 -0
  24. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_id.js +887 -0
  25. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_it.js +887 -0
  26. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_ja.js +887 -0
  27. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_ko.js +887 -0
  28. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_msa.js +887 -0
  29. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_nl.js +887 -0
  30. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_no.js +887 -0
  31. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_pl.js +887 -0
  32. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_pt.js +887 -0
  33. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_ru.js +887 -0
  34. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_sv.js +887 -0
  35. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_th.js +887 -0
  36. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_tr.js +887 -0
  37. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_uk.js +887 -0
  38. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_ur.js +887 -0
  39. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_zh-cn.js +887 -0
  40. data/lib/assets/javascripts/twitter_cldr/twitter_cldr_zh-tw.js +887 -0
  41. data/lib/twitter_cldr/js/compiler.rb +49 -0
  42. data/lib/twitter_cldr/js/mustache/bundle.coffee +15 -0
  43. data/lib/twitter_cldr/js/mustache/calendars/datetime.coffee +240 -0
  44. data/lib/twitter_cldr/js/mustache/calendars/timespan.coffee +52 -0
  45. data/lib/twitter_cldr/js/mustache/numbers/numbers.coffee +179 -0
  46. data/lib/twitter_cldr/js/mustache/plurals/rules.coffee +14 -0
  47. data/lib/twitter_cldr/js/mustache/shared/currencies.coffee +27 -0
  48. data/lib/twitter_cldr/js/renderers/base.rb +18 -0
  49. data/lib/twitter_cldr/js/renderers/bundle.rb +18 -0
  50. data/lib/twitter_cldr/js/renderers/calendars/datetime_renderer.rb +34 -0
  51. data/lib/twitter_cldr/js/renderers/calendars/timespan_renderer.rb +39 -0
  52. data/lib/twitter_cldr/js/renderers/numbers/numbers_renderer.rb +31 -0
  53. data/lib/twitter_cldr/js/renderers/plurals/rules/plural_rules_compiler.rb +93 -0
  54. data/lib/twitter_cldr/js/renderers/plurals/rules/plural_rules_renderer.rb +26 -0
  55. data/lib/twitter_cldr/js/renderers/shared/currencies_renderer.rb +23 -0
  56. data/lib/twitter_cldr/js/renderers.rb +31 -0
  57. data/lib/twitter_cldr/js/tasks/tasks.rake +14 -0
  58. data/lib/twitter_cldr/js/tasks/tasks.rb +88 -0
  59. data/lib/twitter_cldr/js/version.rb +10 -0
  60. data/lib/twitter_cldr/js.rb +31 -0
  61. data/spec/js/calendars/datetime.spec.js +418 -0
  62. data/spec/js/calendars/timespan.spec.js +91 -0
  63. data/spec/js/numbers/currency.spec.js +34 -0
  64. data/spec/js/numbers/decimal.spec.js +24 -0
  65. data/spec/js/numbers/helpers/fraction.spec.js +23 -0
  66. data/spec/js/numbers/helpers/integer.spec.js +100 -0
  67. data/spec/js/numbers/number.spec.js +70 -0
  68. data/spec/js/numbers/percent.spec.js +22 -0
  69. data/spec/js/plurals/plural_rules.spec.js +28 -0
  70. data/spec/ruby/renderers/plurals/plural_rules_compiler_spec.rb +56 -0
  71. data/spec/ruby/spec_helper.rb +11 -0
  72. data/twitter_cldr_js.gemspec +34 -0
  73. metadata +224 -0
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Js
8
+ module Renderers
9
+ module Numbers
10
+ class NumbersRenderer < TwitterCldr::Js::Renderers::Base
11
+ self.template_file = File.expand_path(File.join(File.dirname(__FILE__), "../..", "mustache/numbers/numbers.coffee"))
12
+
13
+ def tokens
14
+ TwitterCldr::Tokenizers::NumberTokenizer::VALID_TYPES.inject({}) do |ret, type|
15
+ tokenizer = TwitterCldr::Tokenizers::NumberTokenizer.new(:type => type, :locale => @locale)
16
+ ret[type] = {}
17
+ [:positive, :negative].each do |sign|
18
+ ret[type][sign] = tokenizer.tokens(:sign => sign)
19
+ end
20
+ ret
21
+ end.to_json
22
+ end
23
+
24
+ def symbols
25
+ TwitterCldr::Tokenizers::NumberTokenizer.new(:locale => @locale).symbols.to_json
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ require 'ruby_parser'
7
+
8
+ module TwitterCldr
9
+ module Js
10
+ module Renderers
11
+ module PluralRules
12
+
13
+ class PluralRulesCompiler
14
+
15
+ class << self
16
+ def rule_to_js(rule_code)
17
+ tree = RubyParser.new.parse(rule_code).to_a
18
+ "function(n) { return #{statement_list(tree)} }"
19
+ end
20
+
21
+ protected
22
+
23
+ def statement_list(tree)
24
+ if tree.first == :block
25
+ tree[1..-1].map { |sub_tree| send(:"_#{sub_tree.first}", sub_tree) }.join(" ")
26
+ else
27
+ send(:"_#{tree.first}", tree)
28
+ end
29
+ end
30
+
31
+ def _if(tree)
32
+ result = "(function() { if (#{statement_list(tree[1])}) { return #{statement_list(tree[2])} }"
33
+ result << " else { return #{statement_list(tree[3])} }" if tree[3]
34
+ "#{result} })();"
35
+ end
36
+
37
+ def _lvar(tree)
38
+ tree.first.to_s
39
+ end
40
+
41
+ def _call(tree)
42
+ case tree[2]
43
+ when :==, :%, :<, :> # special operators that actually resolve to method calls
44
+ "#{statement_list(tree[1])} #{tree[2]} #{statement_list(tree[3])}"
45
+ when :include?
46
+ "#{statement_list(tree[1])}.indexOf(#{statement_list(tree[3])}) >= 0"
47
+ when :between?
48
+ smt = "((#{statement_list(tree[1])} >= #{statement_list(tree[3][1])})"
49
+ smt << " && (#{statement_list(tree[1])} <= #{statement_list(tree[3][2])}))"
50
+ smt
51
+ else
52
+ # this should be the only case where tree[1] might be nil (i.e. the method was not called on any object)
53
+ call = tree[1] ? "#{statement_list(tree[1])}.#{tree[2]}" : tree[2].to_s
54
+ arglist = statement_list(tree[3])
55
+ arglist == "" ? call : "#{call}(#{arglist})"
56
+ end
57
+ end
58
+
59
+ def _arglist(tree)
60
+ tree[1..-1].map { |arg| statement_list(arg) }.join(", ")
61
+ end
62
+
63
+ def _lit(tree)
64
+ if tree[1].is_a?(Symbol) || tree[1].is_a?(String)
65
+ %Q("#{tree[1]}")
66
+ else
67
+ tree[1]
68
+ end
69
+ end
70
+
71
+ def _array(tree)
72
+ "[#{tree[1..-1].map { |arg| statement_list(arg) }.join(", ")}]"
73
+ end
74
+
75
+ def _not(tree)
76
+ "!(#{statement_list(tree[1])})"
77
+ end
78
+
79
+ def _and(tree)
80
+ "#{statement_list(tree[1])} && #{statement_list(tree[2])}"
81
+ end
82
+
83
+ def _or(tree)
84
+ "#{statement_list(tree[1])} || #{statement_list(tree[2])}"
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Js
8
+ module Renderers
9
+ module PluralRules
10
+
11
+ class PluralRulesRenderer < TwitterCldr::Js::Renderers::Base
12
+ self.template_file = File.expand_path(File.join(File.dirname(__FILE__), "../../..", "mustache/plurals/rules.coffee"))
13
+
14
+ def rules
15
+ ruby_str = TwitterCldr.get_locale_resource(@locale, :plurals)[@locale]
16
+ rule_str = ruby_str.scan(/lambda\s*\{\s*\|n\|(.*?)\}/).first.first.strip
17
+ js_str = PluralRulesCompiler.rule_to_js(rule_str)
18
+ hash = eval(ruby_str)
19
+ %Q({"keys": #{hash[@locale][:i18n][:plural][:keys].to_json}, "rule": #{js_str}})
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Js
8
+ module Renderers
9
+ module Shared
10
+ class CurrenciesRenderer < TwitterCldr::Js::Renderers::Base
11
+ self.template_file = File.expand_path(File.join(File.dirname(__FILE__), "../..", "mustache/shared/currencies.coffee"))
12
+
13
+ def currencies
14
+ TwitterCldr::Shared::Currencies.countries.inject({}) do |ret, country_name|
15
+ ret[country_name] = TwitterCldr::Shared::Currencies.for_country(country_name)
16
+ ret
17
+ end.to_json
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Js
8
+ module Renderers
9
+ autoload :Base, 'twitter_cldr/js/renderers/base'
10
+ autoload :Bundle, 'twitter_cldr/js/renderers/bundle'
11
+
12
+ module Calendars
13
+ autoload :DateTimeRenderer, 'twitter_cldr/js/renderers/calendars/datetime_renderer'
14
+ autoload :TimespanRenderer, 'twitter_cldr/js/renderers/calendars/timespan_renderer'
15
+ end
16
+
17
+ module Numbers
18
+ autoload :NumbersRenderer, 'twitter_cldr/js/renderers/numbers/numbers_renderer'
19
+ end
20
+
21
+ module PluralRules
22
+ autoload :PluralRulesCompiler, 'twitter_cldr/js/renderers/plurals/rules/plural_rules_compiler'
23
+ autoload :PluralRulesRenderer, 'twitter_cldr/js/renderers/plurals/rules/plural_rules_renderer'
24
+ end
25
+
26
+ module Shared
27
+ autoload :CurrenciesRenderer, 'twitter_cldr/js/renderers/shared/currencies_renderer'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ namespace :twitter_cldr do
7
+ task :update do
8
+ TwitterCldr::Js::Tasks.update
9
+ end
10
+
11
+ task :compile do
12
+ TwitterCldr::Js::Tasks.compile
13
+ end
14
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Js
8
+ module Tasks
9
+
10
+ class << self
11
+ def update
12
+ build(
13
+ :begin_msg => "Updating build... ",
14
+ :output_dir => File.expand_path(File.join(File.dirname(__FILE__), "../../../assets/javascripts/twitter_cldr")),
15
+ :files => { "twitter_cldr_%s.js" => false }
16
+ )
17
+ end
18
+
19
+ def compile
20
+ build(
21
+ :begin_msg => "Compiling build... ",
22
+ :output_dir => get_output_dir,
23
+ :files => { "twitter_cldr_%s.min.js" => true, "twitter_cldr_%s.js" => false }
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ def build(options = {})
30
+ locales = get_locales
31
+ $stdout.write(options[:begin_msg])
32
+
33
+ compiler = TwitterCldr::Js::Compiler.new(:locales => locales)
34
+ output_dir = options[:output_dir] || get_output_dir
35
+
36
+ build_duration = time_operation do
37
+ options[:files].each_pair do |file_pattern, minify|
38
+ compiler.compile_each(:minify => minify) do |bundle, locale|
39
+ out_file = File.join(output_dir, file_pattern % locale)
40
+ FileUtils.mkdir_p(File.basename(output_dir))
41
+ File.open(out_file, "w+") do |f|
42
+ f.write(bundle)
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ puts "done"
49
+ puts build_summary(
50
+ :locale_count => compiler.locales.size,
51
+ :build_duration => build_duration,
52
+ :dir => output_dir
53
+ )
54
+ end
55
+
56
+ def build_summary(options = {})
57
+ %Q(Built %{locale_count} %<{ "locale_count": { "one": "locale", "other": "locales" } }> %{timespan} into %{dir}).localize % {
58
+ :locale_count => options[:locale_count],
59
+ :timespan => TwitterCldr::Localized::LocalizedTimespan.new(options[:build_duration], :locale => :en).to_s.downcase,
60
+ :dir => options[:dir]
61
+ }
62
+ end
63
+
64
+ def time_operation
65
+ start_time = Time.now.to_i
66
+ yield
67
+ Time.now.to_i - start_time
68
+ end
69
+
70
+ def get_output_dir
71
+ ENV["OUTPUT_DIR"] || File.join(FileUtils.getwd, "twitter_cldr")
72
+ end
73
+
74
+ def get_locales
75
+ if ENV["LOCALES"]
76
+ locales = ENV["LOCALES"].split(",").map { |locale| TwitterCldr.convert_locale(locale.strip.downcase.to_sym) }
77
+ bad_locales = locales.select { |locale| !TwitterCldr.supported_locale?(locale) }
78
+ puts "Ignoring unsupported locales: #{bad_locales.join(", ")}"
79
+ locales - bad_locales
80
+ else
81
+ TwitterCldr.supported_locales
82
+ end
83
+ end
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Js
8
+ VERSION = "1.0.0"
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ require 'rails'
7
+ require 'twitter_cldr'
8
+ require 'mustache'
9
+ require 'json'
10
+ require 'uglifier'
11
+ require 'coffee-script'
12
+ require 'rake'
13
+
14
+ require 'twitter_cldr/js/version'
15
+
16
+ module TwitterCldr
17
+ module Js
18
+ autoload :Compiler, "twitter_cldr/js/compiler"
19
+ autoload :Renderers, "twitter_cldr/js/renderers"
20
+ autoload :Tasks, "twitter_cldr/js/tasks/tasks"
21
+
22
+ class Engine < ::Rails::Engine
23
+ end
24
+
25
+ class Railtie < ::Rails::Railtie
26
+ rake_tasks do
27
+ load "twitter_cldr/js/tasks/tasks.rake"
28
+ end
29
+ end
30
+ end
31
+ end