From bab47939c6dc18a90cfd914d23733313e0cea46f Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Tue, 21 Jan 2025 20:01:10 +0100 Subject: [PATCH 1/5] Implement RubyParserFactory to avoid warning when requiring parser/current --- lib/i18n/tasks/scanners/local_ruby_parser.rb | 4 +-- lib/i18n/tasks/scanners/ruby_ast_scanner.rb | 6 ++--- .../tasks/scanners/ruby_parser_factory.rb | 27 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 lib/i18n/tasks/scanners/ruby_parser_factory.rb diff --git a/lib/i18n/tasks/scanners/local_ruby_parser.rb b/lib/i18n/tasks/scanners/local_ruby_parser.rb index b2063e2f..269ee056 100644 --- a/lib/i18n/tasks/scanners/local_ruby_parser.rb +++ b/lib/i18n/tasks/scanners/local_ruby_parser.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'parser/current' +require 'i18n/tasks/scanners/ruby_parser_factory' module I18n::Tasks::Scanners class LocalRubyParser @@ -9,7 +9,7 @@ class LocalRubyParser BLOCK_EXPR = /\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze def initialize(ignore_blocks: false) - @parser = ::Parser::CurrentRuby.new + @parser = RubyParserFactory.create_parser @ignore_blocks = ignore_blocks end diff --git a/lib/i18n/tasks/scanners/ruby_ast_scanner.rb b/lib/i18n/tasks/scanners/ruby_ast_scanner.rb index 61794703..8d37d538 100644 --- a/lib/i18n/tasks/scanners/ruby_ast_scanner.rb +++ b/lib/i18n/tasks/scanners/ruby_ast_scanner.rb @@ -3,10 +3,10 @@ require 'i18n/tasks/scanners/file_scanner' require 'i18n/tasks/scanners/relative_keys' require 'i18n/tasks/scanners/ruby_ast_call_finder' +require 'i18n/tasks/scanners/ruby_parser_factory' require 'i18n/tasks/scanners/ast_matchers/default_i18n_subject_matcher' require 'i18n/tasks/scanners/ast_matchers/message_receivers_matcher' require 'i18n/tasks/scanners/ast_matchers/rails_model_matcher' -require 'parser/current' module I18n::Tasks::Scanners # Scan for I18n.translate calls using whitequark/parser @@ -18,8 +18,8 @@ class RubyAstScanner < FileScanner def initialize(**args) super(**args) - @parser = ::Parser::CurrentRuby.new - @magic_comment_parser = ::Parser::CurrentRuby.new + @parser = RubyParserFactory.create_parser + @magic_comment_parser = RubyParserFactory.create_parser @matchers = setup_matchers end diff --git a/lib/i18n/tasks/scanners/ruby_parser_factory.rb b/lib/i18n/tasks/scanners/ruby_parser_factory.rb new file mode 100644 index 00000000..47f6f69f --- /dev/null +++ b/lib/i18n/tasks/scanners/ruby_parser_factory.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# This module provides a factory class for creating a Ruby parser instance. +# It temporarily disables verbose mode to suppress compatibility warnings +# when loading the "parser/current" library. +# +# Example warning for the release of Ruby 3.4.1: +# warning: parser/current is loading parser/ruby34, which recognizes +# 3.4.0-compliant syntax, but you are running 3.4.1. +# Please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. +# +# By disabling verbose mode, these warnings are suppressed to provide a cleaner +# output and avoid confusion. The verbose mode is restored after the parser +# instance is created to maintain the original behavior. + +module I18n::Tasks::Scanners + class RubyParserFactory + def self.create_parser + prev = $VERBOSE + $VERBOSE = nil + require "parser/current" + ::Parser::CurrentRuby.new + ensure + $VERBOSE = prev + end + end +end From cd54e38eafbc77c60d7f3fbb30d29409303c552c Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Tue, 21 Jan 2025 20:13:57 +0100 Subject: [PATCH 2/5] Add entry to changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index cf8e661c..a9d9cf35 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,6 @@ ## Unreleased +* Silence parser warning and patch version mismatch for Ruby 3.4.x > 3.4.0 [#612](https://github.com/glebm/i18n-tasks/issues/612) * Append JSON instructions to OpenAI system prompt to be able to use response_format json_object [#615](https://github.com/glebm/i18n-tasks/pull/615) * Set `log_errors: true` on OpenAI::Client options in order to display HTTP client errors. [#614](https://github.com/glebm/i18n-tasks/pull/614) * Uses AST-parser for all ERB-files, not just `.html.erb` From 186f47f88aee67483eafa55762b65ea21d34b8af Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Sat, 25 Jan 2025 14:42:17 +0100 Subject: [PATCH 3/5] Fix json error 'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'. --- CHANGES.md | 2 +- lib/i18n/tasks/configuration.rb | 1 + lib/i18n/tasks/translators/openai_translator.rb | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a9d9cf35..9040056d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ ## Unreleased -* Silence parser warning and patch version mismatch for Ruby 3.4.x > 3.4.0 [#612](https://github.com/glebm/i18n-tasks/issues/612) +* Silence parser warning and patch version mismatch for Ruby 3.4.1 [#612](https://github.com/glebm/i18n-tasks/issues/612) * Append JSON instructions to OpenAI system prompt to be able to use response_format json_object [#615](https://github.com/glebm/i18n-tasks/pull/615) * Set `log_errors: true` on OpenAI::Client options in order to display HTTP client errors. [#614](https://github.com/glebm/i18n-tasks/pull/614) * Uses AST-parser for all ERB-files, not just `.html.erb` diff --git a/lib/i18n/tasks/configuration.rb b/lib/i18n/tasks/configuration.rb index 938bb1ab..cbbceb89 100644 --- a/lib/i18n/tasks/configuration.rb +++ b/lib/i18n/tasks/configuration.rb @@ -70,6 +70,7 @@ def translation_config # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComp conf[:deepl_version] = ENV['DEEPL_VERSION'] if ENV.key?('DEEPL_VERSION') conf[:openai_api_key] = ENV['OPENAI_API_KEY'] if ENV.key?('OPENAI_API_KEY') conf[:openai_model] = ENV['OPENAI_MODEL'] if ENV.key?('OPENAI_MODEL') + conf[:openai_log_errors] = ENV['OPENAI_LOG_ERRORS'] if ENV.key?('OPENAI_LOG_ERRORS') conf[:watsonx_api_key] = ENV['WATSONX_API_KEY'] if ENV.key?('WATSONX_API_KEY') conf[:watsonx_project_id] = ENV['WATSONX_PROJECT_ID'] if ENV.key?('WATSONX_PROJECT_ID') conf[:watsonx_model] = ENV['WATSONX_MODEL'] if ENV.key?('WATSONX_MODEL') diff --git a/lib/i18n/tasks/translators/openai_translator.rb b/lib/i18n/tasks/translators/openai_translator.rb index 9ea3ae2d..f01e829f 100644 --- a/lib/i18n/tasks/translators/openai_translator.rb +++ b/lib/i18n/tasks/translators/openai_translator.rb @@ -18,6 +18,7 @@ class OpenAiTranslator < BaseTranslator Variables (starting with %%{ and ending with }) must not be changed under any circumstance. Keep in mind the context of all the strings for a more accurate translation. + Return the translations as a JSON object with a 'translations' array containing the translated strings. It is CRITICAL you output only the result, without any additional information, code block syntax or comments. PROMPT JSON_FORMAT_INSTRUCTIONS_SYSTEM_PROMPT = <<~PROMPT.squish From 19e0b6500e1dcafd5449681ae22703fb4ca2a850 Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Sat, 25 Jan 2025 17:24:23 +0100 Subject: [PATCH 4/5] Remove unrelated PR to their own --- CHANGES.md | 2 +- lib/i18n/tasks/configuration.rb | 1 - lib/i18n/tasks/translators/openai_translator.rb | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9040056d..a9d9cf35 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ ## Unreleased -* Silence parser warning and patch version mismatch for Ruby 3.4.1 [#612](https://github.com/glebm/i18n-tasks/issues/612) +* Silence parser warning and patch version mismatch for Ruby 3.4.x > 3.4.0 [#612](https://github.com/glebm/i18n-tasks/issues/612) * Append JSON instructions to OpenAI system prompt to be able to use response_format json_object [#615](https://github.com/glebm/i18n-tasks/pull/615) * Set `log_errors: true` on OpenAI::Client options in order to display HTTP client errors. [#614](https://github.com/glebm/i18n-tasks/pull/614) * Uses AST-parser for all ERB-files, not just `.html.erb` diff --git a/lib/i18n/tasks/configuration.rb b/lib/i18n/tasks/configuration.rb index cbbceb89..938bb1ab 100644 --- a/lib/i18n/tasks/configuration.rb +++ b/lib/i18n/tasks/configuration.rb @@ -70,7 +70,6 @@ def translation_config # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComp conf[:deepl_version] = ENV['DEEPL_VERSION'] if ENV.key?('DEEPL_VERSION') conf[:openai_api_key] = ENV['OPENAI_API_KEY'] if ENV.key?('OPENAI_API_KEY') conf[:openai_model] = ENV['OPENAI_MODEL'] if ENV.key?('OPENAI_MODEL') - conf[:openai_log_errors] = ENV['OPENAI_LOG_ERRORS'] if ENV.key?('OPENAI_LOG_ERRORS') conf[:watsonx_api_key] = ENV['WATSONX_API_KEY'] if ENV.key?('WATSONX_API_KEY') conf[:watsonx_project_id] = ENV['WATSONX_PROJECT_ID'] if ENV.key?('WATSONX_PROJECT_ID') conf[:watsonx_model] = ENV['WATSONX_MODEL'] if ENV.key?('WATSONX_MODEL') diff --git a/lib/i18n/tasks/translators/openai_translator.rb b/lib/i18n/tasks/translators/openai_translator.rb index f01e829f..9ea3ae2d 100644 --- a/lib/i18n/tasks/translators/openai_translator.rb +++ b/lib/i18n/tasks/translators/openai_translator.rb @@ -18,7 +18,6 @@ class OpenAiTranslator < BaseTranslator Variables (starting with %%{ and ending with }) must not be changed under any circumstance. Keep in mind the context of all the strings for a more accurate translation. - Return the translations as a JSON object with a 'translations' array containing the translated strings. It is CRITICAL you output only the result, without any additional information, code block syntax or comments. PROMPT JSON_FORMAT_INSTRUCTIONS_SYSTEM_PROMPT = <<~PROMPT.squish From 97f9ffbe39d411034a76b03a15bf3c082c91e672 Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Mon, 17 Feb 2025 18:15:24 +0100 Subject: [PATCH 5/5] Fix Rubocop linting errors --- lib/i18n/tasks/scanners/ruby_parser_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/i18n/tasks/scanners/ruby_parser_factory.rb b/lib/i18n/tasks/scanners/ruby_parser_factory.rb index 47f6f69f..28c79754 100644 --- a/lib/i18n/tasks/scanners/ruby_parser_factory.rb +++ b/lib/i18n/tasks/scanners/ruby_parser_factory.rb @@ -18,7 +18,7 @@ class RubyParserFactory def self.create_parser prev = $VERBOSE $VERBOSE = nil - require "parser/current" + require 'parser/current' ::Parser::CurrentRuby.new ensure $VERBOSE = prev