whowish_word 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -50,7 +50,7 @@ Now when you want to edit wording:
50
50
 
51
51
  1. Activate edit mode by calling:
52
52
  ```
53
- activate_whowish_word_edit_mode()
53
+ whowish_word.activate_edit_mode
54
54
  ```
55
55
  in any controller.
56
56
  (The usual way is to use user's session to determine whether or not to activate WhowishWord's edit mode)
@@ -86,10 +86,29 @@ WhowishWord renders it as:
86
86
  You are here 10 times already
87
87
  ```
88
88
 
89
- Feature to be added
90
- ---------------------
89
+ Multi-language support
90
+ -----------------------
91
+
92
+ You can change locale of WhowishWord by:
93
+
94
+ ```ruby
95
+ whowish_word.set_locale("jp")
96
+ ```
91
97
 
92
- - Multi-language support
98
+ In Rails, you should add before_filter in the application controller as shown below:
99
+
100
+ ```ruby
101
+ before_filter :set_locale
102
+
103
+ def set_locale
104
+ if params[:locale]
105
+ session[:locale] = params[:locale]
106
+ end
107
+
108
+ session[:locale] ||= "en"
109
+ whowish_word.set_locale(session[:locale])
110
+ end
111
+ ```
93
112
 
94
113
 
95
114
  Prerequisite
@@ -2,30 +2,40 @@
2
2
  if defined?(ActionController) and defined?(ActionController::Base)
3
3
 
4
4
  class ActionController::Base
5
+
6
+ prepend_before_filter :initialize_whowish_word
7
+ attr_accessor :whowish_word_config
8
+
9
+
10
+ def initialize_whowish_word
11
+ @whowish_word_config = WhowishWord::Config.new
12
+ end
5
13
 
6
- helper_method :activate_whowish_word_edit_mode
7
- attr_accessor :whowish_word_edit_mode
14
+
15
+ helper_method :whowish_word
8
16
 
9
- def activate_whowish_word_edit_mode
10
- @whowish_word_edit_mode = true
17
+ def whowish_word
18
+ return @whowish_word_config
11
19
  end
12
20
 
21
+
13
22
  def word_for(namespace, id, *variables)
14
23
 
15
- if @whowish_word_edit_mode == true
16
- return WhowishWord.word_for_in_edit_mode(namespace, id, *variables)
24
+ if @whowish_word_config.edit_mode == true
25
+ return WhowishWord.word_for_in_edit_mode(namespace, id, @whowish_word_config.locale, *variables)
17
26
  else
18
- return WhowishWord.word_for(namespace, id, *variables)
27
+ return WhowishWord.word_for(namespace, id, @whowish_word_config.locale, *variables)
19
28
  end
20
29
 
21
30
  end
22
31
 
32
+
23
33
  def word_for_attr(namespace, id, *variables)
24
34
 
25
- if @whowish_word_edit_mode == true
26
- return WhowishWord.word_for_attr_in_edit_mode(namespace, id, *variables)
35
+ if @whowish_word_config.edit_mode == true
36
+ return WhowishWord.word_for_attr_in_edit_mode(namespace, id, @whowish_word_config.locale, *variables)
27
37
  else
28
- return WhowishWord.word_for_attr(namespace, id, *variables)
38
+ return WhowishWord.word_for_attr(namespace, id, @whowish_word_config.locale, *variables)
29
39
  end
30
40
 
31
41
  end
@@ -2,10 +2,11 @@
2
2
  if defined?(ActionView) and defined?(ActionView::Base)
3
3
 
4
4
  class ActionView::Base
5
-
5
+
6
+
6
7
  def whowish_word_javascript_and_css(force = false)
7
8
 
8
- return "" if @whowish_word_edit_mode != true and force == false
9
+ return "" if @whowish_word_config.edit_mode != true and force == false
9
10
 
10
11
  script_text = <<-HTML
11
12
  <script type="text/javascript">
@@ -20,40 +21,45 @@ if defined?(ActionView) and defined?(ActionView::Base)
20
21
  script_text.html_safe
21
22
  end
22
23
 
24
+
23
25
  def global_word_for(namespace, id, *variables)
24
26
 
25
- if @whowish_word_edit_mode == true
26
- return WhowishWord.word_for_in_edit_mode(namespace, id, *variables)
27
+ if @whowish_word_config.edit_mode == true
28
+ return WhowishWord.word_for_in_edit_mode(namespace, id, @whowish_word_config.locale, *variables)
27
29
  else
28
- return WhowishWord.word_for(namespace, id, *variables)
30
+ return WhowishWord.word_for(namespace, id, @whowish_word_config.locale, *variables)
29
31
  end
30
32
 
31
33
  end
32
-
34
+
35
+
33
36
  def global_word_for_attr(namespace, id, *variables)
34
37
 
35
- if @whowish_word_edit_mode == true
36
- return WhowishWord.word_for_attr_in_edit_mode(namespace, id, *variables)
38
+ if @whowish_word_config.edit_mode == true
39
+ return WhowishWord.word_for_attr_in_edit_mode(namespace, id, @whowish_word_config.locale, *variables)
37
40
  else
38
- return WhowishWord.word_for_attr(namespace, id, *variables)
41
+ return WhowishWord.word_for_attr(namespace, id, @whowish_word_config.locale, *variables)
39
42
  end
40
43
 
41
44
  end
42
-
45
+
46
+
43
47
  def word_for(id, *variables)
44
48
 
45
49
  namespace = get_relative_view_path(@whowish_word_page)
46
50
  global_word_for(namespace, id, *variables)
47
51
 
48
52
  end
49
-
53
+
54
+
50
55
  def word_for_attr(id, *variables)
51
56
 
52
57
  namespace = get_relative_view_path(@whowish_word_page)
53
58
  global_word_for_attr(namespace, id, *variables)
54
59
 
55
60
  end
56
-
61
+
62
+
57
63
  private
58
64
  def get_relative_view_path(full_path)
59
65
  result = @whowish_word_page.match(/[\/\\](([^\/\\]+)[\/\\]([^\/\\]+))\Z/)
@@ -0,0 +1,22 @@
1
+ module WhowishWord
2
+
3
+ class Config
4
+
5
+ attr_accessor :locale, :edit_mode
6
+
7
+ def initialize
8
+ @locale = "en"
9
+ @edit_mode = false
10
+ end
11
+
12
+ def activate_edit_mode
13
+ @edit_mode = true
14
+ end
15
+
16
+ def set_locale(locale)
17
+ @locale = locale
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -1,3 +1,4 @@
1
+ require File.expand_path("../config", __FILE__)
1
2
  require File.expand_path("../constant", __FILE__)
2
3
  require File.expand_path("../initializer", __FILE__)
3
4
  require File.expand_path("../word_for", __FILE__)
@@ -5,29 +5,27 @@ module WhowishWord
5
5
  module WordFor
6
6
  include WhowishWord::Constant
7
7
 
8
- def word_for(namespace, id, *variables)
9
- return word_for_normal_mode(namespace, id, *variables)
8
+ def word_for(namespace, id, locale, *variables)
9
+ return word_for_normal_mode(namespace, id, locale, *variables)
10
10
  end
11
11
 
12
- def word_for_attr(namespace, id, *variables)
13
- return word_for_normal_mode(namespace, id, *variables)
12
+ def word_for_attr(namespace, id, locale, *variables)
13
+ return word_for_normal_mode(namespace, id, locale, *variables)
14
14
  end
15
15
 
16
- def word_for_in_edit_mode(namespace, id, *variables)
17
- return "<dfn>#{word_for_edit_mode(namespace, id, *variables)}</dfn>".html_safe
16
+ def word_for_in_edit_mode(namespace, id, locale, *variables)
17
+ return "<dfn>#{word_for_edit_mode(namespace, id, locale, *variables)}</dfn>".html_safe
18
18
  end
19
19
 
20
- def word_for_attr_in_edit_mode(namespace, id, *variables)
21
- return word_for_edit_mode(namespace, id, *variables)
20
+ def word_for_attr_in_edit_mode(namespace, id, locale, *variables)
21
+ return word_for_edit_mode(namespace, id, locale, *variables)
22
22
  end
23
23
 
24
24
 
25
- def word_for_edit_mode(namespace, id, *variables)
25
+ def word_for_edit_mode(namespace, id, locale, *variables)
26
26
 
27
27
  variables = sanitize_variables_arg(variables)
28
28
 
29
- locale = "en"
30
-
31
29
  variable_suffix = ""
32
30
  if variables.length > 0
33
31
 
@@ -43,7 +41,7 @@ module WhowishWord
43
41
  get_whowish_word_id(namespace, id, locale) + \
44
42
  variable_suffix + \
45
43
  SEPARATOR + \
46
- word_for_normal_mode(namespace, id, variables)
44
+ word_for_normal_mode(namespace, id, locale, variables)
47
45
 
48
46
  end
49
47
 
@@ -59,11 +57,10 @@ module WhowishWord
59
57
 
60
58
  end
61
59
 
62
- def word_for_normal_mode(namespace, id, *variables)
60
+ def word_for_normal_mode(namespace, id, locale, *variables)
63
61
 
64
62
  variables = sanitize_variables_arg(variables)
65
63
 
66
- locale = "en"
67
64
  word_id = get_whowish_word_id(namespace, id, locale)
68
65
 
69
66
  if @words[word_id]
@@ -2,14 +2,21 @@ class ApplicationController < ActionController::Base
2
2
 
3
3
  layout "main"
4
4
 
5
- before_filter :activate_whowish_word
5
+ before_filter :activate_whowish_word, :set_locale
6
6
 
7
7
  def activate_whowish_word
8
8
  if params[:edit_mode] == "yes"
9
- activate_whowish_word_edit_mode
9
+ whowish_word.activate_edit_mode
10
+ end
11
+ end
12
+
13
+ def set_locale
14
+ if params[:locale]
15
+ session[:locale] = params[:locale]
10
16
  end
11
17
 
12
-
18
+ session[:locale] ||= "en"
19
+ whowish_word.set_locale(session[:locale])
13
20
  end
14
21
 
15
22
 
@@ -1,3 +1,6 @@
1
+ <a href="/home?locale=en">en</a><br/>
2
+ <a href="/home?locale=th">th</a><br/>
3
+ <a href="/home?locale=jp">jp</a>
1
4
  <form autocomplete="off" onsubmit="return false;">
2
5
  <div>
3
6
  <span style="width:600px;">
@@ -5,7 +8,7 @@
5
8
  </span>
6
9
  <span>
7
10
  <%
8
- if @whowish_word_edit_mode == true
11
+ if @whowish_word_config.edit_mode == true
9
12
  %>
10
13
  <a href="/home">Dectivate edit mode</a>
11
14
  <%
@@ -15,7 +15,7 @@ module WhowishWordRspecHelper
15
15
  variables_clause = ""
16
16
  variables_clause = "{#{var_keys.join(',')}}" if var_keys.length > 0
17
17
 
18
- return "#{namespace}:#{id}(en)#{variables_clause}"
18
+ return "#{id}(en)#{variables_clause}"
19
19
 
20
20
  end
21
21
 
@@ -42,7 +42,7 @@ module WhowishWordRspecHelper
42
42
  SEPARATOR + \
43
43
  "#{namespace}:#{id}(en)#{variables_params}" + \
44
44
  SEPARATOR + \
45
- "#{namespace}:#{id}(en)#{variables_clause}"
45
+ "#{id}(en)#{variables_clause}"
46
46
 
47
47
  end
48
48
 
@@ -9,16 +9,16 @@ describe 'word_for' do
9
9
 
10
10
  it "no variables, no entry" do
11
11
 
12
- value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id")
13
- value.should == "#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)#{WhowishWord::SEPARATOR}namespace:id(en)"
12
+ value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id", "en")
13
+ value.should == "#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)#{WhowishWord::SEPARATOR}id(en)"
14
14
 
15
15
  end
16
16
 
17
17
 
18
18
  it "with variables, no entry" do
19
19
 
20
- value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id", :number=>5, :name=>"tanin")
21
- value.should == "#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)|number,name#{WhowishWord::SEPARATOR}namespace:id(en){number,name}"
20
+ value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id", "en", :number=>5, :name=>"tanin")
21
+ value.should == "#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)|number,name#{WhowishWord::SEPARATOR}id(en){number,name}"
22
22
 
23
23
  end
24
24
 
@@ -26,7 +26,7 @@ describe 'word_for' do
26
26
  it "no variables, with entry" do
27
27
 
28
28
  WhowishWord.words = {"namespace:id(en)" => "hello"}
29
- value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id")
29
+ value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id", "en")
30
30
  value.should == "#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)#{WhowishWord::SEPARATOR}hello"
31
31
 
32
32
  end
@@ -35,7 +35,7 @@ describe 'word_for' do
35
35
  it "with variables, with entry" do
36
36
 
37
37
  WhowishWord.words = {"namespace:id(en)" => "hello {name} for {number} times"}
38
- value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id", :number=>5, :name=>"tanin")
38
+ value = WhowishWord.word_for_attr_in_edit_mode("namespace", "id", "en", :number=>5, :name=>"tanin")
39
39
  value.should == "#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)|number,name#{WhowishWord::SEPARATOR}hello tanin for 5 times"
40
40
 
41
41
  end
@@ -9,16 +9,16 @@ describe 'word_for_attr' do
9
9
 
10
10
  it "no variables, no entry" do
11
11
 
12
- value = WhowishWord.word_for_attr("namespace", "id")
13
- value.should == "namespace:id(en)"
12
+ value = WhowishWord.word_for_attr("namespace", "id", "en")
13
+ value.should == "id(en)"
14
14
 
15
15
  end
16
16
 
17
17
 
18
18
  it "with variables, no entry" do
19
19
 
20
- value = WhowishWord.word_for_attr("namespace", "id", :number=>5, :name=>"tanin")
21
- value.should == "namespace:id(en){number,name}"
20
+ value = WhowishWord.word_for_attr("namespace", "id", "en", :number=>5, :name=>"tanin")
21
+ value.should == "id(en){number,name}"
22
22
 
23
23
  end
24
24
 
@@ -26,7 +26,7 @@ describe 'word_for_attr' do
26
26
  it "no variables, with entry" do
27
27
 
28
28
  WhowishWord.words = {"namespace:id(en)" => "hello"}
29
- value = WhowishWord.word_for_attr("namespace", "id")
29
+ value = WhowishWord.word_for_attr("namespace", "id", "en")
30
30
  value.should == "hello"
31
31
 
32
32
  end
@@ -35,7 +35,7 @@ describe 'word_for_attr' do
35
35
  it "with variables, with entry" do
36
36
 
37
37
  WhowishWord.words = {"namespace:id(en)" => "hello {name} for {number} times"}
38
- value = WhowishWord.word_for_attr("namespace", "id", :number=>5, :name=>"tanin")
38
+ value = WhowishWord.word_for_attr("namespace", "id", "en", :number=>5, :name=>"tanin")
39
39
  value.should == "hello tanin for 5 times"
40
40
 
41
41
  end
@@ -9,16 +9,16 @@ describe 'word_for' do
9
9
 
10
10
  it "no variables, no entry" do
11
11
 
12
- value = WhowishWord.word_for_in_edit_mode("namespace", "id")
13
- value.should == "<dfn>#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)#{WhowishWord::SEPARATOR}namespace:id(en)</dfn>"
12
+ value = WhowishWord.word_for_in_edit_mode("namespace", "id", "en")
13
+ value.should == "<dfn>#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)#{WhowishWord::SEPARATOR}id(en)</dfn>"
14
14
 
15
15
  end
16
16
 
17
17
 
18
18
  it "with variables, no entry" do
19
19
 
20
- value = WhowishWord.word_for_in_edit_mode("namespace", "id", :number=>5, :name=>"tanin")
21
- value.should == "<dfn>#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)|number,name#{WhowishWord::SEPARATOR}namespace:id(en){number,name}</dfn>"
20
+ value = WhowishWord.word_for_in_edit_mode("namespace", "id", "en", :number=>5, :name=>"tanin")
21
+ value.should == "<dfn>#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)|number,name#{WhowishWord::SEPARATOR}id(en){number,name}</dfn>"
22
22
 
23
23
  end
24
24
 
@@ -26,7 +26,7 @@ describe 'word_for' do
26
26
  it "no variables, with entry" do
27
27
 
28
28
  WhowishWord.words = {"namespace:id(en)" => "hello"}
29
- value = WhowishWord.word_for_in_edit_mode("namespace", "id")
29
+ value = WhowishWord.word_for_in_edit_mode("namespace", "id", "en")
30
30
  value.should == "<dfn>#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)#{WhowishWord::SEPARATOR}hello</dfn>"
31
31
 
32
32
  end
@@ -35,7 +35,7 @@ describe 'word_for' do
35
35
  it "with variables, with entry" do
36
36
 
37
37
  WhowishWord.words = {"namespace:id(en)" => "hello {name} for {number} times"}
38
- value = WhowishWord.word_for_in_edit_mode("namespace", "id", :number=>5, :name=>"tanin")
38
+ value = WhowishWord.word_for_in_edit_mode("namespace", "id", "en", :number=>5, :name=>"tanin")
39
39
  value.should == "<dfn>#{WhowishWord::PREFIX}#{WhowishWord::SEPARATOR}namespace:id(en)|number,name#{WhowishWord::SEPARATOR}hello tanin for 5 times</dfn>"
40
40
 
41
41
  end
@@ -9,16 +9,16 @@ describe 'word_for' do
9
9
 
10
10
  it "no variables, no entry" do
11
11
 
12
- value = WhowishWord.word_for("namespace", "id")
13
- value.should == "namespace:id(en)"
12
+ value = WhowishWord.word_for("namespace", "id", "en")
13
+ value.should == "id(en)"
14
14
 
15
15
  end
16
16
 
17
17
 
18
18
  it "with variables, no entry" do
19
19
 
20
- value = WhowishWord.word_for("namespace", "id", :number=>5, :name=>"tanin")
21
- value.should == "namespace:id(en){number,name}"
20
+ value = WhowishWord.word_for("namespace", "id", "en", :number=>5, :name=>"tanin")
21
+ value.should == "id(en){number,name}"
22
22
 
23
23
  end
24
24
 
@@ -26,7 +26,7 @@ describe 'word_for' do
26
26
  it "no variables, with entry" do
27
27
 
28
28
  WhowishWord.words = {"namespace:id(en)" => "hello"}
29
- value = WhowishWord.word_for("namespace", "id")
29
+ value = WhowishWord.word_for("namespace", "id", "en")
30
30
  value.should == "hello"
31
31
 
32
32
  end
@@ -35,7 +35,7 @@ describe 'word_for' do
35
35
  it "with variables, with entry" do
36
36
 
37
37
  WhowishWord.words = {"namespace:id(en)" => "hello {name} for {number} times"}
38
- value = WhowishWord.word_for("namespace", "id", :number=>5, :name=>"tanin")
38
+ value = WhowishWord.word_for("namespace", "id", "en", :number=>5, :name=>"tanin")
39
39
  value.should == "hello tanin for 5 times"
40
40
 
41
41
  end
data/whowish_word.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "whowish_word"
6
- s.version = "0.2.7"
6
+ s.version = "0.3.0"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Tanin Na Nakorn"]
9
9
  s.email = ["tanin47@yahoo.com"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whowish_word
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-01 00:00:00.000000000Z
12
+ date: 2011-11-09 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A Rails gem that make static content editable
15
15
  email:
@@ -31,6 +31,7 @@ files:
31
31
  - lib/whowish_word/action_view/base.rb
32
32
  - lib/whowish_word/action_view/template.rb
33
33
  - lib/whowish_word/authentication.rb
34
+ - lib/whowish_word/config.rb
34
35
  - lib/whowish_word/constant.rb
35
36
  - lib/whowish_word/db_migration/active_record/whowish_word_html.rb
36
37
  - lib/whowish_word/initializer.rb