ember-i18n-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +47 -0
- data/README.md +28 -0
- data/Rakefile +7 -0
- data/config/ember-i18n.yml +22 -0
- data/ember-i18n-rails.gemspec +26 -0
- data/lib/ember-i18n-rails.rb +119 -0
- data/lib/ember/i18n/engine.rb +63 -0
- data/lib/ember/i18n/railtie.rb +13 -0
- data/lib/ember/i18n/rake.rb +19 -0
- data/lib/ember/i18n/version.rb +10 -0
- data/spec/ember_i18n_rails_spec.rb +80 -0
- data/spec/resources/en.yml +7 -0
- data/spec/spec_helper.rb +19 -0
- data/vendor/assets/javascripts/ember-i18n.js +155 -0
- data/vendor/assets/javascripts/i18n/translations.js.erb +9 -0
- metadata +169 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ember-i18n-rails (0.1.0)
|
5
|
+
i18n
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (3.1.1)
|
11
|
+
multi_json (~> 1.0)
|
12
|
+
coderay (0.9.8)
|
13
|
+
diff-lcs (1.1.3)
|
14
|
+
fakeweb (1.3.0)
|
15
|
+
i18n (0.6.1)
|
16
|
+
method_source (0.6.7)
|
17
|
+
ruby_parser (>= 2.3.1)
|
18
|
+
multi_json (1.0.3)
|
19
|
+
pry (0.9.7.4)
|
20
|
+
coderay (~> 0.9.8)
|
21
|
+
method_source (~> 0.6.7)
|
22
|
+
ruby_parser (>= 2.3.1)
|
23
|
+
slop (~> 2.1.0)
|
24
|
+
rake (0.9.2.2)
|
25
|
+
rspec (2.7.0)
|
26
|
+
rspec-core (~> 2.7.0)
|
27
|
+
rspec-expectations (~> 2.7.0)
|
28
|
+
rspec-mocks (~> 2.7.0)
|
29
|
+
rspec-core (2.7.1)
|
30
|
+
rspec-expectations (2.7.0)
|
31
|
+
diff-lcs (~> 1.1.2)
|
32
|
+
rspec-mocks (2.7.0)
|
33
|
+
ruby_parser (2.3.1)
|
34
|
+
sexp_processor (~> 3.0)
|
35
|
+
sexp_processor (3.0.8)
|
36
|
+
slop (2.1.0)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
activesupport (>= 3.0.0)
|
43
|
+
ember-i18n-rails!
|
44
|
+
fakeweb
|
45
|
+
pry
|
46
|
+
rake
|
47
|
+
rspec (~> 2.6)
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#ember-i18n-rails#
|
2
|
+
|
3
|
+
Loosely Based on https://github.com/fnando/i18n-js of Nando Vieira
|
4
|
+
|
5
|
+
__Version:__ 0.1
|
6
|
+
|
7
|
+
## Instalation:
|
8
|
+
|
9
|
+
### in Gemfile:
|
10
|
+
|
11
|
+
gem "ember-i18n-rails"
|
12
|
+
|
13
|
+
### then call:
|
14
|
+
|
15
|
+
rake ember:i18n:setup
|
16
|
+
|
17
|
+
### in app/assets/javascript/application.js
|
18
|
+
//= require i18n/ember-i18n
|
19
|
+
//= require i18n/translations
|
20
|
+
|
21
|
+
## Exporting locales:
|
22
|
+
|
23
|
+
rake ember:i18n:export
|
24
|
+
|
25
|
+
## Updating ember-i18n
|
26
|
+
|
27
|
+
rake ember:i18n:update
|
28
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Split context in several files.
|
2
|
+
# By default only one file with all translations is exported and
|
3
|
+
# no configuration is required. Your settings for asset pipeline
|
4
|
+
# are automatically recognized.
|
5
|
+
#
|
6
|
+
# If you want to split translations into several files or specify
|
7
|
+
# locale contexts that will be exported, just use this file to do
|
8
|
+
# so.
|
9
|
+
#
|
10
|
+
# If you're going to use the Rails 3.1 asset pipeline, change
|
11
|
+
# the following configuration to something like this:
|
12
|
+
#
|
13
|
+
# translations:
|
14
|
+
# - file: "app/assets/javascripts/i18n/translations.js"
|
15
|
+
#
|
16
|
+
# If you're running an old version, you can use something
|
17
|
+
# like this:
|
18
|
+
#
|
19
|
+
# translations:
|
20
|
+
# - file: "public/javascripts/translations.js"
|
21
|
+
# only: "*"
|
22
|
+
#
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ember/i18n/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ember-i18n-rails"
|
7
|
+
s.version = Ember::I18n::Version::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nando Vieira", "Andrzej Sliwa"]
|
10
|
+
s.email = ["andrzej.sliwa@i-tool.eu"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/ember-i18n-rails"
|
12
|
+
s.summary = ""
|
13
|
+
s.description = s.summary
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "i18n"
|
21
|
+
s.add_development_dependency "fakeweb"
|
22
|
+
s.add_development_dependency "activesupport", ">= 3.0.0"
|
23
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
24
|
+
s.add_development_dependency "rake"
|
25
|
+
s.add_development_dependency "pry"
|
26
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require "FileUtils" unless defined?(FileUtils)
|
2
|
+
|
3
|
+
module Ember
|
4
|
+
module I18n
|
5
|
+
extend self
|
6
|
+
|
7
|
+
require "ember/i18n/railtie" if Rails.version >= "3.0"
|
8
|
+
require "ember/i18n/engine" if Rails.version >= "3.1"
|
9
|
+
|
10
|
+
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
11
|
+
MERGER = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2 }
|
12
|
+
|
13
|
+
# Under rails 3.1.1 and higher, perform a check to ensure that the
|
14
|
+
# full environment will be available during asset compilation.
|
15
|
+
# This is required to ensure I18n is loaded.
|
16
|
+
def assert_usable_configuration!
|
17
|
+
@usable_configuration ||= Rails.version >= "3.1.1" &&
|
18
|
+
Rails.configuration.assets.initialize_on_precompile ||
|
19
|
+
raise("Cannot precompile ember-i18n translations unless environment is initialized. Please set config.assets.initialize_on_precompile to true.")
|
20
|
+
end
|
21
|
+
|
22
|
+
def has_asset_pipeline?
|
23
|
+
Rails.configuration.respond_to?(:assets) && Rails.configuration.assets.enabled
|
24
|
+
end
|
25
|
+
|
26
|
+
def config_file
|
27
|
+
Rails.root.join("config/ember-i18n.yml")
|
28
|
+
end
|
29
|
+
|
30
|
+
def export_dir
|
31
|
+
if has_asset_pipeline?
|
32
|
+
"app/assets/javascripts/i18n"
|
33
|
+
else
|
34
|
+
"public/javascripts"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def javascript_file
|
39
|
+
Rails.root.join(export_dir, "ember-i18n.js")
|
40
|
+
end
|
41
|
+
|
42
|
+
# Export translations to JavaScript, considering settings
|
43
|
+
# from configuration file
|
44
|
+
def export!
|
45
|
+
save(flat_hash(translations), File.join(export_dir, "translations.js"))
|
46
|
+
end
|
47
|
+
|
48
|
+
def flat_hash(data, prefix = '', result = {})
|
49
|
+
data.each do |key, value|
|
50
|
+
current_prefix = prefix.present? ? "#{prefix}.#{key}" : key
|
51
|
+
|
52
|
+
if !value.is_a?(Hash)
|
53
|
+
result[current_prefix] = value.respond_to?(:stringify_keys) ? value.stringify_keys : value
|
54
|
+
else
|
55
|
+
flat_hash(value, current_prefix, result)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
result.stringify_keys
|
60
|
+
end
|
61
|
+
|
62
|
+
# Load configuration file for partial exporting and
|
63
|
+
# custom output directory
|
64
|
+
def config
|
65
|
+
if config?
|
66
|
+
(YAML.load_file(config_file) || {}).with_indifferent_access
|
67
|
+
else
|
68
|
+
{}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Check if configuration file exist
|
73
|
+
def config?
|
74
|
+
File.file? config_file
|
75
|
+
end
|
76
|
+
|
77
|
+
# Copy configuration and JavaScript library files to
|
78
|
+
# <tt>config/ember-i18n.yml</tt> and <tt>public/javascripts/i18n.js</tt>.
|
79
|
+
def setup!
|
80
|
+
FileUtils.mkdir_p(export_dir)
|
81
|
+
FileUtils.cp(File.dirname(__FILE__) + "/../vendor/assets/javascripts/ember-i18n.js", javascript_file)
|
82
|
+
FileUtils.cp(File.dirname(__FILE__) + "/../config/ember-i18n.yml", config_file) unless config?
|
83
|
+
end
|
84
|
+
|
85
|
+
# Retrieve an updated JavaScript library from Github.
|
86
|
+
def update!
|
87
|
+
FileUtils.mkdir_p(export_dir)
|
88
|
+
require "open-uri"
|
89
|
+
contents = open("https://raw.github.com/jamesarosen/ember-i18n/master/lib/i18n.js").read
|
90
|
+
File.open(javascript_file, "w+") {|f| f << contents}
|
91
|
+
end
|
92
|
+
|
93
|
+
# Convert translations to JSON string and save file.
|
94
|
+
def save(translations, file)
|
95
|
+
file = Rails.root.join(file)
|
96
|
+
FileUtils.mkdir_p File.dirname(file)
|
97
|
+
|
98
|
+
File.open(file, "w+") do |f|
|
99
|
+
f << 'Em.I18n.translations = '
|
100
|
+
f << JSON.pretty_generate(translations).html_safe
|
101
|
+
f << ';'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Initialize and return translations
|
106
|
+
def translations
|
107
|
+
::I18n.load_path = default_locales_path
|
108
|
+
::I18n.backend.instance_eval do
|
109
|
+
init_translations unless initialized?
|
110
|
+
translations
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def default_locales_path
|
115
|
+
Dir[Rails.root.join('config', 'locales', '*.yml').to_s]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Ember
|
2
|
+
module I18n
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
I18N_TRANSLATIONS_ASSET = "i18n/translations"
|
5
|
+
|
6
|
+
initializer "ember-i18n-rails.asset_dependencies", :after => "sprockets.environment",
|
7
|
+
:before => "ember-i18n-rails.initialize" do
|
8
|
+
next unless Ember::I18n.has_asset_pipeline?
|
9
|
+
|
10
|
+
config = I18n.config_file
|
11
|
+
cache_file = I18n::Engine.load_path_hash_cache
|
12
|
+
|
13
|
+
Rails.application.assets.register_preprocessor "application/javascript", :"ember-i18n-rails_dependencies" do |context, data|
|
14
|
+
if context.logical_path == I18N_TRANSLATIONS_ASSET
|
15
|
+
context.depend_on(config) if I18n.config?
|
16
|
+
# also set up dependencies on every locale file
|
17
|
+
::I18n.load_path.each {|path| context.depend_on(path)}
|
18
|
+
|
19
|
+
# Set up a dependency on the contents of the load path
|
20
|
+
# itself. In some situations it is possible to get here
|
21
|
+
# before the path hash cache file has been written; in
|
22
|
+
# this situation, write it now.
|
23
|
+
I18n::Engine.write_hash! unless File.exists?(cache_file)
|
24
|
+
context.depend_on(cache_file)
|
25
|
+
end
|
26
|
+
|
27
|
+
data
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# rewrite path cache hash at startup and before each request in development
|
32
|
+
config.to_prepare do
|
33
|
+
next unless Ember::I18n.has_asset_pipeline?
|
34
|
+
Ember::I18n::Engine.write_hash_if_changed unless Rails.env.production?
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.load_path_hash_cache
|
38
|
+
@load_path_hash_cache ||= Rails.root.join("tmp/ember-i18n.cache")
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.write_hash_if_changed
|
42
|
+
load_path_hash = ::I18n.load_path.hash
|
43
|
+
|
44
|
+
if load_path_hash != cached_load_path_hash
|
45
|
+
self.cached_load_path_hash = load_path_hash
|
46
|
+
write_hash!
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.write_hash!
|
51
|
+
FileUtils.mkdir_p Rails.root.join("tmp")
|
52
|
+
|
53
|
+
File.open(load_path_hash_cache, "w+") do |f|
|
54
|
+
f.write(cached_load_path_hash)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class << self
|
59
|
+
attr_accessor :cached_load_path_hash
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Ember
|
2
|
+
module I18n
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
rake_tasks do
|
5
|
+
require "ember/i18n/rake"
|
6
|
+
end
|
7
|
+
|
8
|
+
initializer "ember-i18n-rails.initialize" do |app|
|
9
|
+
app.config.middleware.use(Middleware) if Rails.env.development? && !Ember::I18n.has_asset_pipeline?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
namespace :ember do
|
2
|
+
namespace :i18n do
|
3
|
+
desc "install ember-i18n.js"
|
4
|
+
task :setup => :environment do
|
5
|
+
Ember::I18n.setup!
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Export the messages files"
|
9
|
+
task :export => :environment do
|
10
|
+
Ember::I18n.export!
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "install ember-i18n.js"
|
14
|
+
task :update => :environment do
|
15
|
+
Ember::I18n.update!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
if File.basename(Rails.root) != "tmp"
|
4
|
+
abort <<-TXT
|
5
|
+
\e[31;5m
|
6
|
+
WARNING: That will remove your project!
|
7
|
+
Please go to #{File.expand_path(File.dirname(__FILE__) + "/..")} and run `rake spec`\e[0m
|
8
|
+
TXT
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Ember::I18n do
|
12
|
+
before do
|
13
|
+
# Remove temporary directory if already present
|
14
|
+
FileUtils.rm_r(Rails.root) if File.exist?(Rails.root)
|
15
|
+
|
16
|
+
# Create temporary directory to test the files generation
|
17
|
+
%w( config public/javascripts ).each do |path|
|
18
|
+
FileUtils.mkdir_p Rails.root.join(path)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
after do
|
23
|
+
# Remove temporary directory
|
24
|
+
FileUtils.rm_r(Rails.root)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "copies JavaScript library" do
|
28
|
+
path = Rails.root.join("public/javascripts/ember-i18n.js")
|
29
|
+
|
30
|
+
File.should_not be_file(path)
|
31
|
+
Ember::I18n.setup!
|
32
|
+
File.should be_file(path)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "exports messages" do
|
36
|
+
default_locales_path = Dir[File.join('spec', 'resources', '*.yml').to_s]
|
37
|
+
Ember::I18n.should_receive(:default_locales_path).at_least(:once).and_return(default_locales_path)
|
38
|
+
Ember::I18n.export!
|
39
|
+
Rails.root.join(Ember::I18n.export_dir, "translations.js").should be_file
|
40
|
+
end
|
41
|
+
|
42
|
+
it "updates the javascript library" do
|
43
|
+
FakeWeb.register_uri(:get, "https://raw.github.com/jamesarosen/ember-i18n/master/lib/i18n.js", :body => "UPDATED")
|
44
|
+
|
45
|
+
Ember::I18n.setup!
|
46
|
+
Ember::I18n.update!
|
47
|
+
File.read(Ember::I18n.javascript_file).should == "UPDATED"
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#export_dir" do
|
51
|
+
it "detects asset pipeline support" do
|
52
|
+
Ember::I18n.stub :has_asset_pipeline? => true
|
53
|
+
Ember::I18n.export_dir == "vendor/assets/javascripts"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "detects older Rails" do
|
57
|
+
Ember::I18n.stub :has_asset_pipeline? => false
|
58
|
+
Ember::I18n.export_dir.to_s.should == "public/javascripts"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#has_asset_pipeline?" do
|
63
|
+
it "detects support" do
|
64
|
+
Rails.stub_chain(:configuration, :assets, :enabled => true)
|
65
|
+
Ember::I18n.should have_asset_pipeline
|
66
|
+
end
|
67
|
+
|
68
|
+
it "skips support" do
|
69
|
+
Ember::I18n.should_not have_asset_pipeline
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# Shortcut to SimplesIdeias::I18n.translations
|
76
|
+
def translations
|
77
|
+
Ember::I18n.translations
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "active_support/all"
|
2
|
+
require "active_support/version"
|
3
|
+
require "active_support/test_case"
|
4
|
+
require "ostruct"
|
5
|
+
require "pathname"
|
6
|
+
require "i18n"
|
7
|
+
require "json"
|
8
|
+
require "fakeweb"
|
9
|
+
|
10
|
+
FakeWeb.allow_net_connect = false
|
11
|
+
|
12
|
+
# Stub Rails.root, so we don"t need to load the whole Rails environment.
|
13
|
+
# Be careful! The specified folder will be removed!
|
14
|
+
Rails = OpenStruct.new({
|
15
|
+
:root => Pathname.new(File.dirname(__FILE__) + "/tmp"),
|
16
|
+
:version => "0"
|
17
|
+
})
|
18
|
+
|
19
|
+
require "ember-i18n-rails"
|
@@ -0,0 +1,155 @@
|
|
1
|
+
(function(window) {
|
2
|
+
var I18n, assert, findTemplate, get, isBinding, isTranslatedAttribute, lookupKey, pluralForm;
|
3
|
+
|
4
|
+
isTranslatedAttribute = /(.+)Translation$/;
|
5
|
+
|
6
|
+
get = Ember.Handlebars.get || Ember.Handlebars.getPath || Ember.getPath;
|
7
|
+
|
8
|
+
if (typeof CLDR !== "undefined" && CLDR !== null) pluralForm = CLDR.pluralForm;
|
9
|
+
|
10
|
+
if (pluralForm == null) {
|
11
|
+
Ember.Logger.warn("CLDR.pluralForm not found. Em.I18n will not support count-based inflection.");
|
12
|
+
}
|
13
|
+
|
14
|
+
lookupKey = function(key, hash) {
|
15
|
+
var firstKey, idx, remainingKeys, result;
|
16
|
+
result = hash[key];
|
17
|
+
idx = key.indexOf('.');
|
18
|
+
|
19
|
+
if (!result && idx !== -1) {
|
20
|
+
firstKey = key.substr(0, idx);
|
21
|
+
remainingKeys = key.substr(idx + 1);
|
22
|
+
hash = hash[firstKey];
|
23
|
+
if (hash) result = lookupKey(remainingKeys, hash);
|
24
|
+
}
|
25
|
+
|
26
|
+
return result;
|
27
|
+
};
|
28
|
+
|
29
|
+
assert = Ember.assert != null ? Ember.assert : window.ember_assert;
|
30
|
+
|
31
|
+
findTemplate = function(key, setOnMissing) {
|
32
|
+
assert("You must provide a translation key string, not %@".fmt(key), typeof key === 'string');
|
33
|
+
var result = lookupKey(key, I18n.translations);
|
34
|
+
|
35
|
+
if (setOnMissing) {
|
36
|
+
if (result == null) {
|
37
|
+
result = I18n.translations[key] = I18n.compile("Missing translation: " + key);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
if ((result != null) && !jQuery.isFunction(result)) {
|
42
|
+
result = I18n.translations[key] = I18n.compile(result);
|
43
|
+
}
|
44
|
+
|
45
|
+
return result;
|
46
|
+
};
|
47
|
+
|
48
|
+
I18n = {
|
49
|
+
compile: Handlebars.compile,
|
50
|
+
|
51
|
+
translations: {},
|
52
|
+
|
53
|
+
template: function(key, count) {
|
54
|
+
var interpolatedKey, result, suffix;
|
55
|
+
if ((count != null) && (pluralForm != null)) {
|
56
|
+
suffix = pluralForm(count);
|
57
|
+
interpolatedKey = "%@.%@".fmt(key, suffix);
|
58
|
+
result = findTemplate(interpolatedKey, false);
|
59
|
+
}
|
60
|
+
return result != null ? result : result = findTemplate(key, true);
|
61
|
+
},
|
62
|
+
|
63
|
+
t: function(key, context) {
|
64
|
+
var template;
|
65
|
+
if (context == null) context = {};
|
66
|
+
template = I18n.template(key, context.count);
|
67
|
+
return template(context);
|
68
|
+
},
|
69
|
+
|
70
|
+
TranslateableAttributes: Em.Mixin.create({
|
71
|
+
didInsertElement: function() {
|
72
|
+
var attribute, isTranslatedAttributeMatch, key, path, result, translatedValue;
|
73
|
+
result = this._super.apply(this, arguments);
|
74
|
+
|
75
|
+
for (key in this) {
|
76
|
+
path = this[key];
|
77
|
+
isTranslatedAttributeMatch = key.match(isTranslatedAttribute);
|
78
|
+
if (isTranslatedAttributeMatch) {
|
79
|
+
attribute = isTranslatedAttributeMatch[1];
|
80
|
+
translatedValue = I18n.t(path);
|
81
|
+
this.$().attr(attribute, translatedValue);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
return result;
|
86
|
+
}
|
87
|
+
})
|
88
|
+
};
|
89
|
+
|
90
|
+
Ember.I18n = I18n;
|
91
|
+
|
92
|
+
isBinding = /(.+)Binding$/;
|
93
|
+
|
94
|
+
Handlebars.registerHelper('t', function(key, options) {
|
95
|
+
var attrs, context, data, elementID, result, tagName, view;
|
96
|
+
context = this;
|
97
|
+
attrs = options.hash;
|
98
|
+
data = options.data;
|
99
|
+
view = data.view;
|
100
|
+
tagName = attrs.tagName || 'span';
|
101
|
+
delete attrs.tagName;
|
102
|
+
elementID = "i18n-" + (Ember.uuid++);
|
103
|
+
|
104
|
+
Em.keys(attrs).forEach(function(property) {
|
105
|
+
var bindPath, currentValue, invoker, isBindingMatch, normalized, normalizedPath, observer, propertyName, root, _ref;
|
106
|
+
isBindingMatch = property.match(isBinding);
|
107
|
+
|
108
|
+
if (isBindingMatch) {
|
109
|
+
propertyName = isBindingMatch[1];
|
110
|
+
bindPath = attrs[property];
|
111
|
+
currentValue = get(context, bindPath, options);
|
112
|
+
attrs[propertyName] = currentValue;
|
113
|
+
invoker = null;
|
114
|
+
normalized = Ember.Handlebars.normalizePath(context, bindPath, data);
|
115
|
+
_ref = [normalized.root, normalized.path], root = _ref[0], normalizedPath = _ref[1];
|
116
|
+
|
117
|
+
observer = function() {
|
118
|
+
var elem, newValue;
|
119
|
+
if (view.get('state') !== 'inDOM') {
|
120
|
+
Em.removeObserver(root, normalizedPath, invoker);
|
121
|
+
return;
|
122
|
+
}
|
123
|
+
newValue = get(context, bindPath, options);
|
124
|
+
elem = view.$("#" + elementID);
|
125
|
+
attrs[propertyName] = newValue;
|
126
|
+
return elem.html(I18n.t(key, attrs));
|
127
|
+
};
|
128
|
+
|
129
|
+
invoker = function() {
|
130
|
+
return Em.run.once(observer);
|
131
|
+
};
|
132
|
+
|
133
|
+
return Em.addObserver(root, normalizedPath, invoker);
|
134
|
+
}
|
135
|
+
});
|
136
|
+
|
137
|
+
result = '<%@ id="%@">%@</%@>'.fmt(tagName, elementID, I18n.t(key, attrs), tagName);
|
138
|
+
return new Handlebars.SafeString(result);
|
139
|
+
});
|
140
|
+
|
141
|
+
Handlebars.registerHelper('translateAttr', function(options) {
|
142
|
+
var attrs, result;
|
143
|
+
attrs = options.hash;
|
144
|
+
result = [];
|
145
|
+
|
146
|
+
Em.keys(attrs).forEach(function(property) {
|
147
|
+
var translatedValue;
|
148
|
+
translatedValue = I18n.t(attrs[property]);
|
149
|
+
return result.push('%@="%@"'.fmt(property, translatedValue));
|
150
|
+
});
|
151
|
+
|
152
|
+
return new Handlebars.SafeString(result.join(' '));
|
153
|
+
});
|
154
|
+
|
155
|
+
}).call(undefined, this);
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<%# encoding: utf-8%>
|
2
|
+
|
3
|
+
<% Ember::I18n.assert_usable_configuration! %>
|
4
|
+
var I18n = I18n || {};
|
5
|
+
I18n.translations = <%=
|
6
|
+
Ember::I18n.translation_segments.each_with_object({}) do |(name, segment),translations|
|
7
|
+
translations.merge!(segment)
|
8
|
+
end.to_json
|
9
|
+
%>;
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ember-i18n-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nando Vieira
|
9
|
+
- Andrzej Sliwa
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: i18n
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: fakeweb
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activesupport
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.0.0
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '2.6'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '2.6'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: rake
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :development
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: pry
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
type: :development
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ''
|
112
|
+
email:
|
113
|
+
- andrzej.sliwa@i-tool.eu
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- Gemfile
|
121
|
+
- Gemfile.lock
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- config/ember-i18n.yml
|
125
|
+
- ember-i18n-rails.gemspec
|
126
|
+
- lib/ember-i18n-rails.rb
|
127
|
+
- lib/ember/i18n/engine.rb
|
128
|
+
- lib/ember/i18n/railtie.rb
|
129
|
+
- lib/ember/i18n/rake.rb
|
130
|
+
- lib/ember/i18n/version.rb
|
131
|
+
- spec/ember_i18n_rails_spec.rb
|
132
|
+
- spec/resources/en.yml
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- vendor/assets/javascripts/ember-i18n.js
|
135
|
+
- vendor/assets/javascripts/i18n/translations.js.erb
|
136
|
+
homepage: http://rubygems.org/gems/ember-i18n-rails
|
137
|
+
licenses: []
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
hash: -3474424650723491457
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
segments:
|
158
|
+
- 0
|
159
|
+
hash: -3474424650723491457
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 1.8.23
|
163
|
+
signing_key:
|
164
|
+
specification_version: 3
|
165
|
+
summary: ''
|
166
|
+
test_files:
|
167
|
+
- spec/ember_i18n_rails_spec.rb
|
168
|
+
- spec/resources/en.yml
|
169
|
+
- spec/spec_helper.rb
|