text_helpers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +65 -0
- data/Rakefile +7 -0
- data/lib/text_helpers/contexts.rb +24 -0
- data/lib/text_helpers/railtie.rb +47 -0
- data/lib/text_helpers/translation.rb +55 -0
- data/lib/text_helpers/version.rb +3 -0
- data/lib/text_helpers.rb +5 -0
- data/test/lib/text_helpers/translation_test.rb +57 -0
- data/test/lib/text_helpers/version_test.rb +8 -0
- data/test/test_helper.rb +2 -0
- data/text_helpers.gemspec +27 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 307f8ef83a7eb8077ed118ef2bf734a50928a154
|
4
|
+
data.tar.gz: 310ecdda9460282480f1aa0b4631b2ab34959d82
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d74b813995973bbd20a31e896de3da1d0591a78149af78f9aca8520ea9eaf84111b6c9493ea6617751b2605bea2eb8cb09f1faf4719374222a5df8e51942a106
|
7
|
+
data.tar.gz: 5d076830d152a39124937a218916066838ac3c16f6a77670ee4655dee9bdbffc2f0562b97187cbc9692bfc3b809e7d70c50273ec14329f6d3faef1f141cecc57
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrew Horner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# TextHelpers
|
2
|
+
|
3
|
+
`TextHelpers` is a library intended to make working with static text in Rails
|
4
|
+
projects as painless as possible.
|
5
|
+
|
6
|
+
Include it in your `Gemfile` with:
|
7
|
+
|
8
|
+
```
|
9
|
+
gem "text_helpers"
|
10
|
+
```
|
11
|
+
|
12
|
+
## Suggested Use
|
13
|
+
|
14
|
+
All static text should be placed in locale files, in a directory
|
15
|
+
structure mirroring the app directory structure. The text for
|
16
|
+
`app/views/some/_partial.html.haml` would go in
|
17
|
+
`config/locales/views/some/partial.en.yml`, for example. This is not a strict
|
18
|
+
requirement, but will go a long way toward keeping your locales easily
|
19
|
+
maintainable.
|
20
|
+
|
21
|
+
In any locale file entry, you can reference another key in the locale file by
|
22
|
+
using the syntax `!scope.to.key!`. For the sake of maintainability, the use of
|
23
|
+
this interpolation should be restricted to small fragments of highly-recycled
|
24
|
+
static values. `I18n`'s built-in `%{value}` interpolation can be used for
|
25
|
+
variable text.
|
26
|
+
|
27
|
+
### In Views
|
28
|
+
|
29
|
+
To access this text in views, two helpers are available, `text` and `html`.
|
30
|
+
Both helpers take a lookup key, used to identify the desired piece of text,
|
31
|
+
and an argument hash, which is forwarded to the `I18n.t` call.
|
32
|
+
|
33
|
+
`text` returns the requested text, with special values interpolated, and made
|
34
|
+
html_safe (so HTML can be used here, when absolutely necessary).
|
35
|
+
|
36
|
+
`html` parses the requested text using Markdown, making it useful for rendering
|
37
|
+
larger pieces of text involving multiple paragraphs, list items or links.
|
38
|
+
|
39
|
+
If you want to render a small fragment of Markdown without `p` tag wrappers,
|
40
|
+
you can pass `inline: true` as an option to `html`.
|
41
|
+
|
42
|
+
### In Controllers
|
43
|
+
|
44
|
+
The same helpers are available in controllers, with the translation scope based
|
45
|
+
on the controller name rather than the view directory. This will typically be
|
46
|
+
used for flash messages or alerts of some kind.
|
47
|
+
|
48
|
+
## Testing
|
49
|
+
|
50
|
+
Some shared `RSpec` contexts are available to allow the same locale
|
51
|
+
abstractions for testing. You can include these contexts with:
|
52
|
+
|
53
|
+
```
|
54
|
+
require "text_helpers/contexts"
|
55
|
+
```
|
56
|
+
|
57
|
+
### Views
|
58
|
+
|
59
|
+
The view text helpers described above can be accessed in view
|
60
|
+
specs by adding `view: true` to the spec metadata.
|
61
|
+
|
62
|
+
### Controllers
|
63
|
+
|
64
|
+
The controller text helpers described above can be accessed in controller
|
65
|
+
specs by adding `controller: true` to your spec metadata.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
shared_context "a view spec", view: true do
|
2
|
+
include TextHelpers::Translation
|
3
|
+
|
4
|
+
def translation_scope
|
5
|
+
matcher = /(?<path>.*)\/_?(?<view>[^\/.]+)(?<extension>\.html\.haml)?/
|
6
|
+
info = matcher.match(example.metadata[:full_description])
|
7
|
+
|
8
|
+
if info
|
9
|
+
path = info[:path].gsub('/', '.')
|
10
|
+
"views.#{path}.#{info[:view]}"
|
11
|
+
else
|
12
|
+
"views.#{params[:controller]}.#{params[:action]}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
shared_context "a controller spec", controller: true do
|
18
|
+
include TextHelpers::Translation
|
19
|
+
|
20
|
+
def translation_scope
|
21
|
+
controller_name = described_class.name.sub(/Controller\z/, '').underscore
|
22
|
+
"controllers.#{controller_name}"
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module TextHelpers
|
2
|
+
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
|
5
|
+
initializer "text_helpers.configure_rails_initialization" do
|
6
|
+
|
7
|
+
class ActionView::Base
|
8
|
+
include TextHelpers::Translation
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
# Protected: Derive a translation scope from the current view template.
|
13
|
+
#
|
14
|
+
# Determines an I18n-friendly scope for the current view file when possible,
|
15
|
+
# or falls back to "views.<controller>.<action>"
|
16
|
+
#
|
17
|
+
# Returns a String.
|
18
|
+
def translation_scope
|
19
|
+
matcher = /(?<path>.*)\/_?(?<view>[^\/.]+)(?<extension>\.html\.haml)?/
|
20
|
+
info = matcher.match(@virtual_path)
|
21
|
+
|
22
|
+
if info
|
23
|
+
path = info[:path].gsub('/', '.')
|
24
|
+
"views.#{path}.#{info[:view]}"
|
25
|
+
else
|
26
|
+
"views.#{params[:controller]}.#{params[:action]}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class ActionController::Base
|
32
|
+
include TextHelpers::Translation
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
# Protected: Provides a scope for I18n lookups.
|
37
|
+
#
|
38
|
+
# Should look like `controllers.<controller_name>`.
|
39
|
+
#
|
40
|
+
# Returns a String.
|
41
|
+
def translation_scope
|
42
|
+
"controllers.#{params[:controller]}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "i18n"
|
2
|
+
require "active_support/core_ext/string/output_safety"
|
3
|
+
require "github/markdown"
|
4
|
+
|
5
|
+
module TextHelpers
|
6
|
+
|
7
|
+
module Translation
|
8
|
+
|
9
|
+
# Public: Get the I18n localized text for the passed key.
|
10
|
+
#
|
11
|
+
# key - The desired I18n lookup key.
|
12
|
+
# options - A Hash of options to pass through to the lookup.
|
13
|
+
#
|
14
|
+
# Returns a String resulting from the I18n lookup.
|
15
|
+
def text(key, options = {})
|
16
|
+
text = I18n.t(key, {
|
17
|
+
scope: self.translation_scope,
|
18
|
+
default: "!#{key}!"
|
19
|
+
}.merge(options))
|
20
|
+
|
21
|
+
# Interpolate any keypaths (e.g., `!some.lookup.key!`) found in the text.
|
22
|
+
text.strip.gsub(/!([\w._]+)!/) do |match|
|
23
|
+
I18n.t($1)
|
24
|
+
end.html_safe
|
25
|
+
end
|
26
|
+
|
27
|
+
# Public: Get an HTML representation of the rendered markdown for the passed I18n key.
|
28
|
+
#
|
29
|
+
# key - The desired I18n lookup key.
|
30
|
+
# options - A Hash of options to pass through to the lookup.
|
31
|
+
# :inline - A special option that will remove the enclosing <p>
|
32
|
+
# tags when set to true.
|
33
|
+
#
|
34
|
+
# Returns a String containing the localized text rendered via Markdown
|
35
|
+
def html(key, options = {})
|
36
|
+
rendered = GitHub::Markdown.render(text(key, options))
|
37
|
+
if options[:inline]
|
38
|
+
rendered.match(/\A<p>(.*)<\/p>\Z/)[1].html_safe
|
39
|
+
else
|
40
|
+
rendered.html_safe
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
# Protected: The proper scope for I18n translation.
|
47
|
+
#
|
48
|
+
# Must be implemented by any classes which include this module.
|
49
|
+
#
|
50
|
+
# Raises NotImplementedError.
|
51
|
+
def translation_scope
|
52
|
+
raise NotImplementedError
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/text_helpers.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
describe TextHelpers::Translation do
|
4
|
+
before do
|
5
|
+
@helper = Object.send(:include, TextHelpers::Translation).new
|
6
|
+
end
|
7
|
+
describe "given a stored I18n lookup" do
|
8
|
+
before do
|
9
|
+
@scoped_text = "Scoped lookup"
|
10
|
+
@global_text = "Global lookup"
|
11
|
+
|
12
|
+
I18n.backend.store_translations :en, {
|
13
|
+
test_key: @global_text,
|
14
|
+
test: {
|
15
|
+
test_key: "*#{@scoped_text}*",
|
16
|
+
interpolated_key: "Global? (!test_key!)"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "for a specified scope" do
|
22
|
+
before do
|
23
|
+
@helper.define_singleton_method :translation_scope do
|
24
|
+
'test'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "looks up the text for the key in a scope derived from the call stack" do
|
29
|
+
assert_equal @helper.text(:test_key), "*#{@scoped_text}*"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "converts the text to HTML via Markdown" do
|
33
|
+
assert_equal @helper.html(:test_key), "<p><em>#{@scoped_text}</em></p>\n"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "removes the enclosing paragraph with :inline" do
|
37
|
+
assert_equal @helper.html(:test_key, inline: true), "<em>#{@scoped_text}</em>"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "interpolates values wrapped in !!" do
|
41
|
+
assert_equal @helper.text(:interpolated_key), "Global? (#{@global_text})"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "when no valid scope is provided" do
|
46
|
+
before do
|
47
|
+
@helper.define_singleton_method :translation_scope do
|
48
|
+
'nonexistent'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "defaults to a globally-defined value for the key" do
|
53
|
+
assert_equal @helper.text(:test_key), @global_text
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/text_helpers/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Andrew Horner"]
|
6
|
+
gem.email = ["andrew@tablexi.com"]
|
7
|
+
|
8
|
+
gem.homepage = "https://github.com/ahorner/text-helpers"
|
9
|
+
gem.description = %q{Easily fetch text and static content from your locales}
|
10
|
+
gem.summary = %q{
|
11
|
+
TextHelpers is a gem which supplies some basic utilities for text
|
12
|
+
localization in Rails projects.
|
13
|
+
|
14
|
+
The library is intended to make it simple to keep your application's static
|
15
|
+
and semi-static text content independent of the view structure.
|
16
|
+
}
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($\)
|
19
|
+
gem.test_files = gem.files.grep(%r{^test/})
|
20
|
+
gem.name = "text_helpers"
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
gem.version = TextHelpers::VERSION
|
23
|
+
|
24
|
+
gem.add_dependency('activesupport')
|
25
|
+
gem.add_dependency('i18n')
|
26
|
+
gem.add_dependency('github-markdown')
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: text_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Horner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: github-markdown
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Easily fetch text and static content from your locales
|
56
|
+
email:
|
57
|
+
- andrew@tablexi.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/text_helpers.rb
|
68
|
+
- lib/text_helpers/contexts.rb
|
69
|
+
- lib/text_helpers/railtie.rb
|
70
|
+
- lib/text_helpers/translation.rb
|
71
|
+
- lib/text_helpers/version.rb
|
72
|
+
- test/lib/text_helpers/translation_test.rb
|
73
|
+
- test/lib/text_helpers/version_test.rb
|
74
|
+
- test/test_helper.rb
|
75
|
+
- text_helpers.gemspec
|
76
|
+
homepage: https://github.com/ahorner/text-helpers
|
77
|
+
licenses: []
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.6
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: TextHelpers is a gem which supplies some basic utilities for text localization
|
99
|
+
in Rails projects. The library is intended to make it simple to keep your application's
|
100
|
+
static and semi-static text content independent of the view structure.
|
101
|
+
test_files:
|
102
|
+
- test/lib/text_helpers/translation_test.rb
|
103
|
+
- test/lib/text_helpers/version_test.rb
|
104
|
+
- test/test_helper.rb
|