alephant-renderer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99aaf2d2aa11f5ac1f525a49943ad5e3b0662215
4
- data.tar.gz: f50d57090b29cf1016ca6e80f6bd39b1b5598081
3
+ metadata.gz: 8fee80f6f73582dd588edaa90618125a27ceaf6c
4
+ data.tar.gz: 19487c21a2c959aa75195ce3039cd5ebad362edc
5
5
  SHA512:
6
- metadata.gz: 6e45b2dc23c8cd5d18db75445f4dbc138ff96f427ad73acd0a17292ef1eaf35a0bae2019b8447618fa89425bea9e3881b880594b7c3cd8beaaf4a1401226987b
7
- data.tar.gz: 8eaf85eb822165ceb11ae4ae3a1f37aa5e17d6354f987a41142810482e576d7b6f7c306c65bf8c9f50e198438627f3ea3ce44ea4ba761c33d18e3a0e86bcc8e5
6
+ metadata.gz: 108b9c8f2ba7e33146c5c377c9bb2fd36bb6e331a162308a4dfbc066cd7744b21172a51c4b3b343133bb4837399a5ff4f5664a1c10db0e4c04de267f6dde512b
7
+ data.tar.gz: 46db4d9d1bbe6e89d5bbbe5fcf5c1461ab5172dc4b1a5120a1ce31b4e9d916b2d04ebbc3f4a11765aab9091b05d1af743f8b061c2d05a08719271cf3381f971e
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  Gemfile.lock
4
4
  .rspec
5
5
  *.gem
6
+ *.swp
6
7
 
7
8
  /pkg
8
9
  /tmp
data/README.md CHANGED
@@ -28,9 +28,86 @@ require 'alephant/renderer'
28
28
  Alephant::Renderer.create('foo_template', '/base/path', 'foo_model')
29
29
  ```
30
30
 
31
+ ## Translations
32
+
33
+ Currently there is a simple implementation of the [i18n](https://github.com/svenfuchs/i18n) library that allows templates to be translated.
34
+
35
+ ### Setup
36
+
37
+ You need the following directory structure inside of the folder that relates to the `base_path`:
38
+
39
+ ```bash
40
+ components
41
+ ├── locale
42
+ │ ├── en.yml
43
+ │ ├── cy.yml
44
+ ```
45
+
46
+ The yaml configs must have the extension `.yml`.
47
+
48
+ ### Yaml structure
49
+
50
+ The yaml translations files must follow the following structure:
51
+
52
+ ```yaml
53
+ en:
54
+ template_name:
55
+ key: 'foo'
56
+ sub:
57
+ key: 'bar'
58
+
59
+ another_template:
60
+ key: 'baz'
61
+ ```
62
+
63
+ The first node is the language code, then the next set of nodes are the names of the templates files that the translations apply to. This allows you to just reference the translation key in the templates without prefixing the name of the template.
64
+
65
+ ### Usage
66
+
67
+ For each translation, a seperate model and view is needed.
68
+
69
+ #### Model
70
+
71
+ All that's needed in the model is to override the LOCALE constant:
72
+
73
+ ```rb
74
+ class TestModel < Alephant::Views::Base
75
+ LOCALE = :cy
76
+ end
77
+ ```
78
+
79
+ #### Templates
80
+
81
+ The best approach with the templates when translations are needed, is to have a base template then a seperate one for each lanuage that's being translated.
82
+
83
+ The following tag can be used in the templates to perform a translation:
84
+
85
+ ```mustache
86
+ {{#t}}text.to.translate{{/t}}
87
+ ```
88
+
89
+ #### Example
90
+
91
+ If we had a template called 'test_template.mustache' we would have the following:
92
+
93
+ >test_template.mustache
94
+
95
+ ```mustache
96
+ {{#t}}translation.key{{/t}}
97
+ ```
98
+
99
+ >locale/en.yml
100
+
101
+ ```yaml
102
+ en:
103
+ test_template:
104
+ translation:
105
+ key: 'A translation!'
106
+ ```
107
+
31
108
  ## Contributing
32
109
 
33
- 1. Fork it ( http://github.com/<my-github-username>/alephant-renderer/fork )
110
+ 1. Fork it ( http://github.com/bbc-news/alephant-renderer/fork )
34
111
  2. Create your feature branch (`git checkout -b my-new-feature`)
35
112
  3. Commit your changes (`git commit -am 'Add some feature'`)
36
113
  4. Push to the branch (`git push origin my-new-feature`)
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "bundler", "~> 1.5"
29
29
  spec.add_development_dependency "rake"
30
30
 
31
+ spec.add_runtime_dependency 'i18n'
31
32
  spec.add_runtime_dependency 'mustache', '>= 0.99.5'
32
33
  spec.add_runtime_dependency 'alephant-logger'
33
34
  spec.add_runtime_dependency 'hashie'
@@ -1,5 +1,6 @@
1
1
  require 'alephant/logger'
2
2
  require 'mustache'
3
+ require 'i18n'
3
4
 
4
5
  module Alephant
5
6
  module Renderer
@@ -13,6 +14,8 @@ module Alephant
13
14
  @base_path = base_path
14
15
  @model = model
15
16
 
17
+ load_translations_from base_path
18
+
16
19
  logger.info("Renderer.initialize: end with @base_path set to #{@base_path}")
17
20
  end
18
21
 
@@ -31,6 +34,28 @@ module Alephant
31
34
  logger.error("Renderer.template: view template #{template_file} not found")
32
35
  end
33
36
  end
37
+
38
+ private
39
+
40
+ def load_translations_from(base_path)
41
+ if I18n.load_path.empty?
42
+ I18n.config.enforce_available_locales = false
43
+ I18n.load_path << i18n_load_path_from(base_path)
44
+ I18n.backend.load_translations
45
+ end
46
+ end
47
+
48
+ def i18n_load_path_from(base_path)
49
+ Dir[
50
+ File.join(
51
+ Pathname.new(base_path).parent,
52
+ 'locale',
53
+ '*.yml')
54
+ ]
55
+ .flatten
56
+ .uniq
57
+ end
58
+
34
59
  end
35
60
 
36
61
  end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Renderer
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -1,15 +1,31 @@
1
1
  require 'mustache'
2
2
  require 'alephant/views'
3
3
  require 'hashie'
4
+ require 'json'
5
+ require 'i18n'
4
6
 
5
7
  module Alephant::Views
6
8
  class Base < Mustache
7
9
  attr_accessor :data
8
10
 
11
+ LOCALE = :en
12
+
9
13
  def initialize(data = {})
10
14
  @data = Hashie::Mash.new data
11
15
  end
12
16
 
17
+ def t(*args)
18
+ I18n.locale = self.class::LOCALE
19
+ lambda do |comma_delimited_args|
20
+ args = comma_delimited_args.strip.split ','
21
+ key = args.shift
22
+ params = args.empty? ? {} : JSON.parse(args.first).with_indifferent_access
23
+ prefix = /\/([^\/]+)\./.match(template_file)[1]
24
+
25
+ I18n.translate("#{prefix}.#{key}", params)
26
+ end
27
+ end
28
+
13
29
  def self.inherited(subclass)
14
30
  ::Alephant::Views.register(subclass)
15
31
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Alephant::Renderer do
4
4
  let(:template_file) { 'foo' }
5
- let(:base_path) { :base_path }
5
+ let(:base_path) { 'bar/baz' }
6
6
  let(:model) { :model }
7
7
 
8
8
  before(:each) do
@@ -0,0 +1,2 @@
1
+ en:
2
+ test: 'Hello!'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Kenny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-27 00:00:00.000000000 Z
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -136,6 +136,20 @@ dependencies:
136
136
  version: '0'
137
137
  prerelease: false
138
138
  type: :development
139
+ - !ruby/object:Gem::Dependency
140
+ name: i18n
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirement: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ prerelease: false
152
+ type: :runtime
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: mustache
141
155
  version_requirements: !ruby/object:Gem::Requirement
@@ -204,6 +218,7 @@ files:
204
218
  - spec/fixtures/components/foo/templates/foo.mustache
205
219
  - spec/renderer_spec.rb
206
220
  - spec/spec_helper.rb
221
+ - trans.yml
207
222
  homepage: ''
208
223
  licenses:
209
224
  - MIT