better_translate 0.1.0 → 0.2.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 +4 -4
- data/README.md +63 -26
- data/lib/better_translate/translator.rb +14 -9
- data/lib/better_translate/version.rb +1 -1
- data/lib/generators/better_translate/translate_generator.rb +15 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 861e9d3d1401025d2e12487f5fb6aa4e81cd67b90aa08a0a022d135eb4b71371
|
4
|
+
data.tar.gz: 26ff1894216353f54a8e2f60e9563526ac42da9c9cdc0ae71c4b5bed73c8debc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4082ac26837e5adb8e9b96f2f8738b61219780df6234ce25646f7bcde53faa4848abfb0cb20e0d9063e4ffab789ede5f305ebeb0cf52d7898fb58c67cb68237c
|
7
|
+
data.tar.gz: 66d7e88dbcd40785676ce17168ce177e29a948b7ddc7aa741bca2ed852aeee90fe98e713e28e74b5008da92ad613f6306046fda81be59ac7491b966ef1fcb579
|
data/README.md
CHANGED
@@ -3,26 +3,33 @@
|
|
3
3
|
BetterTranslate is a Ruby gem that enables you to translate YAML files from a source language into one or more target languages using translation providers such as ChatGPT (OpenAI) and Google Gemini. The gem supports two translation modes:
|
4
4
|
|
5
5
|
- **Override**: Completely rewrites the translated file.
|
6
|
-
- **Incremental**: Updates the translated file only with new or modified keys while keeping existing ones.
|
6
|
+
- **Incremental**: Updates the translated file only with new or modified keys while keeping the existing ones.
|
7
7
|
|
8
8
|
Configuration is centralized via an initializer (for example, in a Rails app), where you can set API keys, the source language, target languages, key exclusions, the output folder, and the translation mode. Additionally, BetterTranslate integrates progress tracking using the [ruby-progressbar](https://github.com/jfelchner/ruby-progressbar) gem.
|
9
9
|
|
10
10
|
## Features
|
11
11
|
|
12
12
|
- **Multi-language YAML Translation**: Translates YAML files from a source language into one or more target languages.
|
13
|
-
- **Multiple Providers**: Supports ChatGPT (OpenAI) and Google Gemini (with potential for
|
14
|
-
- **Translation Modes**:
|
15
|
-
|
16
|
-
|
13
|
+
- **Multiple Providers**: Supports ChatGPT (OpenAI) and Google Gemini (with potential for extension to other providers in the future).
|
14
|
+
- **Translation Modes**:
|
15
|
+
- **Override**: Rewrites the file from scratch.
|
16
|
+
- **Incremental**: Updates only missing or modified keys.
|
17
17
|
- **Centralized Configuration**: Configured via an initializer with settings for API keys, source language, target languages, exclusions (using dot notation), and the output folder.
|
18
|
-
- **
|
18
|
+
- **Two-Step Exclusion Filtering**:
|
19
|
+
- **Global Exclusions**: Removes keys defined in `global_exclusions` from the entire YAML structure.
|
20
|
+
- **Language-Specific Exclusions**: Applies additional filtering using the `exclusions_per_language` map for each target language.
|
21
|
+
- **Progress Bar**: Displays translation progress using ruby-progressbar.
|
22
|
+
- **Rails Generators**:
|
23
|
+
- `rails generate better_translate:install` to generate the initializer.
|
24
|
+
- `rails generate better_translate:translate` to trigger the translation process directly from your Rails app.
|
19
25
|
|
20
26
|
## Installation
|
21
27
|
|
22
28
|
Add the gem to your Gemfile:
|
23
29
|
|
24
30
|
```ruby
|
25
|
-
gem 'better_translate'
|
31
|
+
gem 'better_translate', '~> 0.1.0'
|
32
|
+
```
|
26
33
|
|
27
34
|
Then run:
|
28
35
|
|
@@ -50,38 +57,43 @@ This command creates the file `config/initializers/better_translate.rb` with a d
|
|
50
57
|
BetterTranslate.configure do |config|
|
51
58
|
# Choose the provider to use: :chatgpt or :gemini
|
52
59
|
config.provider = :chatgpt
|
53
|
-
|
60
|
+
|
54
61
|
# API key for ChatGPT (OpenAI)
|
55
62
|
config.openai_key = ENV.fetch("OPENAI_API_KEY") { "YOUR_OPENAI_API_KEY" }
|
56
|
-
|
63
|
+
|
57
64
|
# API key for Google Gemini
|
58
65
|
config.google_gemini_key = ENV.fetch("GOOGLE_GEMINI_KEY") { "YOUR_GOOGLE_GEMINI_KEY" }
|
59
|
-
|
66
|
+
|
60
67
|
# Source language (e.g., "en" if the source file is in English)
|
61
68
|
config.source_language = "en"
|
62
|
-
|
69
|
+
|
63
70
|
# Output folder where the translated files will be saved
|
64
71
|
config.output_folder = Rails.root.join("config", "locales", "translated").to_s
|
65
|
-
|
72
|
+
|
66
73
|
# List of target languages (short_name and name)
|
67
74
|
config.target_languages = [
|
68
75
|
# Example:
|
69
76
|
{ short_name: "it", name: "italian" }
|
70
77
|
]
|
71
|
-
|
78
|
+
|
72
79
|
# Global exclusions (keys in dot notation) to exclude from translation
|
73
80
|
config.global_exclusions = [
|
74
|
-
"key.child_key"
|
81
|
+
"key.child_key"
|
75
82
|
]
|
76
|
-
|
77
|
-
# Language-specific exclusions
|
83
|
+
|
84
|
+
# Language-specific exclusions: keys to exclude only for specific target languages
|
78
85
|
config.exclusions_per_language = {
|
86
|
+
"es" => [],
|
87
|
+
"it" => ["sample.valid"],
|
88
|
+
"fr" => [],
|
89
|
+
"de" => [],
|
90
|
+
"pt" => [],
|
79
91
|
"ru" => []
|
80
92
|
}
|
81
|
-
|
93
|
+
|
82
94
|
# Path to the input file (e.g., en.yml)
|
83
95
|
config.input_file = Rails.root.join("config", "locales", "en.yml").to_s
|
84
|
-
|
96
|
+
|
85
97
|
# Translation mode: :override or :incremental
|
86
98
|
config.translation_method = :override
|
87
99
|
end
|
@@ -89,7 +101,7 @@ end
|
|
89
101
|
|
90
102
|
## Usage
|
91
103
|
|
92
|
-
### YAML
|
104
|
+
### Translating YAML Files
|
93
105
|
|
94
106
|
To start the translation process, simply call the `magic` method:
|
95
107
|
|
@@ -99,17 +111,42 @@ BetterTranslate.magic
|
|
99
111
|
|
100
112
|
This will execute the process that:
|
101
113
|
1. Reads the input YAML file.
|
102
|
-
2. Applies
|
103
|
-
3.
|
104
|
-
4.
|
114
|
+
2. Applies the global exclusion filtering.
|
115
|
+
3. Applies additional language-specific exclusion filtering for each target language.
|
116
|
+
4. Translates the strings from the source language into the configured target languages.
|
117
|
+
5. Writes the translated files to the output folder, either in **override** or **incremental** mode based on the configuration.
|
118
|
+
|
119
|
+
### Using Rails Generators
|
120
|
+
|
121
|
+
The gem includes generators to simplify tasks:
|
122
|
+
|
123
|
+
- **Generate Initializer:**
|
124
|
+
|
125
|
+
```bash
|
126
|
+
rails generate better_translate:install
|
127
|
+
```
|
128
|
+
|
129
|
+
- **Trigger Translation Process:**
|
130
|
+
|
131
|
+
```bash
|
132
|
+
rails generate better_translate:translate
|
133
|
+
```
|
134
|
+
|
135
|
+
The `better_translate:translate` generator will trigger the translation process (via `BetterTranslate.magic`) and display progress in the terminal.
|
136
|
+
|
137
|
+
## Contact & Feature Requests
|
138
|
+
|
139
|
+
For suggestions, bug reports, or to request new features, please reach out via email at: **alessio.bussolari@pandev.it**.
|
140
|
+
|
141
|
+
## Upcoming Features
|
105
142
|
|
106
|
-
|
143
|
+
- **Helper Methods**: Additional helper methods to integrate BetterTranslate as a translation system for dynamic content.
|
107
144
|
|
108
|
-
|
145
|
+
## Conclusions
|
109
146
|
|
110
|
-
|
147
|
+
BetterTranslate aims to simplify the translation of YAML files in Ruby projects by providing a flexible, configurable, and extensible system. Whether you need a complete file rewrite or an incremental update, BetterTranslate streamlines the translation process using advanced providers like ChatGPT and Google Gemini. Contributions, feedback, and feature requests are highly encouraged to help improve the gem further.
|
111
148
|
|
112
|
-
|
149
|
+
For more details, please visit the [GitHub repository](https://github.com/alessiobussolari/better_translate).
|
113
150
|
|
114
151
|
## License
|
115
152
|
|
@@ -7,19 +7,24 @@ module BetterTranslate
|
|
7
7
|
translations = read_yml_source
|
8
8
|
|
9
9
|
# Rimuove le chiavi da escludere (global_exclusions) dalla struttura letta
|
10
|
-
|
10
|
+
global_filtered_translations = remove_exclusions(
|
11
11
|
translations, BetterTranslate.configuration.global_exclusions
|
12
12
|
)
|
13
13
|
|
14
14
|
BetterTranslate.configuration.target_languages.each do |target_lang|
|
15
|
+
|
16
|
+
# Fase 2: Applica il filtro specifico per la lingua target
|
17
|
+
lang_exclusions = BetterTranslate.configuration.exclusions_per_language[target_lang[:short_name]] || []
|
18
|
+
filtered_translations = remove_exclusions(
|
19
|
+
global_filtered_translations, lang_exclusions
|
20
|
+
)
|
21
|
+
|
15
22
|
puts "Inizio traduzione da #{BetterTranslate.configuration.source_language} a #{target_lang[:short_name]}"
|
16
23
|
service = BetterTranslate::Service.new
|
17
24
|
translated_data = translate_with_progress(filtered_translations, service, target_lang[:short_name], target_lang[:name])
|
18
25
|
BetterTranslate::Writer.write_translations(translated_data, target_lang[:short_name])
|
19
26
|
puts "Traduzione completata da #{BetterTranslate.configuration.source_language} a #{target_lang[:short_name]}"
|
20
27
|
end
|
21
|
-
|
22
|
-
"Traduzione iniziata! #{filtered_translations.inspect}"
|
23
28
|
end
|
24
29
|
|
25
30
|
private
|
@@ -38,18 +43,18 @@ module BetterTranslate
|
|
38
43
|
YAML.load_file(file_path)
|
39
44
|
end
|
40
45
|
|
41
|
-
# Rimuove le chiavi
|
46
|
+
# Rimuove le chiavi globali da escludere dalla struttura dati,
|
42
47
|
# calcolando i percorsi a partire dal contenuto della lingua di partenza.
|
43
48
|
#
|
44
49
|
# Ad esempio, se il file YAML è:
|
45
|
-
#
|
46
|
-
# e
|
50
|
+
# { "en" => { "sample" => { "valid" => "valid", "excluded" => "Excluded" } } }
|
51
|
+
# e global_exclusions = ["sample.excluded"],
|
47
52
|
# il risultato sarà:
|
48
|
-
#
|
53
|
+
# { "en" => { "sample" => { "valid" => "valid" } } }
|
49
54
|
#
|
50
55
|
# @param data [Hash, Array, Object] La struttura dati da filtrare.
|
51
|
-
# @param
|
52
|
-
# @param current_path [Array] Il percorso corrente (usato in maniera ricorsiva).
|
56
|
+
# @param global_exclusions [Array<String>] Lista dei percorsi (in dot notation) da escludere globalmente.
|
57
|
+
# @param current_path [Array] Il percorso corrente (usato in maniera ricorsiva, default: []).
|
53
58
|
# @return [Hash, Array, Object] La struttura dati filtrata.
|
54
59
|
def remove_exclusions(data, exclusion_list, current_path = [])
|
55
60
|
if data.is_a?(Hash)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module BetterTranslate
|
4
|
+
module Generators
|
5
|
+
class TranslateGenerator < Rails::Generators::Base
|
6
|
+
desc "Lancia il processo di traduzione configurato in BetterTranslate"
|
7
|
+
|
8
|
+
def run_translation
|
9
|
+
say_status("Starting", "Esecuzione della traduzione con BetterTranslate...", :green)
|
10
|
+
BetterTranslate.magic
|
11
|
+
say_status("Done", "Traduzione completata.", :green)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alessio_bussolari
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yaml
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/better_translate/writer.rb
|
108
108
|
- lib/generators/better_translate/install_generator.rb
|
109
109
|
- lib/generators/better_translate/templates/better_translate.rb
|
110
|
+
- lib/generators/better_translate/translate_generator.rb
|
110
111
|
homepage: https://github.com/alessiobussolari/better_translate
|
111
112
|
licenses:
|
112
113
|
- MIT
|