localeui 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34ebbd165eaa32936d46e399b8300910a67df1372dfcd7afe305ffb30b862682
4
+ data.tar.gz: 2b848e39e1b5a84614c110998103966792291aff6a9c7e9b17688c5207681faf
5
+ SHA512:
6
+ metadata.gz: 86d70ebe060920ce041f2731cbed9aa65bbcd4b86fa4e6130e48880ef8d4113c447b148218cd4722190428d6c0c0fefd61df9d6261a14a95297cd48eb8bab52c
7
+ data.tar.gz: 94bbf3d795a02511b45209b276f3810e99a687c1fa9661a620faef1ac0155cdc9b39eaf672e9e358bc1d38538a2338b50adeb168692a93c2cb30377efde48ba6
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # LocaleUIRails
2
+
3
+ ![Gem Version](https://img.shields.io/gem/v/localeui-rails)
4
+ ![CI Status](https://github.com/localeUI/localeui-rails/actions/workflows/main.yml/badge.svg)
5
+
6
+ The LocaleUI-Rails Gem is a Ruby on Rails application gem that provides developers with a seamless way to manage and synchronise translation files with LocaleUI.com. By integrating the Gem into the Rails application, translations can be automatically synchronised, updated and deployed directly to LocaleUI without manually uploading or copying language files.
7
+
8
+ The LocaleUI-Rails Gem makes the management of multilingual Ruby on Rails applications uncomplicated and automated - ideal for companies that want to offer their Ruby on Rails application in different languages.
9
+
10
+ ## Requirements
11
+
12
+ This gem requires Ruby 3.2+ and Rails 7.1+.
13
+
14
+ ## Installation
15
+
16
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
17
+
18
+ Install the gem and add to the application's Gemfile by executing:
19
+
20
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+
22
+ If bundler is not being used to manage dependencies, install the gem by executing:
23
+
24
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
25
+
26
+ Next, you need to create a config file for localeui-rails. Run the following command to copy a basic config file.
27
+
28
+ $ rails g localeui:install
29
+
30
+ Go to the config file `config/initializers/localeui.rb` and add your `api_token` and `project_id`.
31
+
32
+ ```ruby
33
+ Localeui.config do |config|
34
+ # ...
35
+
36
+ # Authentication Token
37
+ config.api_token = ENV['LOCALEUID_API_TOKEN']
38
+
39
+ # Project API ID
40
+ config.project_id = ENV['LOCALEUID_PROJECT_ID']
41
+
42
+ # ...other options
43
+ end
44
+ ```
45
+
46
+ ## Upload Translation Files
47
+
48
+ $ rails localeui:upload
49
+
50
+ ## Download Translation Files
51
+
52
+ $ rails localeui:download
53
+
54
+ ## Development
55
+
56
+ Run tests with in the terminal
57
+
58
+ $ bin/test
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/localeUI/localeui_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/localeUI/localeui-rails/blob/main/CODE_OF_CONDUCT.md).
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
67
+
68
+ ## Code of Conduct
69
+
70
+ Everyone interacting in the LocaleuiRails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/localeUI/localeui-rails/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop/rake_task'
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: %i[test rubocop]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Localeui
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../templates', __dir__)
9
+
10
+ desc 'Creates a LocaleUI config file.'
11
+ def copy_config
12
+ template 'localeui_config.rb', "#{Rails.root}/config/initializers/localeui.rb"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'localeui'
4
+
5
+ Localeui.config do |config|
6
+ # These are mandatory parameters that must be set before rake tasks are executed
7
+ # Base API Endpoint
8
+ config.base_api = 'http://localhost:3000/api/v1/'
9
+
10
+ # Authentication Token
11
+ config.api_token = ''
12
+
13
+ # Project API ID
14
+ config.project_id = ''
15
+
16
+ # Path of the translation files
17
+ config.locales_path = "#{Rails.root}/config/locales"
18
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class LocaleuiError < StandardError; end
5
+ class BadRequestError < LocaleuiError; end
6
+ class ForbiddenError < LocaleuiError; end
7
+ class NoBaseApiError < LocaleuiError; end
8
+ class NoApiTokenError < LocaleuiError; end
9
+ class NoProjectError < LocaleuiError; end
10
+ class NotFoundError < LocaleuiError; end
11
+ class UnauthorizedError < LocaleuiError; end
12
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class Http
5
+ def self.request(method:, endpoint:, params: {})
6
+ request = Localeui::Request.new
7
+ request.send(method, endpoint, params)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'tasks/localeui_tasks.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+
5
+ module Localeui
6
+ class Request
7
+ def send(method, endpoint, params)
8
+ http_request = HTTP.headers(header)
9
+ response = http_request.public_send(method, url(endpoint), form: params)
10
+ Localeui::Response.new(http_request, response)
11
+ end
12
+
13
+ private
14
+
15
+ def url(endpoint)
16
+ raise NoBaseApiError, 'No api url set' if Localeui.base_api.nil? || Localeui.base_api.empty?
17
+
18
+ "#{Localeui.base_api}#{endpoint}"
19
+ end
20
+
21
+ def header
22
+ raise NoApiTokenError, 'No api token set' if Localeui.api_token.nil? || Localeui.api_token.empty?
23
+
24
+ { Authorization: "Bearer #{Localeui.api_token}" }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class Event
5
+ def self.create
6
+ raise NoProjectError, 'No project initialised' if Localeui.project_id.nil? || Localeui.project_id.empty?
7
+
8
+ response = Localeui::Http.request(
9
+ method: :post,
10
+ endpoint: "projects/#{Localeui.project_id}/events"
11
+ )
12
+ Localeui::Utils.logger.info "Event '#{response.body['api_id']}' was successfully created"
13
+ response
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class Project
5
+ def self.info
6
+ # show project informations
7
+ raise NoProjectError, 'No project initialised' if Localeui.project_id.nil? || Localeui.project_id.empty?
8
+
9
+ response = Localeui::Http.request(
10
+ method: :get,
11
+ endpoint: "projects/#{Localeui.project_id}"
12
+ )
13
+ Localeui::Utils.logger.info "Project '#{response.body['api_id']}' informations were successfully requested"
14
+ response
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class SourceFile
5
+ def self.upload
6
+ raise NoProjectError, 'No project initialised' if Localeui.project_id.nil?
7
+
8
+ # Get all source files
9
+ files = all
10
+
11
+ # Create event
12
+ event = Localeui::Event.create
13
+
14
+ # Submit each file
15
+ files.each do |file|
16
+ file_upload(file, event)
17
+ end
18
+
19
+ { event: event.body['api_id'], files: files.count }
20
+ end
21
+
22
+ def self.download
23
+ raise NoProjectError, 'No project initialised' if Localeui.project_id.nil?
24
+
25
+ # TODO: think about, if we want to create an event
26
+
27
+ # Request all source files
28
+ files_response = files_to_download
29
+
30
+ # Request all locales
31
+ locales_response = locales_to_download
32
+
33
+ # Request each source file with file content
34
+ locales_response.body.each do |locale|
35
+ files_response.body.each do |file|
36
+ downloaded_file_response = download_file(file, locale)
37
+ store_file(downloaded_file_response)
38
+ end
39
+ end
40
+ end
41
+
42
+ def self.files_to_download
43
+ response = Localeui::Http.request(
44
+ method: :get, endpoint: "projects/#{Localeui.project_id}/source_files"
45
+ )
46
+ Localeui::Utils.logger.info "#{response.body.count} files will be downloaded"
47
+ response
48
+ end
49
+
50
+ def self.locales_to_download
51
+ response = Localeui::Http.request(
52
+ method: :get, endpoint: "projects/#{Localeui.project_id}/locales"
53
+ )
54
+ Localeui::Utils.logger.info "#{response.body.count} locale(s) available"
55
+ response
56
+ end
57
+
58
+ def self.store_file(file) # rubocop:disable Metrics/AbcSize
59
+ if Rails.env.test?
60
+ Localeui::Utils.logger.info "Skipping file storage for '#{file.body['name']}' in test environment"
61
+ return
62
+ end
63
+
64
+ file_path = [Localeui.locales_path, file.body['name']].join('/')
65
+ File.open file_path, 'w' do |f|
66
+ f.write YAML.dump(file.body['content'])
67
+ f.close
68
+ end
69
+ Localeui::Utils.logger.info "File '#{file.body['name']}' has been successfully stored under '#{file_path}'"
70
+ end
71
+
72
+ def self.download_file(file, locale)
73
+ response = Localeui::Http.request(
74
+ method: :get,
75
+ endpoint: "projects/#{Localeui.project_id}/source_files/#{file['api_id']}?locale_id=#{locale['api_id']}"
76
+ )
77
+ Localeui::Utils.logger.info "File '#{response.body['name']}' has been successfully downloaded"
78
+ response
79
+ end
80
+
81
+ def self.file_upload(file, event)
82
+ file_path = [Localeui.locales_path, file].join('/')
83
+
84
+ response = Localeui::Http.request(
85
+ method: :post, endpoint: "projects/#{Localeui.project_id}/source_files", params: {
86
+ filename: file, event_id: event.body['api_id'],
87
+ content: YAML.load_file(file_path).to_json
88
+ }
89
+ )
90
+ Localeui::Utils.logger.info "File '#{file}' has been successfully uploaded"
91
+ response
92
+ end
93
+
94
+ def self.all
95
+ files = Dir.glob("#{Localeui.locales_path}/**/*").select { |file| File.file?(file) }
96
+ files.map { |file| file[Rails.root.join('config/locales/').to_s.length..] }
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ class Response
5
+ module StatusCodes
6
+ HTTP_OK_CODE = 200
7
+ HTTP_CREATED_CODE = 201
8
+ HTTP_BAD_REQUEST_CODE = 400
9
+ HTTP_UNAUTHORIZED_CODE = 401
10
+ HTTP_FORBIDDEN_CODE = 403
11
+ HTTP_NOT_FOUND_CODE = 404
12
+ HTTP_UNPROCESSABLE_ENTITY_CODE = 429
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'localeui/response/status_codes'
4
+
5
+ module Localeui
6
+ class Response
7
+ include StatusCodes
8
+
9
+ attr_reader :request, :response, :headers, :status, :body
10
+
11
+ def initialize(request, response)
12
+ @request = request
13
+ @response = response
14
+ init_header
15
+ init_body
16
+ identifiy_errors
17
+ end
18
+
19
+ def successful?
20
+ [HTTP_OK_CODE, HTTP_CREATED_CODE].include?(@status)
21
+ end
22
+
23
+ private
24
+
25
+ def init_header
26
+ @headers = @response.headers
27
+ @status = @response.status
28
+ end
29
+
30
+ def init_body
31
+ @body = JSON.parse(@response.body)
32
+ end
33
+
34
+ def identifiy_errors
35
+ return nil if successful?
36
+
37
+ raise_error
38
+ end
39
+
40
+ def raise_error
41
+ error = error_class.new(@body)
42
+ raise error
43
+ end
44
+
45
+ def error_class # rubocop:disable Metrics/MethodLength
46
+ case @status
47
+ when HTTP_BAD_REQUEST_CODE
48
+ BadRequestError
49
+ when HTTP_UNAUTHORIZED_CODE
50
+ UnauthorizedError
51
+ when HTTP_FORBIDDEN_CODE
52
+ ForbiddenError
53
+ when HTTP_NOT_FOUND_CODE
54
+ NotFoundError
55
+ else
56
+ LocaleuiError
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Lint/EmptyClass
4
+
5
+ module Localeui
6
+ module Task
7
+ class Base
8
+ end
9
+ end
10
+ end
11
+
12
+ # rubocop:enable Lint/EmptyClass
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ module Task
5
+ class Download < Base
6
+ class << self
7
+ def download!
8
+ Localeui::SourceFile.download
9
+ Localeui::Utils.logger.info 'Task complete!'
10
+ true
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ module Task
5
+ class ProjectInfo < Base
6
+ class << self
7
+ def project_info!
8
+ response = Localeui::Project.info
9
+ pp response.body
10
+ true
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ module Task
5
+ class Upload < Base
6
+ class << self
7
+ def upload!
8
+ Localeui::SourceFile.upload
9
+ Localeui::Utils.logger.info 'Task complete!'
10
+ true
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module Localeui
6
+ module Utils
7
+ class << self
8
+ def logger
9
+ Logger.new($stdout)
10
+ end
11
+
12
+ def root
13
+ Pathname.new(rails_root || Dir.getwd)
14
+ end
15
+
16
+ def rails_root
17
+ return ::Rails.root.to_s if defined?(::Rails.root) && ::Rails.root
18
+ return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
19
+
20
+ nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Localeui
4
+ VERSION = '0.1.0'
5
+ end
data/lib/localeui.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Version
4
+ require 'localeui/version'
5
+
6
+ # Resources
7
+ require 'localeui/resources/event'
8
+ require 'localeui/resources/project'
9
+ require 'localeui/resources/source_file'
10
+
11
+ # Supporting classes
12
+ require 'localeui/errors'
13
+ require 'localeui/http'
14
+ require 'localeui/request'
15
+ require 'localeui/response'
16
+ require 'localeui/utils'
17
+
18
+ # Tasks classes
19
+ require 'localeui/task/base'
20
+ require 'localeui/task/download'
21
+ require 'localeui/task/project_info'
22
+ require 'localeui/task/upload'
23
+ require 'localeui/railtie' if defined?(Rails)
24
+
25
+ module Localeui
26
+ class << self
27
+ attr_accessor :base_api, :api_token, :project_id, :locales_path, :allow_overwriting
28
+
29
+ def config
30
+ yield self
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'localeui'
5
+
6
+ namespace :localeui do
7
+ desc 'Upload all translation source files to localeUI'
8
+ task upload: :environment do
9
+ Localeui::Task::Upload.upload!
10
+ end
11
+
12
+ desc 'Display project information from localeUI'
13
+ task project_info: :environment do
14
+ Localeui::Task::ProjectInfo.project_info!
15
+ end
16
+
17
+ desc 'Download all translation source files to localeUI'
18
+ task download: :environment do
19
+ Localeui::Task::Download.download!
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localeui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lars Klindwordt
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: http
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '5.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '5.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: optparse
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.4'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.4'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rake
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '13.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ description: This gem allows to integrate LocaleUI to your locale Ruby on Rails application
55
+ email:
56
+ - lars.klindwordt@klind.de
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - README.md
61
+ files:
62
+ - README.md
63
+ - Rakefile
64
+ - lib/generators/localeui/install_generator.rb
65
+ - lib/generators/templates/localeui_config.rb
66
+ - lib/localeui.rb
67
+ - lib/localeui/errors.rb
68
+ - lib/localeui/http.rb
69
+ - lib/localeui/railtie.rb
70
+ - lib/localeui/request.rb
71
+ - lib/localeui/resources/event.rb
72
+ - lib/localeui/resources/project.rb
73
+ - lib/localeui/resources/source_file.rb
74
+ - lib/localeui/response.rb
75
+ - lib/localeui/response/status_codes.rb
76
+ - lib/localeui/task/base.rb
77
+ - lib/localeui/task/download.rb
78
+ - lib/localeui/task/project_info.rb
79
+ - lib/localeui/task/upload.rb
80
+ - lib/localeui/utils.rb
81
+ - lib/localeui/version.rb
82
+ - lib/tasks/localeui_tasks.rake
83
+ homepage: https://localeui.com/en/docs/rails_gem/get-started
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ homepage_uri: https://localeui.com/en/docs/rails_gem/get-started
88
+ source_code_uri: https://github.com/localeUI/localeui-rails
89
+ changelog_uri: https://github.com/localeUI/localeui-rails/blob/main/CHANGELOG.md
90
+ rubygems_mfa_required: 'true'
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 3.0.0
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.7.2
106
+ specification_version: 4
107
+ summary: LocaleUI Gem for Ruby on Rails applications
108
+ test_files: []