Permalink
Browse files

Initial commity

  • Loading branch information...
0 parents commit b9e9d4a41272940d8ee093c64852ac87806cad15 @toretore committed Dec 4, 2008
Showing with 169 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +13 −0 README
  3. +23 −0 Rakefile
  4. +2 −0 init.rb
  5. +1 −0 install.rb
  6. +27 −0 lib/i18n_js.rb
  7. +67 −0 lib/javascripts/i18n.js
  8. +4 −0 tasks/i18n_js_tasks.rake
  9. +8 −0 test/i18n_js_test.rb
  10. +3 −0 test/test_helper.rb
  11. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
+I18n-js
+=======
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2008 [name of plugin creator], released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the i18n_js plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the i18n_js plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'I18n-js'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+require "i18n_js"
+I18nJs.generate
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,27 @@
+module I18nJs
+
+ JAVASCRIPT = File.read(File.join(File.dirname(__FILE__), 'javascripts', 'i18n.js'));
+
+ def self.generate
+ Lucy.generate("locales") do |g|
+ g.namespace = "I18n"
+ g.write :defaultLocale, default_locale
+ g.write :translations, translations
+ g.write_raw methods
+ end
+ end
+
+ def self.translations
+ I18n.backend.send(:init_translations) unless I18n.backend.send(:initialized?)
+ I18n.backend.send :translations
+ end
+
+ def self.default_locale
+ I18n.default_locale
+ end
+
+ def self.methods
+ JAVASCRIPT
+ end
+
+end
@@ -0,0 +1,67 @@
+(function(){
+
+ var interpolatePattern = /\{\{([^}]+)\}\}/g;
+
+ //Replace {{foo} with obj.foo
+ function interpolate(str, obj){
+ return str.replace(interpolatePattern, function(){
+ return obj[arguments[1]];
+ });
+ };
+
+ function pluralize(value, count){
+ return count == 1 ? value[0] : value[1];
+ };
+
+ //Split "foo.bar" to ["foo", "bar"] if key is a string
+ function keyToArray(key){
+ if (!key) return [];
+ if (typeof key != "string") return key;
+ return key.split('.');
+ };
+
+ function scope(){
+ return I18n.locale || I18n.defaultLocale;
+ };
+
+ //Works mostly the same as the Ruby equivalent
+ I18n.translate = function(key, opts){
+ if (typeof key != "string") { //Bulk lookup
+ var a = [], i;
+ for (i=0; i<key.length; i++) {
+ a.push(this.translate(key[i], opts));
+ }
+ return a;
+ } else {
+ opts = opts || {};
+ opts.default = opts.default || null;
+ key = keyToArray(opts.scope).concat(keyToArray(key));
+ var value = this.lookup(key, opts.default);
+ if (typeof value != "string" && value && value.length) value = pluralize(value, opts.count);
+ if (typeof value == "string") value = interpolate(value, opts);
+ return value;
+ }
+ };
+
+ I18n.t = I18n.translate;
+
+ I18n.lookup = function(keys, defaults){
+ var key, i, value = this.translations[scope()];
+ defaults = typeof defaults == "string" ? [defaults] : (defaults || []);
+ while (key = keys.shift()) {
+ value = value && value[key];
+ }
+ if (value){
+ return value;
+ } else {
+ if (defaults.length == 0) {
+ return null;
+ } else if (defaults[0].substr(0,1) == ':') {
+ return this.lookup([defaults[0].substr(1)], defaults.slice(1));
+ } else {
+ return defaults[0];
+ }
+ }
+ };
+
+})();
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :i18n_js do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class I18n-jsTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit b9e9d4a

Please sign in to comment.