javascript_localize 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/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jinzhu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,30 @@
1
+ = Javascript Localize
2
+
3
+ == INSTALL:
4
+ $ ./script/plugin install git://github.com/jinzhu/locale_javascript.git
5
+
6
+ == How To Use:
7
+ 1. drop all localized content into RAILS_ROOT/js_locales/
8
+ e.g:
9
+ RAILS_ROOT/js_locales/en.yml
10
+ hello world : 'Hello World!'
11
+ hello world for you : '{{you}},Hello World!'
12
+
13
+ RAILS_ROOT/js_locales/zh.yml
14
+ hello world : '世界你好!'
15
+ hello world for you : '{{you}},世界你好!'
16
+
17
+ 2. include into views
18
+ <%= include_localized_javascript %>
19
+
20
+ 3. Use it as javascript
21
+ _('hello world') => en: 'Hello World!' zh: '世界你好!'
22
+ _('hello world for you',{you : 'ZhangJinzhu'}) => en: 'ZhangJinzhu,Hello World!' zh: 'ZhangJinzhu,世界你好!'
23
+
24
+ 4. Rake Task
25
+ rake js_locale:sync => Keep localized content in sync with default locale
26
+ rake js_locale:build => Generate javascript localization files
27
+
28
+ Author : ZhangJinzhu
29
+ GitHub : http://github.com/jinzhu
30
+ Email : wosmvp (no-spam) gmail (no-spam) com
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActionView::Base.send :include, JavascriptLocalize
@@ -0,0 +1,61 @@
1
+ module JavascriptLocalize
2
+ module Methods
3
+
4
+ # _ = function(str,opt){
5
+ # var localeString = __LocaleString[str];
6
+ # localeString = (localeString && localeString != '') ? localeString : str;
7
+ #
8
+ # if(opt){
9
+ # fn = new Function("obj",
10
+ # "var p=[],print=function(){p.push.apply(p,arguments);};" +
11
+ #
12
+ # "with(obj){p.push(\"" +
13
+ # localeString
14
+ # .replace(/[\t]/g, " ")
15
+ # .replace(/\"/g, '\\"')
16
+ # .split("{{").join("\t")
17
+ # .replace(/\t(.*?)}}/g, "\",function(){ try { return $1 }catch(err){ return '$1 undefined'} }.apply(this),\"")
18
+ # + "\");};return p.join('');");
19
+ #
20
+ # return fn.call(this,opt)
21
+ # }else{
22
+ # return localeString
23
+ # }
24
+ # }
25
+
26
+ def include_localized_javascript
27
+ return %Q{
28
+ <script src='/javascripts/locales/#{I18n.locale}.js' type='text/javascript' charset='utf-8'></script>
29
+ <script type='text/javascript'>
30
+ _ = function(str,opt){
31
+ var localeString = __LocaleString[str];
32
+ localeString = (localeString && localeString != '') ? localeString : str;
33
+
34
+ if(opt){
35
+ fn = new Function("obj",
36
+ "var p=[],print=function(){p.push.apply(p,arguments);};" +
37
+
38
+ "with(obj){p.push(\\"" +
39
+ localeString
40
+ .replace(/[\\t]/g, " ")
41
+ .replace(/\\"/g, '\\\\"')
42
+ .split("{{").join("\\t")
43
+ .replace(/\\t(.*?)}}/g, "\\",function(){ try { return $1 }catch(err){ return '$1 undefined'} }.apply(this),\\"")
44
+ + "\\");};return p.join('');");
45
+
46
+ return fn.call(this,opt)
47
+ }else{
48
+ return localeString
49
+ }
50
+ }
51
+ </script>
52
+ }
53
+ end
54
+ end
55
+
56
+ class Railtie < ::Rails::Railtie
57
+ initializer :include_javascript_localize do |app|
58
+ ActionView::Base.send :include, JavascriptLocalize::Methods
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'ya2yaml'
3
+ $KCODE='u'
4
+
5
+ def copy_hash(old,new)
6
+ old.map do |key,value|
7
+ next if new[key] && new[key].class != Hash
8
+ new.update(key => (value.class == Hash) ? (new[key] ? copy_hash(value,new[key]) : value) : value)
9
+ end
10
+ return new
11
+ end
12
+
13
+ namespace :javascript_localize do
14
+ desc 'sync javascript localization files'
15
+ task :sync => :environment do
16
+ result = {}
17
+
18
+ I18n.available_locales.map do |x|
19
+ file = File.join(RAILS_ROOT,'js_locales',"#{x.to_s}.yml")
20
+ result.update({x => file})
21
+ result.update({"#{x.to_s}_content" =>
22
+ File.exist?(file) ? (YAML.load_file(file) || {}) : {}
23
+ })
24
+ end
25
+
26
+ (I18n.available_locales - [I18n.default_locale]).map do |x|
27
+ f = File.new(result[x],'w')
28
+ f << copy_hash(result["#{I18n.default_locale.to_s}_content"],result["#{x.to_s}_content"]).ya2yaml
29
+ f.close
30
+ end
31
+ end
32
+
33
+ desc 'build javascript localization files'
34
+ task :build => :environment do
35
+ path = File.join(RAILS_ROOT,'public','javascripts','locales')
36
+ FileUtils.mkdir_p(path) if !File.exist?(path)
37
+
38
+ I18n.available_locales.map do |x|
39
+ f = File.new(File.join(path,x.to_s + '.js'),'w')
40
+ f.write '__LocaleString = '
41
+ f.write YAML.load_file(File.join(RAILS_ROOT,'js_locales',"#{x.to_s}.yml")).inspect.gsub(/\"=>\"/,'" : "')
42
+ f.close
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: javascript_localize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jinzhu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-08 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ requirement: *id001
33
+ prerelease: false
34
+ name: shoulda
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ requirement: *id002
49
+ prerelease: false
50
+ name: bundler
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 2
63
+ version: 1.5.2
64
+ requirement: *id003
65
+ prerelease: false
66
+ name: jeweler
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirement: *id004
79
+ prerelease: false
80
+ name: rcov
81
+ description: javascript localize solution
82
+ email: wosmvp@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README
90
+ files:
91
+ - README
92
+ - init.rb
93
+ - lib/javascript_localize.rb
94
+ - lib/tasks/javascript_localize.rake
95
+ - LICENSE.txt
96
+ has_rdoc: true
97
+ homepage: http://github.com/jinzhu/javascript_localize
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.4.2
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: javascript localize solution
130
+ test_files: []
131
+