Skip to content

Commit

Permalink
version 7.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed May 10, 2024
1 parent 66efeca commit bced5bd
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 14 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require:
AllCops:
TargetRubyVersion: 3.0
NewCops: enable
SuggestExtensions: false
Exclude:
- 'spec/dummy/bin/*'
- vendor/**
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.0.1

* Update documentation, minor code tweaks

## 7.0.0 (09-Nov-2023)

* **Breaking change**: require Ruby 3+. Version 2.7 has reached end-of-life and thus we are not planning to support it anymore. If you need support for Ruby 2.7, please stay on 6.0.0.
Expand Down
13 changes: 10 additions & 3 deletions lib/generators/lokalise_rails/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
require 'rails/generators'

module LokaliseRails
# Generators for LokaliseRails
module Generators
# Installs LokaliseRails config
# Generator that sets up the LokaliseRails configuration in a Rails application.
# It copies a predefined configuration template into the appropriate directory within the Rails application,
# making it easier for users to configure and use LokaliseRails.
class InstallGenerator < Rails::Generators::Base
# Sets the directory containing the template files relative to this file's location.
source_root File.expand_path('../templates', __dir__)

desc 'Creates a LokaliseRails config file.'
# Description of the generator's purpose, which is displayed in the Rails generators list.
desc 'Creates a LokaliseRails config file in your Rails application.'

# The primary method of this generator, responsible for copying the LokaliseRails configuration template
# from the gem to the Rails application's config directory. This is where users can customize
# their Lokalise settings.
def copy_config
# Copies the configuration template to the Rails application's config directory.
template 'lokalise_rails_config.rb', "#{Rails.root}/config/lokalise_rails.rb"
end
end
Expand Down
13 changes: 9 additions & 4 deletions lib/lokalise_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
require 'zeitwerk'
require 'lokalise_manager'

# Configure Zeitwerk loader specific for gem environments. This loader is set up to ignore certain files
# that should not be autoloaded, such as Rails-specific files or templates which are not part of the main load path.
loader = Zeitwerk::Loader.for_gem
loader.ignore "#{__dir__}/lokalise_rails/railtie.rb"
loader.ignore "#{__dir__}/generators/templates/lokalise_rails_config.rb"
loader.ignore "#{__dir__}/generators/lokalise_rails/install_generator.rb"
loader.ignore "#{__dir__}/lokalise_rails/railtie.rb" # Ignore the Railtie in non-Rails environments
loader.ignore "#{__dir__}/generators/templates/lokalise_rails_config.rb" # Ignore the generator templates
loader.ignore "#{__dir__}/generators/lokalise_rails/install_generator.rb" # Ignore installation generator scripts
loader.setup

# Main LokaliseRails module
# Main module for the LokaliseRails gem. This module serves as the namespace for all components
# related to the LokaliseRails integration. It provides a structured way to manage translations
# through the Lokalise platform within Ruby on Rails applications.
module LokaliseRails
end

# Require the Railtie only if Rails is defined to integrate with Rails without manual configuration.
require_relative 'lokalise_rails/railtie' if defined?(Rails)
9 changes: 8 additions & 1 deletion lib/lokalise_rails/global_config.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# frozen_string_literal: true

module LokaliseRails
# Global configuration, based on LokaliseManager config
# Inherits from LokaliseManager::GlobalConfig to provide a global configuration specific to the LokaliseRails gem.
# This class is primarily used to manage configuration settings that affect how the LokaliseRails gem operates
# within a Ruby on Rails application, particularly in managing locale paths.
class GlobalConfig < LokaliseManager::GlobalConfig
class << self
# Provides the path to the locales directory where translation files are stored. If not set explicitly,
# it defaults to the `config/locales` directory within the root of the application using this gem.
#
# @return [String] the path to the locales directory
def locales_path
# If @locales_path is not set, it defaults to a path under the application's root directory.
@locales_path || "#{LokaliseRails::Utils.root}/config/locales"
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/lokalise_rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# frozen_string_literal: true

module LokaliseRails
# Load Rake tasks
# The Railtie class in Rails is used to extend Rails' functionality within an application, or in this case,
# a gem. This Railtie is specifically used to add custom Rake tasks from the
# LokaliseRails gem into the Rails application.
#
# It leverages Rails' Railtie architecture to ensure the Rake
# tasks are loaded when the application boots up and Rake is invoked.
class Railtie < Rails::Railtie
# Register Rake tasks that are defined within this gem. This block is called by Rails during the initialization
# process and ensures that all Rake tasks specific to LokaliseRails are available to the application.
rake_tasks do
# Loads the Rake tasks from a file located relative to this file. Ensure this file exists and contains
# valid Rake task definitions specifically tailored for Lokalise integration.
load 'tasks/lokalise_rails_tasks.rake'
end
end
Expand Down
16 changes: 14 additions & 2 deletions lib/lokalise_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@ module LokaliseRails
# Util methods
module Utils
class << self
# Current project root
# Retrieves the root directory of the current project.
# It attempts to find the Rails root directory if Rails is loaded.
# If Rails is not present, it defaults to the current working directory of the process.
#
# @return [Pathname] A Pathname object pointing to the root directory of the project.
def root
# Uses Pathname to create a robust path object from the rails_root or the current working directory.
Pathname.new(rails_root || Dir.getwd)
end

# Tries to get Rails root if Rails is defined
# Attempts to determine the root directory of a Rails
# project by checking the presence of Rails and its root method.
# If Rails is older and does not have the root method, it falls back to the RAILS_ROOT constant if defined.
#
# @return [String, nil] the path to the root directory if Rails is defined, or nil if it cannot be determined.
def rails_root
# First, check if Rails.root is defined and return its path if available.
return ::Rails.root.to_s if defined?(::Rails.root) && ::Rails.root
# Fallback to the RAILS_ROOT constant from older Rails versions.
return RAILS_ROOT.to_s if defined?(RAILS_ROOT)

# Returns nil if none of the above are defined, indicating Rails is not present.
nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lokalise_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module LokaliseRails
VERSION = '7.0.0'
VERSION = '7.0.1'
end
15 changes: 13 additions & 2 deletions lib/tasks/lokalise_rails_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@

require 'rake'
require 'lokalise_rails'
# Loads the configuration settings from the lokalise_rails configuration file within the Rails project.
require "#{LokaliseRails::Utils.root}/config/lokalise_rails"

# Namespace for Rake tasks related to the LokaliseRails gem.
# These tasks facilitate the synchronization of localization files between the Rails project and the Lokalise platform.
namespace :lokalise_rails do
# Imports translation files from Lokalise and integrates them into the current Rails project.
# This task utilizes the LokaliseManager to handle the import process according to the configuration specified
# in the GlobalConfig class.
desc 'Imports translation files from Lokalise to the current Rails project'
task :import do
importer = LokaliseManager.importer({}, LokaliseRails::GlobalConfig)
importer.import!
rescue StandardError => e
abort e.inspect
# Aborts the task and prints the error message.
# Ensures that any exceptions raised during import are handled gracefully.
abort "Import failed: #{e.message}"
end

# Exports translation files from the current Rails project to Lokalise.
# Similar to the import task, it leverages the LokaliseManager and uses the GlobalConfig for configuration settings.
desc 'Exports translation files from the current Rails project to Lokalise'
task :export do
exporter = LokaliseManager.exporter({}, LokaliseRails::GlobalConfig)
exporter.export!
rescue StandardError => e
abort e.inspect
# Aborts the task and prints the error message. Provides clear feedback on why the export failed.
abort "Export failed: #{e.message}"
end
end
1 change: 1 addition & 0 deletions spec/dummy/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
Expand Down

0 comments on commit bced5bd

Please sign in to comment.