rails_translations 0.2.1
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 +8 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/rails_translations +5 -0
- data/lib/custom_hash.rb +40 -0
- data/lib/rails_translations/version.rb +3 -0
- data/lib/rails_translations.rb +104 -0
- data/rails_translations.gemspec +28 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5af6142ee3a589039ee1a384285162e1f5af9f50360f14b1016a75485c9fa88d
|
4
|
+
data.tar.gz: 0d409ae7dcabf3fdc8ddab2e685a950b751a4f7ff2144dd77411bae665fb9e1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91c24d850ea7cc47c35812e846b25d37ef2698f81a1daaebf2abb31e3b647670c82f810895c89228bff5a73ede96f54aa8af3a1e65d69380feb1aecdebe94185
|
7
|
+
data.tar.gz: 82f37cc74496cc176263414363e8906209db0e932df40dbeac7d808fd67bf73368e756e13d237d57d7b50eade6b15b19781f6ef41c69620b343009ba2c1d19fc
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 vedraninho
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# RailsTranslations
|
2
|
+
|
3
|
+
A gem that syncs locales yml files and generates models attributes translation placeholders.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile only (`recommended: for development environment only`):
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rails_translations'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
This gem can only be used from Rails console at this point. The terminal commands will be added in future releases.
|
20
|
+
|
21
|
+
## Equilizing translations files
|
22
|
+
|
23
|
+
**It's assumed that you are using default rails `config/locales/` folder organization with files which are not nested under subfolders and which have `.yml` extension.**
|
24
|
+
|
25
|
+
There are 2 commands which equilize your translation files:
|
26
|
+
|
27
|
+
### `RailsTranslations.equilize_translations(options={})`
|
28
|
+
|
29
|
+
This will go through all your translations files and merge their keys. The existing keys inside the each file will be preserved and those added from other files will have default suffix `"TODO: "`. It will not cross merge keys if you have multiple files for same locale in your `config/locales/` directory.
|
30
|
+
|
31
|
+
### `RailsTranslations.equilize_files(file_name, options={})`
|
32
|
+
|
33
|
+
This is essentially the same as the first command but only for one set of translation files. For example if you pass `"models"` as a file name it will merge the translations of *models.en.yml, models.hr.yml, models.de.yml* etc. You can list your file names with `RailsTranslations.get_translations_files_list` - this will give you the file names without `locale.yml` extension.
|
34
|
+
|
35
|
+
### Options
|
36
|
+
`equilize_locales:` accepts the array of locales you want to merge with other locales. For example, if you have `:hr, :en, :de` and you pass `RailsTranslations.equilize_translations(equilize_locales: [:hr, :de])` the missing keys in files for those locales will be added from other locales - in this case any keys from *models.hr.yml* and *models.de.yml* will not be added to *models.en.yml*.
|
37
|
+
|
38
|
+
`take_from_locales:` accepts the array of locales you want to take missing translations from. So if you pass `RailsTranslations.equilize_translations(equilize_locales: [:hr, :de])` no translations from `.en` files would be copied to the `.de` or `.hr` files.
|
39
|
+
|
40
|
+
`file_names:` accepts the array of file names you want to equilize - only for `equilize_translations` mathod. For example `RailsTranslations.equilize_translations(file_names: ["mailing", "devise"])`. Note that file name for base translation files, such as `en.yml` or `de.yml`, is empty string `""`.
|
41
|
+
|
42
|
+
`prefix:` here you specify how you want to prefix the added missing translations. Default value is `"TODO:"`. And if you pass `RailsTranslations.equilize_translations(prefix: "MISSING:")` it will add the missing translation as:
|
43
|
+
```yml
|
44
|
+
de:
|
45
|
+
...
|
46
|
+
missing_translation: "MISSING: some missing translation"
|
47
|
+
...
|
48
|
+
```
|
49
|
+
|
50
|
+
### Usage tips
|
51
|
+
|
52
|
+
When copying missing translations the method will prioretize the locale which goes first, so if your `I18n.available_locales` are `[:en, :de, :hr]` missing translation will be copied from `:en` first. To change this order, just pass the `take_from_locales:` in prefered order.
|
53
|
+
|
54
|
+
If you pass unexisting `file_names:` the method will create set of files with that name so it can be used to generate new translation files.
|
55
|
+
|
56
|
+
If you have `[:en, :de, :hr]` available locales and you are missing the files for one of the locales it will generate new file by mergeing other two locale files, so it can be used for generating new set of files if new available locale is added or files are simply missing.
|
57
|
+
|
58
|
+
## Building models attributes placeholders
|
59
|
+
|
60
|
+
It is used to collect models attributes and make the translations placeholders in file named `"models_fields"` by default.
|
61
|
+
|
62
|
+
`RailsTranslations.build_attributes_placeholders(options={})`
|
63
|
+
|
64
|
+
It will go through your models, collect attributes and for each your available locale generate something like:
|
65
|
+
|
66
|
+
```yml
|
67
|
+
en:
|
68
|
+
activerecord:
|
69
|
+
attributes:
|
70
|
+
user:
|
71
|
+
email: ""
|
72
|
+
encrypted_password: ""
|
73
|
+
reset_password_token: ""
|
74
|
+
reset_password_sent_at: ""
|
75
|
+
...
|
76
|
+
```
|
77
|
+
|
78
|
+
If that file already exists, it will not override the existing translations, it will only add the missing ones.
|
79
|
+
|
80
|
+
### Options
|
81
|
+
|
82
|
+
`write_to_locales:` accepts array of locales for which you want to generate the files. `RailsTranslations.build_attributes_placeholders(write_to_locales: [:hr])` will only generate `models_fields.hr.yml`.
|
83
|
+
|
84
|
+
`file_name:` define the file name, default value is `"models_fields"`.
|
85
|
+
|
86
|
+
## Future improvements
|
87
|
+
|
88
|
+
- Use Thor to make it executable from command line.
|
89
|
+
- Pass model names to `build_attributes_placeholders` method to avoid repeteadly removing unwanted translation placeholders.
|
90
|
+
- Add module for editing translations in browser.
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/vedranmijatovic0/rails_translations
|
95
|
+
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails_translations"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/custom_hash.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module CustomHash
|
2
|
+
refine Hash do
|
3
|
+
|
4
|
+
def set_deep_values(new_value)
|
5
|
+
self.keys.each do |key|
|
6
|
+
self[key].is_a?(Hash) ? self[key].set_deep_values(new_value) : self[key] = new_value
|
7
|
+
end
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def prefix_deep_values_with(prefix="TODO:")
|
12
|
+
self.keys.each do |key|
|
13
|
+
self[key].is_a?(Hash) ? self[key].prefix_deep_values_with(prefix) : self[key] = "#{prefix} #{self[key]}"
|
14
|
+
end
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_yml(identation=0)
|
19
|
+
yml = ""
|
20
|
+
self.each do |key, value|
|
21
|
+
key = "\"#{key}\"" if key.in? yml_reserved_words
|
22
|
+
if value.is_a?(Hash)
|
23
|
+
yml += "#{ " " * identation }#{key}:\n#{value.to_yml(identation+1)}"
|
24
|
+
else
|
25
|
+
yml += "#{ " " * identation }#{key}: \"#{value.gsub('"', '\\"')}\"\n"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
yml
|
29
|
+
end
|
30
|
+
|
31
|
+
def yml_reserved_words
|
32
|
+
[
|
33
|
+
"y", "Y", "yes", "Yes", "YES", "n", "N", "no",
|
34
|
+
"No", "NO", "true", "True", "TRUE", "false", "False",
|
35
|
+
"FALSE", "on", "On", "ON", "off", "Off", "OFF"
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require "rails_translations/version"
|
2
|
+
require "custom_hash"
|
3
|
+
|
4
|
+
module RailsTranslations
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
using CustomHash
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def equilize_translations(options={})
|
12
|
+
file_names = options[:file_names] ? options[:file_names] : get_translation_file_names
|
13
|
+
file_names.each do |file_name|
|
14
|
+
equilize_files(file_name, options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def equilize_files(file_name, options={})
|
19
|
+
equilize_locales = options[:equilize_locales] ? options[:equilize_locales] : I18n.available_locales
|
20
|
+
take_from_locales = options[:take_from_locales] ? options[:take_from_locales] : I18n.available_locales
|
21
|
+
prefix = options[:prefix].blank? ? nil : options[:prefix]
|
22
|
+
|
23
|
+
equilize_locales.each do |locale|
|
24
|
+
keys = load_keys(file_name, locale)
|
25
|
+
other_locales = take_from_locales - [ locale ]
|
26
|
+
other_locales.each do |other_locale|
|
27
|
+
other_keys = load_keys(file_name, other_locale).prefix_deep_values_with()
|
28
|
+
keys = other_keys.deep_merge(keys)
|
29
|
+
end
|
30
|
+
write_to_yml_file(file_name, locale, keys)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_attributes_placeholders(options={})
|
35
|
+
models_attributes = collect_attributes_from_models
|
36
|
+
write_to_locales = options[:write_to_locales] ? options[:write_to_locales] : I18n.available_locales
|
37
|
+
|
38
|
+
file_name = options[:file_name].blank? ? 'models_fields' : options[:file_name]
|
39
|
+
|
40
|
+
write_to_locales.each do |locale|
|
41
|
+
write_to_yml_file(
|
42
|
+
file_name,
|
43
|
+
locale,
|
44
|
+
models_attributes.deep_merge(load_keys(file_name, locale))
|
45
|
+
)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_translations_files_list
|
50
|
+
get_translations_files_paths.map{ |f| f.split(File::SEPARATOR).last }
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def collect_attributes_from_models(options={})
|
56
|
+
models_attributes = { activerecord: { attributes: {} }}
|
57
|
+
application_models.each do |model|
|
58
|
+
attributes = model.new.attributes.keys - ["created_at", "updated_at", "id"]
|
59
|
+
if attributes.any?
|
60
|
+
model_attributes = {}
|
61
|
+
(attributes).each do |key|
|
62
|
+
model_attributes.merge!({ :"#{key}" => '' })
|
63
|
+
end
|
64
|
+
models_attributes[:activerecord][:attributes][model.name.underscore] = model_attributes
|
65
|
+
end
|
66
|
+
end
|
67
|
+
models_attributes.deep_stringify_keys
|
68
|
+
end
|
69
|
+
|
70
|
+
def application_models
|
71
|
+
Rails.application.eager_load!
|
72
|
+
(ActiveRecord::Base.descendants - [ApplicationRecord])
|
73
|
+
end
|
74
|
+
|
75
|
+
def load_translation_file(file_name)
|
76
|
+
begin
|
77
|
+
YAML::load(File.read(File.join(Rails.root, 'config', 'locales', file_name)))
|
78
|
+
rescue Errno::ENOENT
|
79
|
+
{}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def get_translations_files_paths
|
84
|
+
Dir.glob(File.join("config", "locales", "*"))
|
85
|
+
end
|
86
|
+
|
87
|
+
def get_translation_file_names
|
88
|
+
get_translations_files_list.map{ |f| f.gsub(f.split(".")[-2..-1].join("."), "").sub(/\.$/, "") }.flatten.uniq
|
89
|
+
end
|
90
|
+
|
91
|
+
def load_keys(file_name, locale)
|
92
|
+
load_translation_file([file_name, locale, 'yml'].reject(&:blank?).join("."))[locale.to_s].to_h
|
93
|
+
end
|
94
|
+
|
95
|
+
def write_to_yml_file(file_name, locale, keys)
|
96
|
+
file = File.join(Rails.root, 'config', 'locales', [ file_name, locale, "yml" ].reject(&:blank?).join("."))
|
97
|
+
file_keys = { :"#{locale}" => keys }
|
98
|
+
open(file, 'w') do |f|
|
99
|
+
f.puts file_keys.to_yml
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/rails_translations/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "rails_translations"
|
5
|
+
spec.version = RailsTranslations::VERSION
|
6
|
+
spec.authors = ["vedraninho"]
|
7
|
+
spec.email = ["vedran.mijatovic@bamboolab.eu"]
|
8
|
+
|
9
|
+
spec.summary = "Sync your translations files and generate file with model attributes"
|
10
|
+
spec.homepage = "https://gitlab.com/vedranmijatovic0/rails_translations"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
#spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_translations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vedraninho
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- vedran.mijatovic@bamboolab.eu
|
16
|
+
executables:
|
17
|
+
- rails_translations
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- exe/rails_translations
|
30
|
+
- lib/custom_hash.rb
|
31
|
+
- lib/rails_translations.rb
|
32
|
+
- lib/rails_translations/version.rb
|
33
|
+
- rails_translations.gemspec
|
34
|
+
homepage: https://gitlab.com/vedranmijatovic0/rails_translations
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://gitlab.com/vedranmijatovic0/rails_translations
|
39
|
+
source_code_uri: https://gitlab.com/vedranmijatovic0/rails_translations
|
40
|
+
changelog_uri: https://gitlab.com/vedranmijatovic0/rails_translations
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.3.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.0.8
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Sync your translations files and generate file with model attributes
|
60
|
+
test_files: []
|