gettext_simple 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc52a6c77d98ee197e4ec2a6fb94f16f1d05b9ac
4
+ data.tar.gz: eabbfcbb9bf2c5508a669b87a8ec4d0c8d2a71c5
5
+ SHA512:
6
+ metadata.gz: 59b717d2a852942698df7333b32916928dfd750bbb640c7eabaae016042ad2a1dc7cfc2cdea3702d23dd99abb77b19bb65db9fd0a7684cfc462dbd0d44b3dc08
7
+ data.tar.gz: 1802f0f9558e44fac5e814948ab45e654b557b67fc46095aa88bfe5d552a3cd5e32b8ee58e36df5f00ac61dd260ace1ff9c359a8c8c772041bd4d377ba7bbea7
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0.1"
13
+ gem "simplecov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.6)
5
+ builder (3.2.2)
6
+ descendants_tracker (0.0.4)
7
+ thread_safe (~> 0.3, >= 0.3.1)
8
+ diff-lcs (1.1.3)
9
+ docile (1.1.3)
10
+ faraday (0.9.0)
11
+ multipart-post (>= 1.2, < 3)
12
+ git (1.2.6)
13
+ github_api (0.11.3)
14
+ addressable (~> 2.3)
15
+ descendants_tracker (~> 0.0.1)
16
+ faraday (~> 0.8, < 0.10)
17
+ hashie (>= 1.2)
18
+ multi_json (>= 1.7.5, < 2.0)
19
+ nokogiri (~> 1.6.0)
20
+ oauth2
21
+ hashie (2.1.0)
22
+ highline (1.6.21)
23
+ jeweler (2.0.1)
24
+ builder
25
+ bundler (>= 1.0)
26
+ git (>= 1.2.5)
27
+ github_api
28
+ highline (>= 1.6.15)
29
+ nokogiri (>= 1.5.10)
30
+ rake
31
+ rdoc
32
+ json (1.8.1)
33
+ jwt (0.1.11)
34
+ multi_json (>= 1.5)
35
+ mini_portile (0.5.3)
36
+ multi_json (1.9.2)
37
+ multi_xml (0.5.5)
38
+ multipart-post (2.0.0)
39
+ nokogiri (1.6.1)
40
+ mini_portile (~> 0.5.0)
41
+ oauth2 (0.9.3)
42
+ faraday (>= 0.8, < 0.10)
43
+ jwt (~> 0.1.8)
44
+ multi_json (~> 1.3)
45
+ multi_xml (~> 0.5)
46
+ rack (~> 1.2)
47
+ rack (1.5.2)
48
+ rake (10.2.2)
49
+ rdoc (3.12.2)
50
+ json (~> 1.4)
51
+ rspec (2.8.0)
52
+ rspec-core (~> 2.8.0)
53
+ rspec-expectations (~> 2.8.0)
54
+ rspec-mocks (~> 2.8.0)
55
+ rspec-core (2.8.0)
56
+ rspec-expectations (2.8.0)
57
+ diff-lcs (~> 1.1.2)
58
+ rspec-mocks (2.8.0)
59
+ simplecov (0.8.2)
60
+ docile (~> 1.1.0)
61
+ multi_json
62
+ simplecov-html (~> 0.8.0)
63
+ simplecov-html (0.8.0)
64
+ thread_safe (0.3.3)
65
+
66
+ PLATFORMS
67
+ ruby
68
+
69
+ DEPENDENCIES
70
+ bundler (~> 1.0)
71
+ jeweler (~> 2.0.1)
72
+ rdoc (~> 3.12)
73
+ rspec (~> 2.8.0)
74
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Kasper Johansen
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,19 @@
1
+ = gettext_simple
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to gettext_simple
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2014 Kasper Johansen. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "gettext_simple"
18
+ gem.homepage = "http://github.com/kaspernj/gettext_simple"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A very simple version of Gettext reading .po files directly.}
21
+ gem.description = %Q{A very simple version of Gettext reading .po files directly.}
22
+ gem.email = "k@spernj.org"
23
+ gem.authors = ["Kasper Johansen"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ desc "Code coverage detail"
35
+ task :simplecov do
36
+ ENV['COVERAGE'] = "true"
37
+ Rake::Task['spec'].execute
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rdoc/task'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "gettext_simple #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gettext_simple"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kasper Johansen"]
12
+ s.date = "2014-04-09"
13
+ s.description = "A very simple version of Gettext reading .po files directly."
14
+ s.email = "k@spernj.org"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "gettext_simple.gemspec",
29
+ "include/kernel_methods.rb",
30
+ "lib/gettext_simple.rb",
31
+ "spec/gettext_simple_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/kaspernj/gettext_simple"
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "2.0.7"
38
+ s.summary = "A very simple version of Gettext reading .po files directly."
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 4
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
45
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
48
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
51
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
54
+ s.add_dependency(%q<simplecov>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
58
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
61
+ s.add_dependency(%q<simplecov>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,5 @@
1
+ module ::Kernel
2
+ def _(str, replaces = nil)
3
+ return $gettext_simple_kernel_instance.translate(str, replaces)
4
+ end
5
+ end
@@ -0,0 +1,113 @@
1
+ #This class reads .po-files generated by something like POEdit and can be used to run multi-language applications or websites.
2
+ class GettextSimple
3
+ #Initializes various data.
4
+ def initialize(args = {})
5
+ @args = {
6
+ :encoding => "utf-8",
7
+ :i18n => false,
8
+ :default_locale => "en"
9
+ }.merge(args)
10
+ @locales = {}
11
+
12
+ # To avoid doing a hash lookup on every translate.
13
+ @i18n = @args[:i18n]
14
+
15
+ if @i18n
16
+ @default_locale = @i18n.default_locale
17
+ else
18
+ @default_locale = @args[:default_locale]
19
+ end
20
+
21
+ @locale = @default_locale
22
+ end
23
+
24
+ def register_kernel_methods
25
+ require "#{__dir__}/../include/kernel_methods"
26
+ $gettext_simple_kernel_instance = self
27
+ end
28
+
29
+ def locale_for_thread=(newlocale)
30
+ Thread.current[:gettext_simple_locale] = newlocale
31
+ end
32
+
33
+ #Loads a 'locales'-directory with .mo- and .po-files and fills the '@locales'-hash.
34
+ #===Examples
35
+ # gtext.load_dir("#{File.dirname(__FILE__)}/../locales")
36
+ def load_dir(dir)
37
+ check_folders = ["LC_MESSAGES", "LC_ALL"]
38
+
39
+ Dir.foreach(dir) do |file|
40
+ fn = "#{dir}/#{file}"
41
+ if File.directory?(fn) && file.match(/^[a-z]{2}/)
42
+ @locales[file] = {} unless @locales[file]
43
+
44
+ check_folders.each do |fname|
45
+ fpath = "#{dir}/#{file}/#{fname}"
46
+
47
+ if File.exists?(fpath) && File.directory?(fpath)
48
+ Dir.foreach(fpath) do |pofile|
49
+ if pofile.match(/\.po$/)
50
+ pofn = "#{dir}/#{file}/#{fname}/#{pofile}"
51
+ scan_pofile(file, pofn)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ #Translates a given string to a given locale from the read .po-files.
61
+ #===Examples
62
+ # str = "Hello" #=> "Hello"
63
+ # gtext.trans("da_DK", str) #=> "Hej"
64
+ def translate_with_locale(locale, str, replaces = nil)
65
+ locale = locale.to_s
66
+ str = str.to_s
67
+ raise "Locale was not found: '#{locale}' in '#{@locales.keys.join(", ")}'." unless @locales.key?(locale)
68
+
69
+ if !@locales[locale].key?(str)
70
+ translated_str = str
71
+ else
72
+ translated_str = @locales[locale][str]
73
+ end
74
+
75
+ if replaces
76
+ replaces.each do |key, val|
77
+ translated_str = translated_str.gsub("%{#{key}}", val)
78
+ end
79
+ end
80
+
81
+ return translated_str
82
+ end
83
+
84
+ def translate(str, replaces = nil)
85
+ if @i18n
86
+ locale = I18n.locale
87
+ elsif locale = Thread.current[:gettext_simple_locale]
88
+ # Locale already set through condition.
89
+ else
90
+ locale = @default_locale
91
+ end
92
+
93
+ translate_with_locale(locale, str, replaces)
94
+ end
95
+
96
+ # Returns an array of available locales.
97
+ def locales
98
+ return @locales.keys
99
+ end
100
+
101
+ private
102
+
103
+ def scan_pofile(locale, filepath)
104
+ cont = nil
105
+ File.open(filepath, "r", {:encoding => @args[:encoding]}) do |fp|
106
+ cont = fp.read.encode("utf-8")
107
+ end
108
+
109
+ cont.scan(/msgid\s+\"(.+)\"(\r|)\nmsgstr\s+\"(.+)\"(\r|)\n(\r|)\n/) do |match|
110
+ @locales[locale][match[0]] = match[2].to_s.encode("utf-8")
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "GettextSimple" do
4
+ before do
5
+ require "tmpdir"
6
+ @dir = Dir.mktmpdir
7
+ Dir.mkdir("#{@dir}/da")
8
+ Dir.mkdir("#{@dir}/da/LC_MESSAGES")
9
+
10
+ File.open("#{@dir}/da/LC_MESSAGES/default.po", "w") do |fp|
11
+ fp.puts "msgid \"test %{name}\"\n"
12
+ fp.puts "msgstr \"test translate %{name}\"\n\n"
13
+ end
14
+
15
+ @gs = GettextSimple.new
16
+ @gs.load_dir("#{@dir}")
17
+ end
18
+
19
+ it "can load a pofile and do some translations" do
20
+ @gs.instance_variable_get(:@locales).keys.should include "da"
21
+ @gs.instance_variable_get(:@locales)["da"].keys.should include "test %{name}"
22
+ @gs.instance_variable_get(:@locales)["da"]["test %{name}"].should eq "test translate %{name}"
23
+ @gs.translate_with_locale("da", "test %{name}", :name => "Trala").should eq "test translate Trala"
24
+ end
25
+
26
+ it "can register kernel method shortcuts" do
27
+ @gs.register_kernel_methods
28
+ @gs.locale_for_thread = "da"
29
+
30
+ _("test %{name}", :name => "Kasper").should eq "test translate Kasper"
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_adapter 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require 'rspec'
21
+ require 'gettext_simple'
22
+
23
+ # Requires supporting files with custom matchers and macros, etc,
24
+ # in ./support/ and its subdirectories.
25
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
26
+
27
+ RSpec.configure do |config|
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gettext_simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.8.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A very simple version of Gettext reading .po files directly.
84
+ email: k@spernj.org
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - .rspec
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - VERSION
99
+ - gettext_simple.gemspec
100
+ - include/kernel_methods.rb
101
+ - lib/gettext_simple.rb
102
+ - spec/gettext_simple_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: http://github.com/kaspernj/gettext_simple
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.0.7
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: A very simple version of Gettext reading .po files directly.
128
+ test_files: []