lokalise_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.github/CODE_OF_CONDUCT.md +46 -0
  3. data/.github/CONTRIBUTING.md +14 -0
  4. data/.github/PULL_REQUEST_TEMPLATE.md +11 -0
  5. data/CHANGELOG.md +5 -0
  6. data/Gemfile +9 -0
  7. data/LICENSE +22 -0
  8. data/README.md +92 -0
  9. data/Rakefile +21 -0
  10. data/lib/generators/lokalise_rails/install_generator.rb +17 -0
  11. data/lib/generators/templates/lokalise_rails_config.rb +29 -0
  12. data/lib/lokalise_rails.rb +31 -0
  13. data/lib/lokalise_rails/railtie.rb +9 -0
  14. data/lib/lokalise_rails/task_definition/base.rb +19 -0
  15. data/lib/lokalise_rails/task_definition/importer.rb +51 -0
  16. data/lib/lokalise_rails/version.rb +5 -0
  17. data/lokalise_rails.gemspec +44 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +4 -0
  20. data/spec/dummy/app/models/application_record.rb +5 -0
  21. data/spec/dummy/config/application.rb +34 -0
  22. data/spec/dummy/config/boot.rb +5 -0
  23. data/spec/dummy/config/environment.rb +7 -0
  24. data/spec/dummy/config/environments/development.rb +56 -0
  25. data/spec/dummy/config/environments/production.rb +100 -0
  26. data/spec/dummy/config/environments/test.rb +40 -0
  27. data/spec/dummy/config/initializers/application_controller_renderer.rb +9 -0
  28. data/spec/dummy/config/initializers/assets.rb +14 -0
  29. data/spec/dummy/config/initializers/backtrace_silencers.rb +8 -0
  30. data/spec/dummy/config/initializers/content_security_policy.rb +29 -0
  31. data/spec/dummy/config/initializers/cookies_serializer.rb +7 -0
  32. data/spec/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  33. data/spec/dummy/config/initializers/inflections.rb +17 -0
  34. data/spec/dummy/config/initializers/lokalise_rails.rb +29 -0
  35. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  36. data/spec/dummy/config/initializers/wrap_parameters.rb +16 -0
  37. data/spec/dummy/config/puma.rb +40 -0
  38. data/spec/dummy/config/routes.rb +5 -0
  39. data/spec/dummy/db/seeds.rb +8 -0
  40. data/spec/lib/generators/lokalise_rails/install_generator_spec.rb +19 -0
  41. data/spec/lib/lokalise_rails/importer_spec.rb +30 -0
  42. data/spec/lib/lokalise_rails_spec.rb +36 -0
  43. data/spec/lib/tasks/import_task_spec.rb +82 -0
  44. data/spec/spec_helper.rb +33 -0
  45. data/spec/support/file_utils.rb +27 -0
  46. data/spec/support/rake_utils.rb +11 -0
  47. data/spec/support/vcr.rb +11 -0
  48. metadata +317 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0d1228cffa0e9cf5ca3a149942246d942478e42c0d9f8e56c9a86273077aaa7e
4
+ data.tar.gz: 25885db67dfd0626bb7f15528a8bb049377202da1da8ccb114b1fa6191d39d1b
5
+ SHA512:
6
+ metadata.gz: f05d5b99e4de60ab91490f9085950e8e8ab25291be7ff350126966c35664fd37a14c996d4d6bd4351d228ab13125479c792b6380ebab1eeb7531559b27b07a4e
7
+ data.tar.gz: 2a95e4eb1d9959fbd1ce03243c4ef7c59aca8bf2a424c33bc5da7e0e884784b4f0dc5286e02a5233e66f961dea3d024acccb33f5707b0e262bd00e2ceeb7b0c1
@@ -0,0 +1,46 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to creating a positive environment include:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+ * Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
20
+ * Trolling, insulting/derogatory comments, and personal or political attacks
21
+ * Public or private harassment
22
+ * Publishing others' private information, such as a physical or electronic address, without explicit permission
23
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Our Responsibilities
26
+
27
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28
+
29
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ ## Scope
32
+
33
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34
+
35
+ ## Enforcement
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at hello@lokalise.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38
+
39
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40
+
41
+ ## Attribution
42
+
43
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44
+
45
+ [homepage]: http://contributor-covenant.org
46
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,14 @@
1
+ # Contributing
2
+
3
+ 1. [Fork the repository.][fork]
4
+ 2. [Create a topic branch.][branch]
5
+ 3. Implement your feature or bug fix.
6
+ 4. Don't forget to add specs and make sure they pass by running `rspec .`.
7
+ 5. Make sure your code complies with the style guide by running `rubocop`. `rubocop -a` can automatically fix most issues for you.
8
+ 6. If necessary, add documentation for your feature or bug fix.
9
+ 7. Commit and push your changes.
10
+ 8. [Submit a pull request.][pr]
11
+
12
+ [fork]: http://help.github.com/fork-a-repo/
13
+ [branch]: https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-branches
14
+ [pr]: https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests
@@ -0,0 +1,11 @@
1
+ ### Summary
2
+
3
+ Provide a general description of the code changes in your pull
4
+ request. Were there any bugs you had fixed? If so, mention them. If
5
+ these bugs have open GitHub issues, be sure to tag them here as well,
6
+ to keep the conversation linked together.
7
+
8
+ ### Other Information
9
+
10
+ If there's anything else that's important and relevant to your pull
11
+ request, mention that information here.
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.0.1 (26-Sep-20)
4
+
5
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ group :test do
8
+ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Lokalise team, Ilya Bodrov
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ # LokaliseRails
2
+
3
+ <!-- [![Gem Version](https://badge.fury.io/rb/ruby-lokalise-api.svg)](https://badge.fury.io/rb/ruby-lokalise-api) -->
4
+ [![Build Status](https://travis-ci.org/bodrovis/lokalise_rails.svg?branch=master)](https://travis-ci.org/bodrovis/lokalise_rails)
5
+ [![Test Coverage](https://codecov.io/gh/bodrovis/lokalise_rails/graph/badge.svg)](https://codecov.io/gh/bodrovis/lokalise_rails)
6
+
7
+ This gem provides [Lokalise](http://lokalise.com) integration for Ruby on Rails and allows to exchange translation files easily. It relies on [ruby-lokalise-api](https://lokalise.github.io/ruby-lokalise-api) to send APIv2 requests.
8
+
9
+ ## Getting started
10
+
11
+ ### Requirements
12
+
13
+ This gem requires Ruby 2.5+ and Rails 5.1+. It might work with older versions of Rails though. You will also need to setup a Lokalise account and a translation project. Finally, you will need to generate a read/write API token at your Lokalise profile.
14
+
15
+ ### Installation
16
+
17
+ Add the gem to your `Gemfile`
18
+
19
+ ```ruby
20
+ gem 'lokalise_rails'
21
+ ```
22
+
23
+ and run
24
+
25
+ ```
26
+ bundle install
27
+ rails g lokalise_rails:install
28
+ ```
29
+
30
+ The latter command will generate a new initializer `lokalise_rails.rb` looking like this:
31
+
32
+ ```ruby
33
+ require 'lokalise_rails'
34
+
35
+ LokaliseRails.api_token = ENV['LOKALISE_API_TOKEN']
36
+ LokaliseRails.project_id = ENV['LOKALISE_PROJECT_ID']
37
+ # ...
38
+ ```
39
+
40
+ You have to provide `api_token` and `project_id` to proceed. [Other options can be customized as well (see below)](https://github.com/bodrovis/lokalise_rails#import-settings) but they have sensible defaults.
41
+
42
+ ## Importing translations from Lokalise
43
+
44
+ To import translations from the specified Lokalise project to your Rails app, run the following command:
45
+
46
+ ```
47
+ rails lokalise_rails:import
48
+ ```
49
+
50
+ Please note that any existing files inside the `locales` directory will be overwritten! You may enable [safe mode](https://github.com/bodrovis/lokalise_rails#import-settings) to check whether the folder is empty or not.
51
+
52
+ ## Configuration
53
+
54
+ Options are specified in the `config/initializers/lokalise_rails.rb` file.
55
+
56
+ ### Global settings
57
+
58
+ * `LokaliseRails.api_token` (`string`, required) - Lokalise API token with read/write permissions.
59
+ * `LokaliseRails.project_id` (`string`, required) - Lokalise project ID. You must have import/export permissions in the specified project.
60
+ * `locales_path` - method returning a string with the path to your translation files. Defaults to `"#{Rails.root}/config/locales"`. To provide a custom path, override the method inside the initializer (make sure that the path exists!):
61
+
62
+ ```ruby
63
+ class LokaliseRails
64
+ class << self
65
+ def locales_path
66
+ "#{Rails.root}/config/locales_custom"
67
+ end
68
+ end
69
+ end
70
+ ```
71
+
72
+ ### Import settings
73
+
74
+ * `LokaliseRails.import_opts` (`hash`) - options that will be passed to Lokalise API when downloading translations to your app. Here are the default options:
75
+
76
+ ```ruby
77
+ {
78
+ format: 'yaml',
79
+ placeholder_format: :icu,
80
+ yaml_include_root: true,
81
+ original_filenames: true,
82
+ directory_prefix: '',
83
+ indentation: '2sp'
84
+ }
85
+ ```
86
+
87
+ Full list of available options [can be found at the official API documentation](https://app.lokalise.com/api2docs/curl/#transition-download-files-post).
88
+ * `LokaliseRails.import_safe_mode` (`boolean`) - default to `false`. When this option is enabled, the import task will check whether the `locales` directory is empty or not. If it is not empty, you will be prompted to continue.
89
+
90
+ ## License
91
+
92
+ Copyright (c) [Lokalise team](http://lokalise.com), [Ilya Bodrov](http://bodrovis.tech)
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+
5
+ begin
6
+ require 'bundler/setup'
7
+ Bundler::GemHelper.install_tasks
8
+ rescue LoadError
9
+ puts 'although not required, bundler is recommened for running the tests'
10
+ end
11
+
12
+ task default: :spec
13
+
14
+ require 'rspec/core/rake_task'
15
+ RSpec::Core::RakeTask.new(:spec)
16
+
17
+ require 'rubocop/rake_task'
18
+ RuboCop::RakeTask.new do |task|
19
+ task.requires << 'rubocop-performance'
20
+ task.requires << 'rubocop-rspec'
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ class LokaliseRails
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../templates', __dir__)
9
+
10
+ desc 'Creates a LokaliseRails config file.'
11
+
12
+ def copy_config
13
+ template 'lokalise_rails_config.rb', "#{Rails.root}/config/initializers/lokalise_rails.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lokalise_rails'
4
+
5
+ # These are mandatory options that you must set before running rake tasks:
6
+ LokaliseRails.api_token = ENV['LOKALISE_API_TOKEN']
7
+ LokaliseRails.project_id = ENV['LOKALISE_PROJECT_ID']
8
+
9
+ # Import options have the following defaults:
10
+ # @import_opts = {
11
+ # format: 'yaml',
12
+ # placeholder_format: :icu,
13
+ # yaml_include_root: true,
14
+ # original_filenames: true,
15
+ # directory_prefix: '',
16
+ # indentation: '2sp'
17
+ # }
18
+
19
+ # Safe mode is disabled by default:
20
+ # @import_safe_mode = false
21
+
22
+ # Provide a custom path to the directory with your translation files:
23
+ # class LokaliseRails
24
+ # class << self
25
+ # def locales_path
26
+ # "#{Rails.root}/config/locales"
27
+ # end
28
+ # end
29
+ # end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lokalise_rails/task_definition/base'
4
+ require 'lokalise_rails/task_definition/importer'
5
+ require 'lokalise_rails/railtie' if defined?(Rails)
6
+
7
+ class LokaliseRails
8
+ @project_id = nil
9
+ @import_opts = {
10
+ format: 'yaml',
11
+ placeholder_format: :icu,
12
+ yaml_include_root: true,
13
+ original_filenames: true,
14
+ directory_prefix: '',
15
+ indentation: '2sp'
16
+ }
17
+ # @export_opts = {
18
+ #
19
+ # }
20
+ @import_safe_mode = false
21
+ @api_token = nil
22
+
23
+ class << self
24
+ attr_accessor :import_opts, :import_safe_mode, :api_token, :export_opts,
25
+ :project_id
26
+
27
+ def locales_path
28
+ "#{Rails.root}/config/locales"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LokaliseRails
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'tasks/lokalise_rails_tasks.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby-lokalise-api'
4
+ require 'open-uri'
5
+
6
+ class LokaliseRails
7
+ module TaskDefinition
8
+ class Base
9
+ class << self
10
+ def check_required_opts
11
+ return [false, 'Project ID is not set! Aborting...'] unless LokaliseRails.project_id
12
+ return [false, 'Lokalise API token is not set! Aborting...'] unless LokaliseRails.api_token
13
+
14
+ [true, '']
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LokaliseRails
4
+ module TaskDefinition
5
+ class Importer < Base
6
+ class << self
7
+ def import!
8
+ status_ok, msg = check_required_opts
9
+ return msg unless status_ok
10
+ return 'Task cancelled!' unless proceed_when_safe_mode?
11
+
12
+ open_and_process_zip download_files['bundle_url']
13
+
14
+ 'Task complete!'
15
+ end
16
+
17
+ def download_files
18
+ client = ::Lokalise.client LokaliseRails.api_token
19
+ opts = LokaliseRails.import_opts
20
+
21
+ client.download_files LokaliseRails.project_id, opts
22
+ end
23
+
24
+ def open_and_process_zip(path)
25
+ Zip::File.open_buffer(URI.open(path)) { |zip| process_zip zip }
26
+ end
27
+
28
+ def process_zip(zip)
29
+ zip.each do |entry|
30
+ next unless /\.ya?ml/.match?(entry.name)
31
+
32
+ filename = entry.name.include?('/') ? entry.name.split('/')[1] : entry.name
33
+ data = YAML.safe_load entry.get_input_stream.read
34
+ File.open("#{LokaliseRails.locales_path}/#{filename}", 'w+:UTF-8') do |f|
35
+ f.write(data.to_yaml)
36
+ end
37
+ end
38
+ end
39
+
40
+ def proceed_when_safe_mode?
41
+ return true unless LokaliseRails.import_safe_mode && !Dir.empty?(LokaliseRails.locales_path.to_s)
42
+
43
+ $stdout.puts "The target directory #{LokaliseRails.locales_path} is not empty!"
44
+ $stdout.print 'Enter Y to continue: '
45
+ answer = $stdin.gets
46
+ answer.to_s.strip == 'Y'
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LokaliseRails
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('lib/lokalise_rails/version', __dir__)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'lokalise_rails'
7
+ spec.version = LokaliseRails::VERSION
8
+ spec.authors = ['Ilya Bodrov']
9
+ spec.email = ['golosizpru@gmail.com']
10
+ spec.summary = 'Lokalise integration for Ruby on Rails'
11
+ spec.description = 'This gem allows to exchange translation files between your Rails app and Lokalise TMS.'
12
+ spec.homepage = 'https://github.com/bodrovis/lokalise_rails'
13
+ spec.license = 'MIT'
14
+ spec.platform = Gem::Platform::RUBY
15
+ spec.required_ruby_version = '>= 2.5.0'
16
+
17
+ spec.files = Dir['README.md', 'LICENSE',
18
+ 'CHANGELOG.md', 'lib/**/*.rb',
19
+ 'lokalise_rails.gemspec', '.github/*.md',
20
+ 'Gemfile', 'Rakefile']
21
+ spec.test_files = Dir['spec/**/*.rb']
22
+ spec.extra_rdoc_files = ['README.md']
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'ruby-lokalise-api', '~> 3.1'
26
+ spec.add_dependency 'rubyzip', '~> 2.3'
27
+
28
+ spec.add_development_dependency 'codecov', '~> 0.1'
29
+ spec.add_development_dependency 'dotenv', '~> 2.5'
30
+ if ENV['TEST_RAILS_VERSION'].nil?
31
+ spec.add_development_dependency 'rails', '~> 6.0.3'
32
+ else
33
+ spec.add_development_dependency 'rails', ENV['TEST_RAILS_VERSION'].to_s
34
+ end
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.6'
37
+ spec.add_development_dependency 'rspec-rails', '~> 4.0'
38
+ spec.add_development_dependency 'rubocop', '~> 0.60'
39
+ spec.add_development_dependency 'rubocop-performance', '~> 1.5'
40
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.37'
41
+ spec.add_development_dependency 'simplecov', '~> 0.16'
42
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
43
+ spec.add_development_dependency 'vcr', '~> 6.0'
44
+ end