polyglot_js 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +87 -0
- data/Rakefile +2 -0
- data/lib/polyglot_js/version.rb +3 -0
- data/lib/polyglot_js.rb +12 -0
- data/polyglot_js.gemspec +17 -0
- metadata +69 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Pawel
|
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,87 @@
|
|
1
|
+
## PolyglotJS
|
2
|
+
|
3
|
+
PolyglotJS is very simple solution for javascript locales. It based on Gon and CoffeeScript.
|
4
|
+
|
5
|
+
## Getting Started
|
6
|
+
|
7
|
+
Add it to your gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'polyglot_js'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then use rails generator to create locales directory and empty files for your language strings:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
rails g polyglot_js:install
|
17
|
+
```
|
18
|
+
If you have some files in config/locales/ directory, generator will copy these files to javascript. For example if you have file en.yml it will create en.js.coffee file in /assets/javascripts/locales/ directory. For now it not copy your translations it only will initialize empty file as follows:
|
19
|
+
|
20
|
+
```javascript
|
21
|
+
window.en = { }
|
22
|
+
```
|
23
|
+
|
24
|
+
## PolyglotJS with Rails
|
25
|
+
|
26
|
+
Edit your application_controller.rb and add code below:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
before_filter :polyglot_js
|
30
|
+
|
31
|
+
def polyglot_js
|
32
|
+
set_polyglot_adapter :current_language => language_variable
|
33
|
+
end
|
34
|
+
```
|
35
|
+
If you use R18n-rails the language_variable can looks like R18n.get.locale.code but this variable can be different with other gems.
|
36
|
+
|
37
|
+
In your layout in head section add following instructions:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
<%= include_gon %>
|
41
|
+
<%= include_polyglot_js %>
|
42
|
+
```
|
43
|
+
|
44
|
+
Now you can enjoy your translations in javascript code.
|
45
|
+
|
46
|
+
## Creating translations
|
47
|
+
|
48
|
+
Creating translations is very smilar to ruby code. For example if you have following code in your .yml file:
|
49
|
+
|
50
|
+
```yaml
|
51
|
+
en:
|
52
|
+
layout:
|
53
|
+
welcome: "Welcome to my world"
|
54
|
+
```
|
55
|
+
|
56
|
+
You can translate it to js translations in very simple way:
|
57
|
+
|
58
|
+
```javascript
|
59
|
+
window.en = {
|
60
|
+
layout:
|
61
|
+
welcome: "Welcome to my world"
|
62
|
+
}
|
63
|
+
```
|
64
|
+
|
65
|
+
Now you can access this translation in javascript code in ruby way:
|
66
|
+
|
67
|
+
```javascript
|
68
|
+
alert(t.layout.welcome)
|
69
|
+
```
|
70
|
+
|
71
|
+
### Translations with extra parameters
|
72
|
+
|
73
|
+
You can also pass parameters to translations. To do this you must create function:
|
74
|
+
|
75
|
+
```javascript
|
76
|
+
window.en = {
|
77
|
+
layout:
|
78
|
+
welcome: (name) ->
|
79
|
+
"Welcome " + name + " to my world"
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
Now you can do it like before:
|
84
|
+
|
85
|
+
```javascript
|
86
|
+
alert(t.layout.welcome("Mike"))
|
87
|
+
```
|
data/Rakefile
ADDED
data/lib/polyglot_js.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "polyglot_js/version"
|
2
|
+
require 'polyglot_js/polyglot_js'
|
3
|
+
require 'polyglot_js/controller_adapter'
|
4
|
+
|
5
|
+
module PolyglotJs
|
6
|
+
ActionView::Base.send :include, PolyglotJs
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
initializer "polyglot_controller_adapter" do |app|
|
9
|
+
ActionController::Base.send :include, PolyglotJs::ControllerAdapter
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/polyglot_js.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/polyglot_js/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Paweł Dąbrowski"]
|
6
|
+
gem.email = ["dziamber@gmail.com"]
|
7
|
+
gem.description = "i18n for JavaScript with Rails"
|
8
|
+
gem.summary = "i18n for JavaScript with Rails"
|
9
|
+
gem.homepage = "http://dziambersky.github.com"
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "polyglot_js"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = PolyglotJs::VERSION
|
16
|
+
gem.add_dependency('gon')
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polyglot_js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paweł Dąbrowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: gon
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: i18n for JavaScript with Rails
|
31
|
+
email:
|
32
|
+
- dziamber@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/polyglot_js.rb
|
43
|
+
- lib/polyglot_js/version.rb
|
44
|
+
- polyglot_js.gemspec
|
45
|
+
homepage: http://dziambersky.github.com
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: i18n for JavaScript with Rails
|
69
|
+
test_files: []
|