interpreter 0.0.3 → 0.0.4
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/app/controllers/interpreter/translations_controller.rb +36 -3
- data/app/models/interpreter_translation.rb +11 -0
- data/app/views/interpreter/shared/_css.html.haml +25 -0
- data/app/views/interpreter/translations/_errors.html.haml +8 -0
- data/app/views/interpreter/translations/_form.html.haml +9 -9
- data/app/views/interpreter/translations/index.html.haml +26 -14
- data/app/views/interpreter/translations/new.html.haml +4 -0
- data/app/views/interpreter/translations/show.html.haml +9 -0
- data/app/views/layouts/translations.html.haml +1 -0
- data/config/routes.rb +6 -1
- data/lib/interpreter.rb +8 -1
- data/lib/interpreter/base.rb +108 -0
- data/lib/interpreter/version.rb +1 -1
- data/test/dummy/config/initializers/interpreter.rb +2 -1
- data/test/dummy/config/routes.rb +1 -1
- data/test/dummy/features/manage_translations.feature +32 -3
- data/test/dummy/features/step_definitions/translation_steps.rb +8 -0
- data/test/dummy/features/support/paths.rb +0 -3
- metadata +8 -3
- data/lib/interpreter/translation.rb +0 -21
@@ -2,12 +2,45 @@ class Interpreter::TranslationsController < ApplicationController
|
|
2
2
|
layout "translations"
|
3
3
|
|
4
4
|
def index
|
5
|
-
@
|
5
|
+
@categories = InterpreterTranslation.categories
|
6
|
+
if params[:category]
|
7
|
+
@translations = InterpreterTranslation.find_like_key("en.#{params[:category]}.*")
|
8
|
+
else
|
9
|
+
@translations = InterpreterTranslation.all
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def search
|
14
|
+
@categories = InterpreterTranslation.categories
|
15
|
+
@translations = InterpreterTranslation.find_like_key params[:query]
|
16
|
+
@search_notice = "#{@translations.length} translation#{'s' if @translations.length > 1} found."
|
17
|
+
render :action => :index
|
18
|
+
end
|
19
|
+
|
20
|
+
def new
|
21
|
+
@translation = InterpreterTranslation.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def show
|
25
|
+
@key = params[:id].gsub('-','.')
|
26
|
+
@translations = InterpreterTranslation.find_all_by_key(@key)
|
6
27
|
end
|
7
28
|
|
8
29
|
def create
|
9
|
-
|
10
|
-
|
30
|
+
@translation = InterpreterTranslation.new
|
31
|
+
@translation.locale = params[:interpreter_translation][:locale]
|
32
|
+
@translation.key = params[:interpreter_translation][:key]
|
33
|
+
@translation.value = params[:interpreter_translation][:value]
|
34
|
+
if @translation.save
|
35
|
+
redirect_to interpreter_translations_url, :notice => "Translation added."
|
36
|
+
else
|
37
|
+
render :action => :new
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def destroy
|
42
|
+
count = InterpreterTranslation.destroy(params[:id].gsub('-','.'))
|
43
|
+
redirect_to :back, :notice => "#{count} translation#{'s' if count > 1} destroyed."
|
11
44
|
end
|
12
45
|
|
13
46
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
:css
|
2
|
+
div#translations .key{
|
3
|
+
float:left;
|
4
|
+
width:400px;
|
5
|
+
overflow:hidden;
|
6
|
+
padding-left:10px;
|
7
|
+
}
|
8
|
+
|
9
|
+
div#translations .value{
|
10
|
+
float:left;
|
11
|
+
overflow:hidden;
|
12
|
+
padding-left:10px;
|
13
|
+
}
|
14
|
+
|
15
|
+
div#translations .links{
|
16
|
+
float:right;
|
17
|
+
padding-bottom:10px;
|
18
|
+
}
|
19
|
+
div.translation{
|
20
|
+
overflow: auto;
|
21
|
+
width: 100%
|
22
|
+
}
|
23
|
+
div.translation:hover{
|
24
|
+
background:#eee;
|
25
|
+
}
|
@@ -1,16 +1,16 @@
|
|
1
|
-
=
|
2
|
-
|
1
|
+
= form_for @translation do |f|
|
2
|
+
= render :partial => 'errors', :locals => {:object => @translation}
|
3
3
|
.field
|
4
|
-
=
|
4
|
+
= f.label :locale
|
5
5
|
%br
|
6
|
-
=
|
6
|
+
= f.text_field :locale
|
7
7
|
.field
|
8
|
-
=
|
8
|
+
= f.label :key
|
9
9
|
%br
|
10
|
-
=
|
10
|
+
= f.text_field :key
|
11
11
|
.field
|
12
|
-
=
|
12
|
+
= f.label :value
|
13
13
|
%br
|
14
|
-
=
|
14
|
+
= f.text_field :value
|
15
15
|
.actions
|
16
|
-
=
|
16
|
+
= f.submit "Save"
|
@@ -1,16 +1,28 @@
|
|
1
|
-
%h1 Translations
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
.value= t.value
|
12
|
-
%br.clearfloat
|
1
|
+
%h1= link_to 'Translations', interpreter_translations_url
|
2
|
+
|
3
|
+
%p= @search_notice
|
4
|
+
|
5
|
+
#search_translations
|
6
|
+
= form_tag search_interpreter_translations_path do
|
7
|
+
.field
|
8
|
+
= text_field_tag :query
|
9
|
+
.actions
|
10
|
+
= submit_tag "Search"
|
13
11
|
|
12
|
+
%h3 Categories
|
13
|
+
#categories
|
14
|
+
- @categories.each do |c|
|
15
|
+
= link_to c.capitalize, category_interpreter_translations_path(c)
|
16
|
+
|
17
|
+
%p= link_to "New Translation", new_interpreter_translation_path
|
18
|
+
|
19
|
+
#translations
|
20
|
+
- @translations.each do |t|
|
21
|
+
.translation{:id => t.id}
|
22
|
+
.key= t.id
|
23
|
+
.value= t.value
|
24
|
+
.links
|
25
|
+
= link_to 'Show', interpreter_translation_path(t.key.gsub('.','-'))
|
26
|
+
= link_to 'Remove', interpreter_translation_path(t.key.gsub('.','-')), :method => :delete
|
14
27
|
|
15
|
-
%
|
16
|
-
= render :partial => "form"
|
28
|
+
%p= link_to "New Translation", new_interpreter_translation_path
|
data/config/routes.rb
CHANGED
data/lib/interpreter.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module Interpreter
|
2
|
-
autoload :
|
2
|
+
autoload :Base, "interpreter/base"
|
3
3
|
|
4
4
|
mattr_reader :backend
|
5
5
|
@@backend = nil
|
6
6
|
|
7
|
+
mattr_reader :locales
|
8
|
+
@@locales = I18n.backend.available_locales
|
9
|
+
|
7
10
|
class Engine < Rails::Engine
|
8
11
|
end
|
9
12
|
|
@@ -15,4 +18,8 @@ module Interpreter
|
|
15
18
|
@@backend = backend
|
16
19
|
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(@@backend), I18n.backend)
|
17
20
|
end
|
21
|
+
|
22
|
+
def self.locales=(locales = [])
|
23
|
+
@@locales = locales
|
24
|
+
end
|
18
25
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
class Interpreter::Base
|
2
|
+
include ActiveModel::Conversion
|
3
|
+
extend ActiveModel::Naming
|
4
|
+
extend ActiveModel::Translation
|
5
|
+
include ActiveModel::Validations
|
6
|
+
include ActiveModel::AttributeMethods
|
7
|
+
|
8
|
+
class_attribute :_attributes
|
9
|
+
self._attributes = []
|
10
|
+
|
11
|
+
attribute_method_suffix '?'
|
12
|
+
|
13
|
+
def self.attributes(*names)
|
14
|
+
attr_accessor *names
|
15
|
+
define_attribute_methods names
|
16
|
+
self._attributes += names
|
17
|
+
end
|
18
|
+
|
19
|
+
def attributes
|
20
|
+
self._attributes.inject({}) do |hash, attr|
|
21
|
+
hash[attr.to_s] = send(attr)
|
22
|
+
hash
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def persisted?
|
27
|
+
if id?
|
28
|
+
Interpreter.backend.get(id).nil? ? false : true
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def id
|
35
|
+
locale + '.' + key
|
36
|
+
end
|
37
|
+
|
38
|
+
def id?
|
39
|
+
locale? and key?
|
40
|
+
end
|
41
|
+
|
42
|
+
def save
|
43
|
+
if self.valid?
|
44
|
+
Interpreter.backend.set("#{locale}.#{key}", value)
|
45
|
+
else
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def ==(other)
|
51
|
+
self.locale == other.locale and self.key == other.key and self.value == other.value
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.all
|
55
|
+
collection = []
|
56
|
+
Interpreter.backend.keys.each do |k|
|
57
|
+
obj = find_by_key(k)
|
58
|
+
if obj and obj.valid?# and obj.child_keys.empty?
|
59
|
+
collection << obj
|
60
|
+
end
|
61
|
+
end
|
62
|
+
collection.sort{|a, b| a.key <=> b.key}
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.find_all_by_key key
|
66
|
+
result = {}
|
67
|
+
Interpreter.backend.keys("*.#{key}").map{|k| find_by_key(k)}.compact.each do |t|
|
68
|
+
result[t.locale] = t.value
|
69
|
+
end
|
70
|
+
return result
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.find_by_key full_key
|
74
|
+
locale = full_key.split('.')[0]
|
75
|
+
key = full_key.gsub("#{locale}.",'')
|
76
|
+
obj = self.new
|
77
|
+
obj.locale = locale
|
78
|
+
obj.key = key
|
79
|
+
obj.value = Interpreter.backend.get(full_key)
|
80
|
+
obj.valid? ? obj : nil
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.find_like_key str
|
84
|
+
Interpreter.backend.keys("*#{str}*").map{|k| find_by_key(k)}.compact.sort{|a, b| a.locale <=> b.locale}
|
85
|
+
end
|
86
|
+
|
87
|
+
def child_keys
|
88
|
+
Interpreter.backend.keys("#{id}.*")
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.destroy key
|
92
|
+
Interpreter.backend.keys("*.#{key}").sum{|k| Interpreter.backend.del(k)}
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.categories
|
96
|
+
Interpreter.backend.keys.map{|k| k.split('.')[1]}.uniq
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.available_locales
|
100
|
+
Interpreter.locales
|
101
|
+
end
|
102
|
+
|
103
|
+
protected
|
104
|
+
|
105
|
+
def attribute?(attribute)
|
106
|
+
send(attribute).present?
|
107
|
+
end
|
108
|
+
end
|
data/lib/interpreter/version.rb
CHANGED
data/test/dummy/config/routes.rb
CHANGED
@@ -48,7 +48,7 @@ Dummy::Application.routes.draw do
|
|
48
48
|
|
49
49
|
# You can have the root of your site routed with "root"
|
50
50
|
# just remember to delete public/index.html.
|
51
|
-
|
51
|
+
root :to => "interpreter/translations#index"
|
52
52
|
|
53
53
|
# See how all your routes lay out with "rake routes"
|
54
54
|
|
@@ -3,12 +3,41 @@ Feature: Manage translations
|
|
3
3
|
As an user
|
4
4
|
I want to view, add and edit translations
|
5
5
|
|
6
|
-
Scenario:
|
7
|
-
Given I am on
|
6
|
+
Scenario: Add new translation
|
7
|
+
Given I am on the new interpreter translation page
|
8
8
|
When I fill in "Locale" with "locale 1"
|
9
9
|
And I fill in "Key" with "key 1"
|
10
10
|
And I fill in "Value" with "value 1"
|
11
11
|
And I press "Save"
|
12
|
-
Then I should see "
|
12
|
+
Then I should see "Translation added."
|
13
|
+
And I should see "locale 1"
|
13
14
|
And I should see "key 1"
|
14
15
|
And I should see "value 1"
|
16
|
+
|
17
|
+
Scenario: Edit translation
|
18
|
+
Given a translation is present with key: hello, value: hello and locale: en
|
19
|
+
When I go to the new interpreter translation page
|
20
|
+
And I fill in "Key" with "hello"
|
21
|
+
And I fill in "Value" with "hello again"
|
22
|
+
And I fill in "Locale" with "en"
|
23
|
+
And I press "Save"
|
24
|
+
Then I should see "hello"
|
25
|
+
And I should see "hello again"
|
26
|
+
|
27
|
+
Scenario: Remove translation
|
28
|
+
Given a translation is present with key: hello, value: hello and locale: en
|
29
|
+
When I go to the interpreter translations page
|
30
|
+
Then I should see "hello"
|
31
|
+
And I should see "Remove"
|
32
|
+
When I follow "Remove"
|
33
|
+
Then I should not see "hello"
|
34
|
+
And I should see "1 translation destroyed."
|
35
|
+
And I should be on the interpreter translations page
|
36
|
+
|
37
|
+
Scenario: Search translations
|
38
|
+
Given a translation is present with key: hello, value: hello and locale: en
|
39
|
+
When I go to the interpreter translations page
|
40
|
+
And I fill in "query" with "hello" within "#search_translations"
|
41
|
+
And I press "Search"
|
42
|
+
Then I should see "hello" within "#translations"
|
43
|
+
And I should see "1 translation found."
|
@@ -12,3 +12,11 @@ end
|
|
12
12
|
Then /^I should see the following translations:$/ do |expected_translations_table|
|
13
13
|
expected_translations_table.diff!(tableish('table tr', 'td,th'))
|
14
14
|
end
|
15
|
+
|
16
|
+
Given /^a translation is present with key: (.+), value: (.+) and locale: (.+)$/ do |key, value, locale|
|
17
|
+
i = InterpreterTranslation.new
|
18
|
+
i.locale = locale
|
19
|
+
i.key = key
|
20
|
+
i.value = value
|
21
|
+
i.save
|
22
|
+
end
|
@@ -25,9 +25,6 @@ module NavigationHelpers
|
|
25
25
|
when /^#{capture_model}(?:'s)? (.+?) page$/ # eg. the forum's posts page
|
26
26
|
polymorphic_path(model($1), :action => $2) # or the forum's edit page
|
27
27
|
|
28
|
-
when /translations page/
|
29
|
-
interpreter_translations_path
|
30
|
-
|
31
28
|
# Add more mappings here.
|
32
29
|
# Here is an example that pulls values out of the Regexp:
|
33
30
|
#
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: interpreter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jagdeep Singh
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-17 00:00:00 +05:30
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -52,13 +52,18 @@ files:
|
|
52
52
|
- README.rdoc
|
53
53
|
- Rakefile
|
54
54
|
- app/controllers/interpreter/translations_controller.rb
|
55
|
+
- app/models/interpreter_translation.rb
|
56
|
+
- app/views/interpreter/shared/_css.html.haml
|
57
|
+
- app/views/interpreter/translations/_errors.html.haml
|
55
58
|
- app/views/interpreter/translations/_form.html.haml
|
56
59
|
- app/views/interpreter/translations/index.html.haml
|
60
|
+
- app/views/interpreter/translations/new.html.haml
|
61
|
+
- app/views/interpreter/translations/show.html.haml
|
57
62
|
- app/views/layouts/translations.html.haml
|
58
63
|
- config/routes.rb
|
59
64
|
- interpreter.gemspec
|
60
65
|
- lib/interpreter.rb
|
61
|
-
- lib/interpreter/
|
66
|
+
- lib/interpreter/base.rb
|
62
67
|
- lib/interpreter/version.rb
|
63
68
|
- test/dummy/.gitignore
|
64
69
|
- test/dummy/Gemfile
|
@@ -1,21 +0,0 @@
|
|
1
|
-
class Interpreter::Translation
|
2
|
-
attr_reader :locale, :key, :value
|
3
|
-
|
4
|
-
def initialize locale, key, value
|
5
|
-
@locale = locale
|
6
|
-
@key = key
|
7
|
-
@value = value
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.all
|
11
|
-
collection = []
|
12
|
-
Interpreter.backend.keys.each do |key|
|
13
|
-
collection << self.new(key.split('.')[0], key, Interpreter.backend[key])
|
14
|
-
end
|
15
|
-
return collection.sort{|a, b| a.key <=> b.key}
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.create locale, key, value
|
19
|
-
I18n.backend.store_translations(locale, { key => value }, :escape => false)
|
20
|
-
end
|
21
|
-
end
|