RemoteTranslationLoader
RemoteTranslationLoader is a Ruby gem designed to dynamically fetch and load translation files (YAML format) into your Ruby or Ruby on Rails application. It supports multiple sources such as HTTP URLs, local files, and AWS S3, allowing you to seamlessly integrate external translations.
Features
- Fetch translations from multiple sources:
- HTTP URLs
- Local files
- AWS S3 buckets
- Supports deep merging of translations with existing
I18n
backend. - Namespace support for isolating translations.
- Dry-run mode to simulate translation loading.
- Rake tasks for easy integration with Rails applications.
- CLI tool for manual loading.
Installation
Add this line to your application's Gemfile:
gem 'remote_translation_loader'
And then execute:
bundle install
Or install it directly:
gem install remote_translation_loader
Usage
Basic Usage
1. HTTP Fetching
require 'remote_translation_loader'
urls = ['https://example.com/en.yml', 'https://example.com/fr.yml']
loader = RemoteTranslationLoader::Loader.new(urls)
loader.fetch_and_load
2. Local File Fetching
require 'remote_translation_loader'
files = ['/path/to/local/en.yml', '/path/to/local/fr.yml']
loader = RemoteTranslationLoader::Loader.new(files, fetcher: RemoteTranslationLoader::Fetchers::FileFetcher.new)
loader.fetch_and_load
3. AWS S3 Fetching
require 'remote_translation_loader'
bucket = 'your-s3-bucket'
s3_fetcher = RemoteTranslationLoader::Fetchers::S3Fetcher.new(bucket, region: 'us-east-1')
keys = ['translations/en.yml', 'translations/fr.yml']
loader = RemoteTranslationLoader::Loader.new(keys, fetcher: s3_fetcher)
loader.fetch_and_load
Advanced Options
Namespace Support
Add a namespace to group translations under a specific key:
loader.fetch_and_load(namespace: 'remote')
# Translations will be grouped under the `remote` key, e.g., `remote.en.some_key`
Dry-Run Mode
Simulate the loading process without modifying the I18n
backend:
loader.fetch_and_load(dry_run: true)
# Outputs fetched translations to the console without loading them
CLI Usage
Install the gem globally and use the CLI tool:
remote_translation_loader https://example.com/en.yml /path/to/local/fr.yml
- The CLI fetches and loads the specified translations.
- Add the executable to your
$PATH
for easier access.
Rails Integration
1. Rake Task
Use the provided Rake task to fetch translations in a Rails application:
Add this to your Rakefile
:
require 'remote_translation_loader'
load 'remote_translation_loader/tasks/remote_translation_loader.rake'
Run the task:
rake translations:load[https://example.com/en.yml,/path/to/local/fr.yml]
2. Automatic Loading
Add an initializer to load translations on application startup:
config/initializers/remote_translation_loader.rb
require 'remote_translation_loader'
urls = ['https://example.com/en.yml', '/path/to/local/fr.yml']
loader = RemoteTranslationLoader::Loader.new(urls)
loader.fetch_and_load(namespace: 'remote')
Contributing
We welcome contributions! Follow these steps:
- Fork the repository.
- Create your feature branch:
git checkout -b feature/my-new-feature
- Commit your changes:
git commit -m 'Add some feature'
- Push the branch:
git push origin feature/my-new-feature
- Create a pull request.
Development
To work on the gem locally:
- Clone the repository:
git clone https://github.com/gklsan/remote_translation_loader.git cd remote_translation_loader
- Install dependencies:
bundle install
- Run tests:
rspec
License
This gem is released under the MIT License. See the LICENSE file for details.
Acknowledgments
A big thanks to the open-source community for the inspiration and support. Special mention to contributors who helped shape this gem!
For questions, bug reports, or feature requests, feel free to open an issue. 🚀