gettext_json 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Simon COURTOSI
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.markdown ADDED
@@ -0,0 +1,55 @@
1
+ # GettextJson
2
+
3
+ Rails plugin that make possible to use GetText ranslations in javascipt.
4
+
5
+ ## Installation
6
+
7
+ GettextJson can be installed as a gem :
8
+
9
+ gem install gettext_json
10
+
11
+ Or directly as a Rails plugin :
12
+
13
+ ./script/plugin install git://github.com/simonc/rails_gettext_json.git
14
+
15
+ ## Usage
16
+
17
+ ### Initialization
18
+
19
+ To install the GettextJson files in your application use the following generator :
20
+
21
+ ./script/generate gettext_json
22
+
23
+ It will create two files :
24
+
25
+ public/javascripts/gettext_json.js
26
+ lib/js_parser.rb
27
+
28
+ `gettext_json.js` is the GetText micro library for javascript. To use gettext_json you have to call this file from your layout.
29
+
30
+ `lib/js_parser.rb` is a Javascript gettext parser. To use it, require the file inn your rake tasks for gettext.
31
+
32
+ For more informations, see [GetText micro library](http://wiki.github.com/simonc/rails_gettext_json/gettext-micro-library)
33
+ and [Javascript parser](http://wiki.github.com/simonc/rails_gettext_json/javascript-parser) in the wiki.
34
+
35
+ ### Getting the translations
36
+
37
+ To create the translations file, use the following rake task :
38
+
39
+ rake makemo:json
40
+
41
+ It will generate the `public/javascripts/gettext_json_translations.js` file that contains the translated strings for all languages.
42
+ To make the GetText translations work, you need to call this file in your layout.
43
+
44
+ ### Use it in your scripts
45
+
46
+ To translate strings in your scripts, simply use the GetText micro library API :
47
+
48
+ GetText.locale = 'fr'; // To set the current locale (default is 'en')
49
+
50
+ var str = GetText.t('Hello World');
51
+ //=> "Bonjour le Monde"
52
+
53
+ ## Copyright
54
+
55
+ Copyright (c) 2009 Simon COURTOIS, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the gettext_json plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the gettext_json plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'GettextJson'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ PKG_FILES = FileList[
27
+ '[a-zA-Z]*',
28
+ 'generators/**/*',
29
+ 'lib/**/*',
30
+ 'rails/**/*',
31
+ 'tasks/**/*',
32
+ 'test/**/*'
33
+ ]
34
+
35
+ spec = Gem::Specification.new do |s|
36
+ s.name = "gettext_json"
37
+ s.version = "0.0.1"
38
+ s.author = "Simon COURTOIS"
39
+ s.email = "happynoff@free.fr"
40
+ s.homepage = "http://github.com/simonc/rails_gettext_json"
41
+ s.platform = Gem::Platform::RUBY
42
+ s.summary = "Rails plugin that make possible to use GetText ranslations in javascipt."
43
+ s.files = PKG_FILES.to_a
44
+ s.require_path = "lib"
45
+ s.has_rdoc = false
46
+ end
47
+
48
+ desc 'Turn this plugin into a gem.'
49
+ Rake::GemPackageTask.new(spec) do |pkg|
50
+ pkg.gem_spec = spec
51
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Create the GetText javascript library
3
+
4
+ Example:
5
+ ./script/generate gettext_json
6
+
7
+ This will create:
8
+ public/javascript/gettext_json.js
@@ -0,0 +1,33 @@
1
+ def colorize(text)
2
+ "\e[33m#{text}\e[0m"
3
+ end
4
+
5
+ class GettextJsonGenerator < Rails::Generator::Base
6
+ def manifest
7
+ record do |m|
8
+ m.directory 'public/javascripts'
9
+ m.template 'gettext_json.js', File.join('public/javascripts', 'gettext_json.js')
10
+ m.directory 'lib'
11
+ m.template 'js_parser.rb', File.join('lib', 'js_parser.rb')
12
+ end
13
+ end
14
+
15
+ def after_generate
16
+ puts <<-ALERT
17
+
18
+ ATTENTION !
19
+
20
+ In order to use the gettext_json library, add the following line in your layout :
21
+ #{colorize("javascript_include_tag 'gettext_json.js'")}
22
+
23
+ To add JavaScript files parser in your updatepo rake task, add this in the task code :
24
+ #{colorize("require 'js_parser'")}
25
+
26
+ To generate the translations json file use the following rake task :
27
+ #{colorize("rake makemo:json")}
28
+ And then add the following line in your layout :
29
+ #{colorize("javascript_include_tag 'gettext_json_translations.js'")}
30
+
31
+ ALERT
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ // The GetText main class
2
+ function GetTextJson() {
3
+ this.default_locale = 'en'; // The default locale to use
4
+ this.locale = null; // Can be set by the user
5
+ this.translations = null; // List of all translations
6
+
7
+ // Returns the locale currently in use.
8
+ // Returns default_locale if locale is null.
9
+ this.current_locale = function() {
10
+ if (typeof(this._current_locale) == 'undefined') {
11
+ this._current_locale = (this.locale || this.default_locale);
12
+ }
13
+ return this._current_locale;
14
+ };
15
+
16
+ // Returns the translation of a string if it exists.
17
+ // Otherwise the string itself is returned.
18
+ this.translate = function(msgid) {
19
+ var msgstr = msgid;
20
+ var locale = this.current_locale();
21
+
22
+ if (this.translations[locale] != undefined) {
23
+ if (this.translations[locale][msgid] != undefined) {
24
+ msgstr = this.translations[locale][msgid];
25
+ }
26
+ }
27
+ return msgstr;
28
+ };
29
+ // Aliasing translate() to t()
30
+ this.t = this.translate;
31
+ }
32
+
33
+ // Creating the GetText instance
34
+ var GetText = new GetTextJson();
@@ -0,0 +1,17 @@
1
+ require 'gettext_rails/tools'
2
+
3
+ module JsParser
4
+ module_function
5
+
6
+ def target?(file)
7
+ File.extname(file) == ".js"
8
+ end
9
+
10
+ def parse(file, ary = [])
11
+ js = File.read(file)
12
+ code = js.scan(/GetText.t(?:ranslate)? ?\(['"].*?['"]\)/).map { |s| s.gsub(/GetText.t(?:ranslate)? ?/, '_') }
13
+ GetText::RubyParser.parse_lines(file, code, ary)
14
+ end
15
+ end
16
+
17
+ GetText::RGetText.add_parser(JsParser)
data/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ begin
2
+ require File.join(File.dirname(__FILE__), 'lib', 'gettext_json')
3
+ rescue LoadError
4
+ begin
5
+ require 'gettext_json'
6
+ rescue LoadError => e
7
+ raise e unless defined?(Rake) && Rake.application.top_level_tasks.include?('gems:install')
8
+ end
9
+ end
data/install.rb ADDED
File without changes
File without changes
@@ -0,0 +1,22 @@
1
+ desc "Generate json file for gettext translations"
2
+ task :"makemo:json" do
3
+ translations = {}
4
+
5
+ Dir.glob("po/*/") do |folder|
6
+ lang = File.basename(folder)
7
+ translations[lang] = {}
8
+
9
+ Dir.glob(File.join(folder, '*.po')) do |file|
10
+ code = File.read(file)
11
+ msgs = code.scan(/^msgid "(.*?)"\nmsgstr "(.*?)"\n(?:\n|$)/m).each do |msg|
12
+ translations[lang][msg[0]] = msg[1].gsub(/["\n]/, '') unless msg[0].empty?
13
+ end
14
+ end
15
+ end
16
+
17
+ File.open('public/javascripts/gettext_json_translations.js', 'w') do |f|
18
+ f.write 'GetText.translations = '
19
+ f.write translations.to_json
20
+ f.write ";"
21
+ end
22
+ end
data/uninstall.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gettext_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon COURTOIS
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-21 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: happynoff@free.fr
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - init.rb
26
+ - install.rb
27
+ - MIT-LICENSE
28
+ - Rakefile
29
+ - README.markdown
30
+ - uninstall.rb
31
+ - generators/gettext_json/gettext_json_generator.rb
32
+ - generators/gettext_json/templates/gettext_json.js
33
+ - generators/gettext_json/templates/js_parser.rb
34
+ - generators/gettext_json/USAGE
35
+ - lib/gettext_json.rb
36
+ - tasks/gettext_json_tasks.rake
37
+ has_rdoc: true
38
+ homepage: http://github.com/simonc/rails_gettext_json
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Rails plugin that make possible to use GetText ranslations in javascipt.
65
+ test_files: []
66
+