yaml_locales_jsonizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d56a0422c0978cadfec19f43bdd4a76227d12192
4
+ data.tar.gz: e26e2bbd9f460c40e04b1319e5b023205e57feaa
5
+ SHA512:
6
+ metadata.gz: ba03ff85684d27898254f518c202a521bc4f26988c537c5db0dd56ca69ca9895e3c0828e8f30428eedc2237a9184f80d0bcc9628546a91aec354cd1b96baaee5
7
+ data.tar.gz: ba5aeda8b548c9e86cd7a82d2b35a22c909a40b06285b2fb7a703fe1f9f621f598a35913382ce5f7906f18e36dfcfbe3fc0fb8ac5a76832b78a7330461e71368
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .ruby-gemset
20
+ bin
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yaml_locales_jsonizer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Oleg Ivanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # YamlLocalesJsonizer
2
+
3
+ Simple generation of JSON i18n data for the frontend based on Rails-style YAML
4
+ locales.
5
+
6
+ The gem provides an asset file for asset pipeline, to be used in production,
7
+ but it is quite cumbersome to use in development. Changes in dynamic assets
8
+ derived from other resources are not being detected and picked up by sprockets
9
+ properly, and they require a server restart and cache wipe to get applied.
10
+
11
+ To give you a better development experience, an action view helper is also
12
+ available, which allows on-reload locale updates, without any extra steps.
13
+
14
+ ## Compatibility
15
+
16
+ The current version is built to support [i18next](http://i18next.com/), and
17
+ produces JSON data in the format expected by this tool. If you want to have
18
+ support for other i18n engines - open an issue with a feature request, or
19
+ (even better) - take a look at `styles/i18next.rb` file and send me a pull
20
+ request.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'yaml_locales_jsonizer'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install yaml_locales_jsonizer
35
+
36
+ ## Usage
37
+
38
+ Add to your asset manifest file, for production:
39
+
40
+ ```
41
+ //= require yaml_locales_jsonizer
42
+ ```
43
+
44
+ For effortless locale updates in dev environment, add a helper call to your
45
+ view (e.g. layout file). Make sure to add it AFTER your regular assets, to let
46
+ it override cached asset data!
47
+
48
+ ```erb
49
+ = javascript_include_tag 'application' # application assets
50
+ = yaml_locales_jsonizer if Rails.env.development?
51
+ ```
52
+
53
+ JSON data is exposed via `yaml_locales_jsonised` variable, so initialize
54
+ i18next with this variable as the resource storage:
55
+
56
+ ```javascript
57
+ i18n.init({resStore: yaml_locales_jsonized});
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ module YamlLocalesJsonizer::ActionView
2
+ module Helper
3
+
4
+ def yaml_locales_jsonizer
5
+ locales = YamlLocalesJsonizer::Loader.locales
6
+ locales_json = ActiveSupport::JSON.encode locales
7
+ javascript_tag "window.yaml_locales_jsonized = #{locales_json}"
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module YamlLocalesJsonizer
2
+ class Railtie < Rails::Engine
3
+ initializer 'yaml_locales_jsonizer.view_helper' do
4
+ ::ActionView::Base.class_eval do
5
+ include ::YamlLocalesJsonizer::ActionView::Helper
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module YamlLocalesJsonizer::Loader
2
+ extend self
3
+
4
+ def locales
5
+ locale_files.reduce({}) do |locales, file|
6
+ langdata = YAML.load_file file
7
+
8
+ langdata.each_pair do |lang, translations|
9
+ locales[lang] ||= style.empty_locale
10
+ style.add_to_locale locales[lang], translations
11
+ end
12
+
13
+ locales
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def locale_files
20
+ %w(en.yml ru.yml).map { |name| Rails.root + "config/locales/#{name}" }
21
+ end
22
+
23
+ def style
24
+ YamlLocalesJsonizer::Styles::I18next
25
+ end
26
+
27
+ end
@@ -0,0 +1,16 @@
1
+ module YamlLocalesJsonizer
2
+ module Styles
3
+ module I18next
4
+ extend self
5
+
6
+ def empty_locale
7
+ { translation: {} }
8
+ end
9
+
10
+ def add_to_locale(locale, new_data)
11
+ locale[:translation].merge! new_data
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ module YamlLocalesJsonizer::Styles
2
+ end
@@ -0,0 +1,3 @@
1
+ module YamlLocalesJsonizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'yaml_locales_jsonizer/version'
2
+ require 'yaml'
3
+
4
+ module YamlLocalesJsonizer
5
+ end
6
+
7
+ require 'yaml_locales_jsonizer/loader'
8
+ require 'yaml_locales_jsonizer/styles/i18next'
9
+ require 'yaml_locales_jsonizer/action_view/helper'
10
+ require 'yaml_locales_jsonizer/engine' if defined?(Rails)
@@ -0,0 +1,13 @@
1
+ shared_context 'locale files' do
2
+ let(:locale_en) { { kaiju: 'Kaiju' } }
3
+ let(:locale_ru) { { kaiju: 'Кайджу' } }
4
+ let(:locale_files) do
5
+ { 'en.yml' => { en: locale_en }, 'ru.yml' => { ru: locale_ru } }
6
+ end
7
+
8
+ before do
9
+ allow(YamlLocalesJsonizer::Loader).to receive(:locale_files)
10
+ .and_return(locale_files.keys)
11
+ allow(YAML).to receive(:load_file) { |file| locale_files[file] }
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ config.mock_with :rspec
3
+ config.filter_run focus: true
4
+ config.alias_example_to :fit, focus: true
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'action_view'
3
+ require 'active_support/json'
4
+ require File.expand_path('../../../lib/yaml_locales_jsonizer', __FILE__)
5
+ require 'shared/locale_files'
6
+
7
+ describe YamlLocalesJsonizer::ActionView::Helper, '#yaml_locales_jsonizer' do
8
+ include ActionView::Helpers::JavaScriptHelper
9
+ include ActionView::Helpers::TagHelper
10
+ include described_class
11
+
12
+ include_context 'locale files'
13
+
14
+ it 'generates json data' do
15
+ json_data = ActiveSupport::JSON.encode YamlLocalesJsonizer::Loader.locales
16
+ expect(yaml_locales_jsonizer).to eq javascript_tag(
17
+ "window.yaml_locales_jsonized = #{json_data}"
18
+ )
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require File.expand_path('../../../lib/yaml_locales_jsonizer', __FILE__)
3
+ require 'shared/locale_files'
4
+
5
+ describe YamlLocalesJsonizer::Loader, '.locales' do
6
+ include_context 'locale files'
7
+ subject(:load_locales) { described_class.locales }
8
+
9
+ it 'loads locale files' do
10
+ expect(load_locales).to eq ({
11
+ en: { translation: locale_en },
12
+ ru: { translation: locale_ru }
13
+ })
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ <%
2
+ locales = YamlLocalesJsonizer::Loader.locales
3
+ %>
4
+ window.yaml_locales_jsonized = <%= locales.to_json %>;
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yaml_locales_jsonizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'yaml_locales_jsonizer'
8
+ spec.version = YamlLocalesJsonizer::VERSION
9
+ spec.authors = ['Oleg Ivanov']
10
+ spec.email = ['morhekil@morhekil.net']
11
+ spec.description = %q{Simple generation of JSON data for the frontend based on Rails-style YAML locales}
12
+ spec.summary = %q{Simple generation of JSON data for the frontend based on Rails-style YAML locales}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'actionpack', '~> 3.0'
25
+ spec.add_development_dependency 'activesupport', '~> 3.0'
26
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_locales_jsonizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Ivanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: actionpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Simple generation of JSON data for the frontend based on Rails-style
84
+ YAML locales
85
+ email:
86
+ - morhekil@morhekil.net
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - lib/yaml_locales_jsonizer.rb
97
+ - lib/yaml_locales_jsonizer/action_view/helper.rb
98
+ - lib/yaml_locales_jsonizer/engine.rb
99
+ - lib/yaml_locales_jsonizer/loader.rb
100
+ - lib/yaml_locales_jsonizer/styles.rb
101
+ - lib/yaml_locales_jsonizer/styles/i18next.rb
102
+ - lib/yaml_locales_jsonizer/version.rb
103
+ - spec/shared/locale_files.rb
104
+ - spec/spec_helper.rb
105
+ - spec/yaml_locales_jsonizer/action_view_spec.rb
106
+ - spec/yaml_locales_jsonizer/loader_spec.rb
107
+ - vendor/assets/javascripts/yaml_locales_jsonizer.js.erb
108
+ - yaml_locales_jsonizer.gemspec
109
+ homepage: ''
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.1.11
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Simple generation of JSON data for the frontend based on Rails-style YAML
133
+ locales
134
+ test_files:
135
+ - spec/shared/locale_files.rb
136
+ - spec/spec_helper.rb
137
+ - spec/yaml_locales_jsonizer/action_view_spec.rb
138
+ - spec/yaml_locales_jsonizer/loader_spec.rb