i18n-backend-side_by_side 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +99 -0
- data/Rakefile +8 -0
- data/VERSION +1 -0
- data/i18n-backend-side_by_side.gemspec +24 -0
- data/lib/i18n/backend/side_by_side.rb +56 -0
- data/test/locales/novel.yml +21 -0
- data/test/locales/oldschool.de.yml +2 -0
- data/test/locales/oldschool.en.yml +2 -0
- data/test/test.rb +35 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8d311a7851ea64a4d1115d3f7d675f44b0606d3d
|
4
|
+
data.tar.gz: c7f81ea78dfac9b4d0c8fd9ced3fa081f23d39a4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2dfde3d797ea374e4d188c8dbe737bd35c4fdbfee4ccc7c7e4539e719a0bd0a7e0141d946e57fd902cad3fbadac0554d2f5f2e3a9eaef731f864f2f17f869b51
|
7
|
+
data.tar.gz: 59e9aa42b0b68db073af5e20fc0bf2fecfc41487587cc49737cf9a545a582140f0dcdca9f1d4e676a95d6d05c3a199f830434d89f4146f25250bdcb163917997
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 ElectricFeel Mobility Systems GmbH
|
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,99 @@
|
|
1
|
+
# i18n-backend-side\_by\_side
|
2
|
+
|
3
|
+
**Tired of jumping between language files when translating keys? Stop jumping
|
4
|
+
and have all the languages side by side.**
|
5
|
+
|
6
|
+
This gem is a subclass of the default `I18n::Backend::Simple` backend. It
|
7
|
+
changes the way translations get loaded from files in order to support
|
8
|
+
specifying the language code anywhere along the key path as opposed to the root
|
9
|
+
key. This allows for all languages being defined next to each other,
|
10
|
+
making it easier to translate between them.
|
11
|
+
|
12
|
+
## How It Works
|
13
|
+
|
14
|
+
Let's assume your app supports English, Spanish, and German. You probably have
|
15
|
+
one file per language:
|
16
|
+
|
17
|
+
```
|
18
|
+
locales/
|
19
|
+
view.en.yml
|
20
|
+
view.es.yml
|
21
|
+
view.de.yml
|
22
|
+
```
|
23
|
+
|
24
|
+
The files might look something like this (e.g. `locales/en.yml`):
|
25
|
+
|
26
|
+
```yaml
|
27
|
+
en:
|
28
|
+
view:
|
29
|
+
title: Welcome
|
30
|
+
inbox:
|
31
|
+
zero: You have no messages
|
32
|
+
one: You have one message
|
33
|
+
other: 'You have %{count} messages'
|
34
|
+
```
|
35
|
+
|
36
|
+
Whenever you have to add or modify a key, you have to edit all files. Also, you
|
37
|
+
can't see all translations at once for a specific key. With this gem you can
|
38
|
+
merge all the files and specify the translation for a key at that key:
|
39
|
+
|
40
|
+
```yaml
|
41
|
+
_:
|
42
|
+
view:
|
43
|
+
title:
|
44
|
+
_en: Welcome
|
45
|
+
_es: Bienvenido
|
46
|
+
_de: Willkommen
|
47
|
+
inbox:
|
48
|
+
_en:
|
49
|
+
zero: You have no messages
|
50
|
+
one: You have one message
|
51
|
+
other: 'You have %{count} messages'
|
52
|
+
_es:
|
53
|
+
zero: No tiene mensajes
|
54
|
+
one: Tienes un mensaje
|
55
|
+
other: 'Tienes %{count} messajes'
|
56
|
+
_de:
|
57
|
+
zero: Du hast keine Nachrichten
|
58
|
+
one: Du hast eine Nachricht
|
59
|
+
other: 'Du hast %{count} Nachrichten'
|
60
|
+
```
|
61
|
+
|
62
|
+
Two things to note here:
|
63
|
+
|
64
|
+
1. The root key is an underscore. Omitting it results in the file being
|
65
|
+
processed as a regular translation file, without support for side-by-side
|
66
|
+
translations.
|
67
|
+
|
68
|
+
2. The language codes are prefixed with an underscore. This is needed in order
|
69
|
+
to distinguish a language code key from a normal key. This also means that
|
70
|
+
regular keys can't start with an underscore.
|
71
|
+
|
72
|
+
When the files get loaded, they're transformed on the fly to the original format
|
73
|
+
by moving the language code to the beginning of the key path:
|
74
|
+
|
75
|
+
```
|
76
|
+
_.foo.bar._en => en.foo.bar
|
77
|
+
_.foo.bar._en-UK.abc.xyz => en-UK.foo.bar.abc.xyz
|
78
|
+
```
|
79
|
+
|
80
|
+
## Installation
|
81
|
+
|
82
|
+
Add this line to your application's Gemfile:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
gem 'i18n-backend-side_by_side'
|
86
|
+
```
|
87
|
+
|
88
|
+
Set up `I18n` to use an instance of this backend:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
I18n.backend = I18n::Backend::SideBySide.new
|
92
|
+
```
|
93
|
+
|
94
|
+
That's it. Continue using `I18n` as you're used to. Happy translating!
|
95
|
+
|
96
|
+
## Authors
|
97
|
+
|
98
|
+
- Pratik Mukerji ([pmukerji](https://github.com/pmukerji))
|
99
|
+
- Philipe Fatio ([fphilipe](https://github.com/fphilipe))
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'i18n-backend-side_by_side'
|
7
|
+
spec.version = File.read('VERSION').strip
|
8
|
+
spec.authors = ['Philipe Fatio', 'Pratik Mukerji']
|
9
|
+
spec.email = ['me@phili.pe', 'pratik@electricfeel.com']
|
10
|
+
spec.summary = %q{Tired of jumping between language files when translating keys? Stop jumping and have all the languages side by side.}
|
11
|
+
spec.description = spec.summary
|
12
|
+
spec.homepage = 'https://github.com/electric-feel/i18n-backend-side_by_side'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'minitest', '~> 5.8'
|
23
|
+
spec.add_dependency 'i18n', '< 1'
|
24
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Backend
|
5
|
+
class SideBySide < Simple
|
6
|
+
VERSION = File.read(File.expand_path('../../../../VERSION', __FILE__))
|
7
|
+
LOCALE_PREFIX = '_'
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def load_file(filename)
|
12
|
+
type = File.extname(filename).tr('.', '').downcase
|
13
|
+
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
|
14
|
+
data = send(:"load_#{type}", filename)
|
15
|
+
unless data.is_a?(Hash)
|
16
|
+
raise InvalidLocaleData.new(filename, 'expects it to return a hash, but does not')
|
17
|
+
end
|
18
|
+
|
19
|
+
if data.first.first == LOCALE_PREFIX
|
20
|
+
_process([], data[LOCALE_PREFIX].deep_symbolize_keys)
|
21
|
+
else
|
22
|
+
data.each { |locale, d| store_translations(locale, d || {}) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def _process(path, hash)
|
29
|
+
if _contains_locales?(hash)
|
30
|
+
hash.each do |locale, value|
|
31
|
+
_store([_strip_locale_prefix(locale)] + path, value)
|
32
|
+
end
|
33
|
+
else
|
34
|
+
hash.each { |key, value| _process(path + [key], value) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def _store(path, value)
|
39
|
+
*keys, last_key = path
|
40
|
+
target = keys.inject(translations) do |hash, key|
|
41
|
+
hash[key] ||= {}
|
42
|
+
hash[key]
|
43
|
+
end
|
44
|
+
target[last_key] = value
|
45
|
+
end
|
46
|
+
|
47
|
+
def _contains_locales?(hash)
|
48
|
+
hash.first.first[0] == LOCALE_PREFIX
|
49
|
+
end
|
50
|
+
|
51
|
+
def _strip_locale_prefix(locale)
|
52
|
+
locale[LOCALE_PREFIX.length..-1].to_sym
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'i18n/backend/side_by_side'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
I18n.load_path = Dir['test/locales/*.yml']
|
6
|
+
I18n.backend = I18n::Backend::SideBySide.new
|
7
|
+
|
8
|
+
class Test < Minitest::Test
|
9
|
+
def test_oldschool
|
10
|
+
assert_equal('Still here', I18n.t('oldschool', locale: :en))
|
11
|
+
assert_equal('Immer noch hier', I18n.t('oldschool', locale: :de))
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_simple
|
15
|
+
assert_equal('Hi', I18n.t('foo.bar', locale: :en))
|
16
|
+
assert_equal('Hallo', I18n.t('foo.bar', locale: :de))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_hash
|
20
|
+
assert_equal({ key: 'value' }, I18n.t('foo.hash', locale: :en))
|
21
|
+
assert_equal({ key: 'Wert' }, I18n.t('foo.hash', locale: :de))
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_count
|
25
|
+
assert_equal('none', I18n.t('foo.count', count: 0, locale: :en))
|
26
|
+
assert_equal('keine', I18n.t('foo.count', count: 0, locale: :de))
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_partially_missing
|
30
|
+
assert_equal('some value', I18n.t('foo.only_en.some_key', locale: :en))
|
31
|
+
assert_equal({ some_key: 'some value' }, I18n.t('foo.only_en', locale: :en))
|
32
|
+
assert_raises { I18n.t('foo.only_en.some_key', locale: :de, raise: true) }
|
33
|
+
assert_raises { I18n.t('foo.only_en', locale: :de, raise: true) }
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n-backend-side_by_side
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philipe Fatio
|
8
|
+
- Pratik Mukerji
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-08-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.7'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.7'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.8'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '5.8'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: i18n
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "<"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '1'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "<"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1'
|
70
|
+
description: Tired of jumping between language files when translating keys? Stop jumping
|
71
|
+
and have all the languages side by side.
|
72
|
+
email:
|
73
|
+
- me@phili.pe
|
74
|
+
- pratik@electricfeel.com
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- ".gitignore"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- VERSION
|
85
|
+
- i18n-backend-side_by_side.gemspec
|
86
|
+
- lib/i18n/backend/side_by_side.rb
|
87
|
+
- test/locales/novel.yml
|
88
|
+
- test/locales/oldschool.de.yml
|
89
|
+
- test/locales/oldschool.en.yml
|
90
|
+
- test/test.rb
|
91
|
+
homepage: https://github.com/electric-feel/i18n-backend-side_by_side
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.5
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Tired of jumping between language files when translating keys? Stop jumping
|
115
|
+
and have all the languages side by side.
|
116
|
+
test_files:
|
117
|
+
- test/locales/novel.yml
|
118
|
+
- test/locales/oldschool.de.yml
|
119
|
+
- test/locales/oldschool.en.yml
|
120
|
+
- test/test.rb
|