teamon-merb-simple-translation 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Tymon <teamon> Tobolski (http://teamon.eu)
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.
@@ -0,0 +1,63 @@
1
+ h2. merb-simple-translation
2
+
3
+ A plugin for the Merb framework that provides simple UI translation
4
+
5
+ h3. Installation
6
+
7
+
8
+ h4. Gem
9
+
10
+ @gem sources -a http://gems.github.com@
11
+ @sudo gem install teamon-merb-simple-translation@
12
+
13
+ h4. Configuration
14
+
15
+ _config/dependencies.rb_
16
+ <pre></code>dependency "teamon-merb-simple-translation", :require_as => "merb-simple-translation"
17
+ </code></pre>
18
+
19
+ _config/init.rb_
20
+ <pre></code>Merb::BootLoader.after_app_loads do
21
+ Translation.load
22
+ end</code></pre>
23
+
24
+ h3. Other options
25
+
26
+ h4. Reload translation files on every request
27
+
28
+ _config/environments/development.rb_
29
+ <pre><code>
30
+ Merb::Config.use { |c|
31
+ c[:translation_debug] = true
32
+ }
33
+ </code></pre>
34
+
35
+ h4. Change translations dir (default PROJECT_ROOT/translations) or default language
36
+
37
+ _config/init.rb_
38
+ <pre><code>Merb::BootLoader.after_app_loads do
39
+ Translation.translations_dir = Merb.root / 'i18n'
40
+ Translation.default_language = 'pl'
41
+ end</code></pre>
42
+
43
+ h3. Translation files
44
+
45
+ Plugin lets you write translations in two ways
46
+
47
+ h4. Ruby files
48
+
49
+ _translations/pl.rb_
50
+ <pre><code>Translation.add_translations('pl', {
51
+ "Log in" => "Zaloguj"
52
+ })</code></pre>
53
+
54
+ h4. YAML files
55
+
56
+ _translations/pl.yml_
57
+ <pre><code>pl:
58
+ Homepage: Strona domowa</code></pre>
59
+
60
+ h3. Copy default translation files to app directory
61
+ @rake merb-simple-translation:copy-defaults@
62
+
63
+
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+
7
+ GEM_NAME = "merb-simple-translation"
8
+ GEM_VERSION = "0.1.0"
9
+ AUTHOR = "Tymon <teamon> Tobolski"
10
+ EMAIL = "i@teamon.eu"
11
+ HOMEPAGE = "http://teamon.eu/"
12
+ SUMMARY = "Merb plugin that provides simple UI translation"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.rubyforge_project = 'merb'
16
+ s.name = GEM_NAME
17
+ s.version = GEM_VERSION
18
+ s.platform = Gem::Platform::RUBY
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ["README.textile", "LICENSE", 'TODO']
21
+ s.summary = SUMMARY
22
+ s.description = s.summary
23
+ s.author = AUTHOR
24
+ s.email = EMAIL
25
+ s.homepage = HOMEPAGE
26
+ s.add_dependency('merb', '>= 1.0')
27
+ s.require_path = 'lib'
28
+ s.files = %w(LICENSE README.textile Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
29
+
30
+ end
31
+
32
+ Rake::GemPackageTask.new(spec) do |pkg|
33
+ pkg.gem_spec = spec
34
+ end
35
+
36
+ desc "install the plugin as a gem"
37
+ task :install do
38
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
39
+ end
40
+
41
+ desc "Uninstall the gem"
42
+ task :uninstall do
43
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
44
+ end
45
+
46
+ desc "Create a gemspec file"
47
+ task :gemspec do
48
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
49
+ file.puts spec.to_ruby
50
+ end
51
+ end
data/TODO ADDED
@@ -0,0 +1 @@
1
+ TODO:
@@ -0,0 +1,20 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+
4
+ # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
5
+ Merb::Plugins.config[:merb_simple_translation] = {
6
+ :chickens => false
7
+ }
8
+
9
+ Merb::BootLoader.before_app_loads do
10
+ require "merb-simple-translation" / "translation.rb"
11
+ require "merb-simple-translation" / "string.rb"
12
+ require "merb-simple-translation" / "controller.rb"
13
+ end
14
+
15
+ Merb::BootLoader.after_app_loads do
16
+ # code that can be required after the application loads
17
+ end
18
+
19
+ Merb::Plugins.add_rakefiles "merb-simple-translation/merbtasks"
20
+ end
@@ -0,0 +1,16 @@
1
+ class Merb::Controller
2
+ before :set_language
3
+
4
+ def set_language
5
+ Translation.language = session['language'] || begin
6
+ request.env['HTTP_ACCEPT_LANGUAGE'].split(',')[0].split(';')[0].split('-')[0]
7
+ rescue
8
+ Translation.default_language
9
+ end
10
+
11
+ if Translation.debug?
12
+ Translation.reload
13
+ Translation.save_not_found
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ namespace "merb-simple-translation" do
2
+ desc "Copy default translation files do Translation.translations_dir"
3
+ task "copy-defaults" => :merb_env do
4
+ Dir.mkdir(Translation.translations_dir) unless File.exists?(Translation.translations_dir)
5
+ Dir[File.dirname(__FILE__) / :translations / '*.{rb,yml}'].each do |path|
6
+ FileUtils.cp(path, Translation.translations_dir / File.basename(path))
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def self.translate(value)
3
+ Translation.translate(value)
4
+ end
5
+ end
@@ -0,0 +1,67 @@
1
+ class Translation
2
+ @@languages = {}
3
+ @@debug_mark = "[T]%s[/T]"
4
+ @@default_language = 'en'
5
+ @@translations_dir = Merb.root / :translations
6
+ @@not_found = {}
7
+
8
+ cattr_accessor :debug_mark, :default_language, :translations_dir
9
+
10
+ class << self
11
+ def add_translations(lang, translations)
12
+ @@languages[lang] ||= {}
13
+ @@languages[lang].merge!(translations)
14
+ end
15
+
16
+ def language
17
+ Thread.current[:language] ||= default_language
18
+ end
19
+
20
+ def language=(lang)
21
+ Thread.current[:language] = @@languages[lang] ? lang : default_language
22
+ end
23
+
24
+ def self.languages
25
+ @@languages
26
+ end
27
+
28
+ def translate(phrase)
29
+ if dict[phrase]
30
+ dict[phrase]
31
+ elsif debug?
32
+ @@not_found[phrase] = ""
33
+ @@debug_mark % phrase
34
+ else
35
+ phrase
36
+ end
37
+ end
38
+
39
+ def save_not_found
40
+ File.open(translations_dir / 'not_found.yml', 'w') {|f| f.puts @@not_found.to_yaml }
41
+ end
42
+
43
+ def reload
44
+ @@languages = {}
45
+ load
46
+ end
47
+
48
+ def load
49
+ [File.dirname(__FILE__) / :translations, translations_dir].each do |dir|
50
+ Dir[dir / '*.rb'].each {|f| Kernel.load f}
51
+ Dir[dir / '*.yml'].reject{|e| e =~ /not_found.yml/}.each do |f|
52
+ YAML.load(File.read(f)).each_pair do |lang, translations|
53
+ add_translations(lang, translations)
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ def dict
60
+ @@languages[language] || @@languages[default_language] || {}
61
+ end
62
+
63
+ def debug?
64
+ Merb::Config[:translation_debug]
65
+ end
66
+ end
67
+ end
@@ -0,0 +1 @@
1
+ Translation.add_translations('pl', {})
@@ -0,0 +1,33 @@
1
+ pl:
2
+ %s is not accepted: %s nie jest zaakceptowane
3
+ %s does not match the confirmation: %s nie pasuje do potwierdzenia
4
+ %s is invalid: %s jest niepoprawne
5
+ %s has an invalid format: %s ma niepoprawny format
6
+ %s must be between %s and %s characters long: %s musi mieć od %s do %s znaków
7
+ %s must be more than %s characters long: %s musi mieć więcej niż %s znaków
8
+ %s must be less than %s characters long: %s musi mieć mniej niż %s znaków
9
+ %s must be %s characters long: %s musi mieć %s znaków
10
+ %s must be an integer: %s musi być liczbą całkowitą
11
+ %s must be a number: %s musi być liczbą
12
+ %s must not be %s: %s nie moze być %s
13
+
14
+ blank: puste
15
+ Oops! There are some errors: Wystąpiły błędy...
16
+ Login: Login
17
+ Password: Hasło
18
+ Password confirmation: Powtórz hasło
19
+ Create: Dodaj
20
+ Back: Powrót
21
+ Save: Zapisz
22
+ Log in: Zaloguj
23
+ Log out: Wyloguj
24
+ Sign in: Zarejestruj
25
+ Forgot password: Zapomniałem hasła
26
+ Not found: 404 Nie znaleziono
27
+ Name: Imię i nazwisko
28
+ Please login: Proszę się zalogować
29
+ Sign up: Zarejestruj się
30
+ Profile: Profil
31
+ Logged in as %s: Zalogowany jako %s
32
+ Email: Email
33
+ Your profile: Twój profil
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require "merb-core"
3
+ require "merb-simple-translation/translation.rb"
4
+ require "merb-simple-translation/string.rb"
5
+
6
+ describe "merb-simple-translation" do
7
+ before do
8
+ Translation.add_translations('pl', {
9
+ 'simple string' => 'prosty ciąg',
10
+ 'i like %s' => 'lubie %s',
11
+ 'apples' => 'jabłka'
12
+ })
13
+
14
+ Translation.language = 'pl'
15
+ end
16
+
17
+
18
+ it "should translate simple string" do
19
+ "simple string".t.should == "prosty ciąg"
20
+ end
21
+
22
+ it "should not translate missing phrase" do
23
+ "i do not exist!".t.should == "i do not exist!"
24
+ end
25
+
26
+ it "should mark untranslated phrase in debug mode" do
27
+ Merb::Config[:translation_debug] = true
28
+ "i do not exist!".t.should == "[T]i do not exist![/T]"
29
+ end
30
+
31
+ it "should allow to change debug marking string" do
32
+ Merb::Config[:translation_debug] = true
33
+ Translation.debug_mark = "{TRANS}%s{/TRANS}"
34
+ "i do not exist!".t.should == "{TRANS}i do not exist!{/TRANS}"
35
+ end
36
+
37
+ it "should translate like sprintf" do
38
+ "i like %s".t("apples").should == "lubie jabłka"
39
+ end
40
+ end
@@ -0,0 +1 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: teamon-merb-simple-translation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tymon <teamon> Tobolski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-10 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "1.0"
23
+ version:
24
+ description: Merb plugin that provides simple UI translation
25
+ email: i@teamon.eu
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.textile
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - LICENSE
36
+ - README.textile
37
+ - Rakefile
38
+ - TODO
39
+ - lib/merb-simple-translation
40
+ - lib/merb-simple-translation/controller.rb
41
+ - lib/merb-simple-translation/merbtasks.rb
42
+ - lib/merb-simple-translation/string.rb
43
+ - lib/merb-simple-translation/translation.rb
44
+ - lib/merb-simple-translation/translations
45
+ - lib/merb-simple-translation/translations/pl.rb
46
+ - lib/merb-simple-translation/translations/pl.yml
47
+ - lib/merb-simple-translation.rb
48
+ - spec/merb-simple-translation_spec.rb
49
+ - spec/spec_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://teamon.eu/
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: merb
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Merb plugin that provides simple UI translation
76
+ test_files: []
77
+