javascript_i18n 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,7 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ generators/javascript_i18n/*.rb
4
+ generators/javascript_i18n/templates/javascripts/*.js
5
+ generators/javascript_i18n/templates/tasks/*.rake
6
+ generators/javascript_i18n/templates/initializers/*.rb
7
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.gem
7
+ *.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jakub Kuźma
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.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ = javascript_i18n
2
+
3
+ Dead simple JavaScript i18n tool. It contains JavaScript builder to
4
+ generate static files with translations in /public/javascripts/i18n
5
+ directory. Each language is stored in separate file, so if we have
6
+ e.g. 10 languages in our application, client needs to download only
7
+ the language he uses (10 times smaller than in Babilu). Because of
8
+ that fact we don't need to set any cookies to tell the client which
9
+ locale he uses. To use the gem put the following line into your
10
+ environment.rb:
11
+
12
+ config.gem "qoobaa-javascript_i18n", :lib => "javascript_i18n"
13
+
14
+ and run:
15
+
16
+ gem sources -a http://gems.github.com # (you only have to do this once)
17
+ sudo rake gems:install
18
+
19
+ After installing the gem, use the generator:
20
+
21
+ ruby script/generate javascript_i18n
22
+
23
+ That's it! If you want to build javascript files based on current
24
+ translation files use the rake task:
25
+
26
+ rake js:i18n:build
27
+
28
+ To include translations, put the code below in your
29
+ layout file (or somewhere in the view):
30
+
31
+ <%= javascript_include_tag "i18n/#{I18n.locale}" %>
32
+
33
+ If you often change the translations files, you may generate files on
34
+ every request in development mode, putting the code in your
35
+ /app/controllers/application_controller.rb:
36
+
37
+ before_filter :update_javascript_i18n
38
+
39
+ protected
40
+
41
+ def update_javascript_i18n
42
+ JavascriptI18n.update unless Rails.env.production?
43
+ end
44
+
45
+ You can use I18n.t function in your JavaScript files, it's similar to
46
+ ActiveSupport I18n.t:
47
+
48
+ I18n.t("hello");
49
+ // "Hello World!"
50
+ I18n.t("foo.bar")
51
+ // "baz"
52
+ I18n.t("non.existing")
53
+ // null
54
+ I18n.t("non.existing", { defaultValue: "foo" })
55
+ // "foo"
56
+ I18n.t("bar", { count: 1 })
57
+ // "foo"
58
+ I18n.t("bar", { count: 2 })
59
+ // "foos"
60
+
61
+ Current locale string can be retrieved using I18n.locale property:
62
+
63
+ I18n.locale
64
+ // "en"
65
+
66
+ == Copyright
67
+
68
+ Copyright (c) 2009 Jakub Kuźma. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "javascript_i18n"
10
+ gem.summary = %Q{Dead simple JavaScript i18n}
11
+ gem.email = "qoobaa@gmail.com"
12
+ gem.homepage = "http://github.com/qoobaa/javascript_i18n"
13
+ gem.authors = ["Jakub Kuźma"]
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION.yml')
47
+ config = YAML.load(File.read('VERSION.yml'))
48
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "javascript_i18n #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.2
@@ -0,0 +1,24 @@
1
+ require 'rbconfig'
2
+
3
+ class JavascriptI18nGenerator < Rails::Generator::Base
4
+ def manifest
5
+ record do |m|
6
+ m.directory "public/javascripts"
7
+ m.directory "public/javascripts/i18n"
8
+ m.file "javascripts/i18n.js", "public/javascripts/i18n/i18n.js"
9
+
10
+ m.directory "config"
11
+ m.directory "config/initializers"
12
+ m.file "initializers/javascript_i18n.rb", "config/initializers/javascript_i18n.rb"
13
+
14
+ m.directory "lib/tasks"
15
+ m.file "tasks/javascript_i18n.rake", "lib/tasks/javascript_i18n.rake"
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def banner
22
+ "Usage: #{$0} javascript_i18n"
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ JavascriptI18n.configuration do |config|
2
+ config.root_path = RAILS_ROOT
3
+ config.lib_dir = "public/javascripts/i18n"
4
+ config.output_dir = "public/javascripts/i18n"
5
+ config.output_style = :merged
6
+ end
@@ -0,0 +1,88 @@
1
+ // I18n is slightly modified version of babilu.js from Tore Darell
2
+
3
+ var I18n = I18n || (function() {
4
+ // Replace {{foo}} with obj.foo
5
+ function interpolate(string, object) {
6
+ return string.replace(/\{\{([^}]+)\}\}/g, function() {
7
+ return object[arguments[1]] || arguments[0];
8
+ });
9
+ };
10
+
11
+ // Split "foo.bar" to ["foo", "bar"] if key is a string
12
+ function keyToArray(key) {
13
+ if(!key) {
14
+ return [];
15
+ }
16
+ if(typeof key != "string") {
17
+ return key;
18
+ }
19
+ return key.split('.');
20
+ };
21
+
22
+ // Looks up a translation using an array of strings where the last
23
+ // is the key and any string before that define the scope. The
24
+ // current locale is always prepended and does not need to be
25
+ // provided. The second parameter is an array of strings used as
26
+ // defaults if the key can not be found. If a key starts with ":"
27
+ // it is used as a key for lookup. This method does not perform
28
+ // pluralization or interpolation.
29
+ function lookup(keys, defaults) {
30
+ var i = 0, value = I18n.translations;
31
+ defaults = (typeof defaults === "string") ? [defaults] : (defaults || []);
32
+ while(keys[i]) {
33
+ value = value && value[keys[i]];
34
+ i++;
35
+ }
36
+ if(value) {
37
+ return value;
38
+ } else {
39
+ if(defaults.length === 0) {
40
+ return null;
41
+ } else if (defaults[0].substr(0,1) === ':') {
42
+ return lookup(keys.slice(0, keys.length - 1).concat(keyToArray(defaults[0].substr(1))), defaults.slice(1));
43
+ } else {
44
+ return defaults[0];
45
+ }
46
+ }
47
+ };
48
+
49
+ // Returns other when 0 given
50
+ function pluralize(value, count) {
51
+ if(count === undefined) return value;
52
+ return count === 1 ? value.one : value.other;
53
+ };
54
+
55
+ // Works mostly the same as the Ruby equivalent, except there are
56
+ // no symbols in JavaScript, so keys are always strings. The only
57
+ // time this makes a difference is when differentiating between
58
+ // keys and values in the defaultValue option. Strings starting
59
+ // with ":" will be considered to be keys and used for lookup,
60
+ // while other strings are returned as-is.
61
+ function translate(key, options) {
62
+ if(typeof key != "string") {
63
+ // Bulk lookup
64
+ var a = [], i;
65
+ for(i = 0; i < key.length; i++) {
66
+ a.push(translate(key[i], options));
67
+ }
68
+ return a;
69
+ } else {
70
+ options = options || {};
71
+ options.defaultValue = options.defaultValue || null;
72
+ key = keyToArray(options.scope).concat(keyToArray(key));
73
+ var value = lookup(key, options.defaultValue);
74
+ if(typeof value !== "string" && value) {
75
+ value = pluralize(value, options.count);
76
+ }
77
+ if(typeof value === "string") {
78
+ value = interpolate(value, options);
79
+ }
80
+ return value;
81
+ }
82
+ }
83
+
84
+ return {
85
+ translate: translate,
86
+ t: translate
87
+ };
88
+ })();
@@ -0,0 +1,8 @@
1
+ namespace :js do
2
+ namespace :i18n do
3
+ desc "Build I18n JavaScript files using current translations"
4
+ task :build => :environment do
5
+ JavascriptI18n.update!
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require "javascript_i18n/configuration"
2
+ require "javascript_i18n/builder"
3
+ require "javascript_i18n/updater"
@@ -0,0 +1,26 @@
1
+ module JavascriptI18n
2
+ class Builder
3
+ attr_accessor :output_path, :lib_path, :output_style
4
+
5
+ def initialize(lib_path, output_path, output_style = :merged)
6
+ self.lib_path = lib_path
7
+ self.output_path = output_path
8
+ self.output_style = output_style
9
+ I18n.backend.send(:init_translations)
10
+ end
11
+
12
+ def run
13
+ lib = File.read(File.join(lib_path, "i18n.js")) if output_style == :merged
14
+ I18n.backend.send(:translations).each do |key, value|
15
+ File.open(File.join(output_path, "#{key}.js"), "w") do |file|
16
+ if output_style == :merged
17
+ file.puts(lib)
18
+ file.puts("\n")
19
+ end
20
+ file.puts("I18n.locale = I18n.locale || \"#{key}\";")
21
+ file.puts("I18n.translations = I18n.translations || #{value.to_json};")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'singleton'
2
+
3
+ module JavascriptI18n
4
+ class Configuration
5
+ include Singleton
6
+
7
+ ATTRIBUTES = [:root_path, :output_dir, :lib_dir, :output_style]
8
+
9
+ attr_accessor *ATTRIBUTES
10
+
11
+ def output_path
12
+ if root_path and output_dir
13
+ File.join(root_path, output_dir)
14
+ end
15
+ end
16
+
17
+ def lib_path
18
+ if root_path and lib_dir
19
+ File.join(root_path, lib_dir)
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.configuration
25
+ if block_given?
26
+ yield Configuration.instance
27
+ end
28
+ Configuration.instance
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ require 'singleton'
2
+
3
+ module JavascriptI18n
4
+ class Updater
5
+ include Singleton
6
+
7
+ attr_accessor :last_update_time
8
+
9
+ def initialize
10
+ # force first update
11
+ self.last_update_time = DateTime.parse("1970-01-01 00:00:00")
12
+ end
13
+
14
+ def most_recent_update_time
15
+ I18n.load_path.map { |translation_file| File.stat(translation_file).mtime }.max
16
+ end
17
+
18
+ def should_update?
19
+ most_recent_update_time > last_update_time
20
+ end
21
+ end
22
+
23
+ def self.update
24
+ update! if Updater.instance.should_update?
25
+ end
26
+
27
+ def self.update!
28
+ time = Updater.instance.most_recent_update_time
29
+ builder = Builder.new(configuration.lib_path, configuration.output_path, configuration.output_style)
30
+ builder.run
31
+ Updater.instance.last_update_time = time
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class JavascriptI18nTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'javascript_i18n'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: javascript_i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - "Jakub Ku\xC5\xBAma"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-20 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: qoobaa@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - generators/javascript_i18n/javascript_i18n_generator.rb
33
+ - generators/javascript_i18n/templates/initializers/javascript_i18n.rb
34
+ - generators/javascript_i18n/templates/javascripts/i18n.js
35
+ - generators/javascript_i18n/templates/tasks/javascript_i18n.rake
36
+ - lib/javascript_i18n.rb
37
+ - lib/javascript_i18n/builder.rb
38
+ - lib/javascript_i18n/configuration.rb
39
+ - lib/javascript_i18n/updater.rb
40
+ - test/javascript_i18n_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/qoobaa/javascript_i18n
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Dead simple JavaScript i18n
70
+ test_files:
71
+ - test/javascript_i18n_test.rb
72
+ - test/test_helper.rb