internashunalize-js 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in internashunalize-js.gemspec
4
+ gemspec
data/README.textile ADDED
@@ -0,0 +1,37 @@
1
+ h2. Intro
2
+
3
+ This gem takes an other approach to Javascript internationalization than most others do.
4
+ I use this in production but you might want to look at it as a proof of concept.
5
+ The goal is to build a lightweight, unobtrusive and powerful translation solution for Frontend applications by using standard libraries.
6
+
7
+ Right now, you'll have to manually provide the following depencencies: Rails, jQuery and Mustache.
8
+
9
+ h2. Installation
10
+
11
+ h3. Add the gem to your Gemfile
12
+
13
+ bc. gem 'internashunalize-js'
14
+
15
+ h3. Run the generator
16
+
17
+ bc. rails g internashunalize:js:install
18
+
19
+ Don't forget to include the javascript in your Html.
20
+
21
+ h3. Mark translations you want to use in your scripts
22
+
23
+ bc. <% t(:yml_key, :javascript => true) %>
24
+
25
+ h3. Place the view helper somewhere in your layout / views
26
+
27
+ bc. <%= javascript_translations %>
28
+
29
+ h2. Usage
30
+
31
+ Start using the marked translations in your Javascript
32
+
33
+ bc. I18n.t('yml_key')
34
+
35
+ For interpolating values, you can pass a Json object as second argument
36
+
37
+ bc. I18n.t('yml_key_with_variable', { your_variable: 'Variable content' })
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "internashunalize-js/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "internashunalize-js"
7
+ s.version = Internashunalize::Js::VERSION
8
+ s.authors = ["Christian Felder (masone)"]
9
+ s.email = ["ema@rh-productions.ch"]
10
+ s.homepage = "http://masone.ch"
11
+ s.summary = "I18n for your Javascript."
12
+ s.description = "Uses the I18n library to provide translated strings to your Javascript."
13
+
14
+ #s.add_dependency "railties", "~> 3.0"
15
+ s.rubyforge_project = "internashunalize-js"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # specify any dependencies here; for example:
23
+ # s.add_development_dependency "rspec"
24
+ s.add_runtime_dependency "i18n"
25
+ end
@@ -0,0 +1,16 @@
1
+ module Internashunalize
2
+ module Generators
3
+
4
+ class InstallGenerator < Rails::Generators::Base
5
+
6
+ desc "This generator installs internashunalize.js"
7
+ source_root File.expand_path('../../../../vendor/assets/javascripts', __FILE__)
8
+
9
+ def copy
10
+ say_status("copying", "internashunalize.js", :green)
11
+ copy_file "internashunalize.js", "public/javascripts/internashunalize.js"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ module I18n
2
+
3
+ class << self
4
+
5
+ alias :translate_core :translate
6
+ alias :t :translate
7
+
8
+ def translate(*args)
9
+ args_core = args.dup
10
+ options = args.last.is_a?(Hash) ? args.pop : {}
11
+ javascript = options.delete(:javascript)
12
+ key = args.shift
13
+
14
+ translation = self.translate_core(*args_core)
15
+
16
+ if javascript
17
+ @javascript_translations ||= {}
18
+ translation.gsub!(/%\{(\w*)\}/, "{{\\1}}") if translation.is_a? String # TODO: translate can return a hash, eg. when interpolating %{count}
19
+ @javascript_translations[key] = translation
20
+ end
21
+
22
+ return translation
23
+ end
24
+
25
+ # TODO: implement localization?
26
+
27
+ def javascript_translations
28
+ @javascript_translations
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module Internashunalize
2
+ module Js
3
+ class Railtie < Rails::Railtie
4
+ initializer "internashunalize-js.view_helpers" do
5
+ ActionView::Base.send :include, Internashunalize::Js::ViewHelpers
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Internashunalize
2
+ module Js
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Internashunalize
2
+ module Js
3
+ module ViewHelpers
4
+ def internashunalize
5
+ content_tag(:div, nil, :id => 'internashunalize', :'data-internashunalize' => I18n.javascript_translations.to_json).html_safe
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ require "internashunalize-js/version"
2
+ require "internashunalize-js/i18n"
3
+ require "internashunalize-js/view_helpers"
4
+
5
+ if defined?(Rails)
6
+ require 'internashunalize-js/railtie'
7
+ end
@@ -0,0 +1,17 @@
1
+ class I18n
2
+
3
+ @t: (key, object=undefined) ->
4
+ @translate(key, object)
5
+
6
+ @translate: (key, object=undefined) ->
7
+ translation = @translations()[key]
8
+ if object?
9
+ return Mustache.to_html(translation, object)
10
+ else
11
+ return translation
12
+
13
+ @translations: ->
14
+ @translations_json ||= $.parseJSON($('body div#internashunalize').attr('data-internashunalize'))
15
+ return @translations_json
16
+
17
+ window.I18n = I18n
@@ -0,0 +1,30 @@
1
+ (function() {
2
+ var I18n;
3
+ I18n = (function() {
4
+ function I18n() {}
5
+ I18n.t = function(key, object) {
6
+ if (object == null) {
7
+ object = void 0;
8
+ }
9
+ return this.translate(key, object);
10
+ };
11
+ I18n.translate = function(key, object) {
12
+ var translation;
13
+ if (object == null) {
14
+ object = void 0;
15
+ }
16
+ translation = this.translations()[key];
17
+ if (object != null) {
18
+ return Mustache.to_html(translation, object);
19
+ } else {
20
+ return translation;
21
+ }
22
+ };
23
+ I18n.translations = function() {
24
+ this.translations_json || (this.translations_json = $.parseJSON($('body div#javascript_translations').attr('data-javascript_translations')));
25
+ return this.translations_json;
26
+ };
27
+ return I18n;
28
+ })();
29
+ window.I18n = I18n;
30
+ }).call(this);
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: internashunalize-js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Felder (masone)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &70327439186840 !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: *70327439186840
25
+ description: Uses the I18n library to provide translated strings to your Javascript.
26
+ email:
27
+ - ema@rh-productions.ch
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.textile
35
+ - Rakefile
36
+ - internashunalize-js.gemspec
37
+ - lib/generators/internashunalize/install_generator.rb
38
+ - lib/internashunalize-js.rb
39
+ - lib/internashunalize-js/i18n.rb
40
+ - lib/internashunalize-js/railtie.rb
41
+ - lib/internashunalize-js/version.rb
42
+ - lib/internashunalize-js/view_helpers.rb
43
+ - vendor/assets/javascripts/internashunalize.coffee
44
+ - vendor/assets/javascripts/internashunalize.js
45
+ homepage: http://masone.ch
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: internashunalize-js
65
+ rubygems_version: 1.8.16
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: I18n for your Javascript.
69
+ test_files: []