I installed Discourse Translator and tried to use it with my self-hosted LibreTranslate instance. It didn’t work and there was the following backtrace:
NameError (undefined local variable or method `target_locale' for class DiscourseTranslator::LibreTranslate)
app/controllers/application_controller.rb:427:in `block in with_resolved_locale'
app/controllers/application_controller.rb:427:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:35:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:409:in `call'
lib/middleware/csp_script_nonce_injector.rb:12:in `call'
config/initializers/008-rack-cors.rb:14:in `call'
config/initializers/100-quiet_logger.rb:20:in `call'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/enforce_hostname.rb:24:in `call'
lib/middleware/processing_request.rb:12:in `call'
lib/middleware/request_tracker.rb:385:in `call'
Backtrace
plugins/discourse-translator/app/services/discourse_translator/libre_translate.rb:107:in `block in translate!'
plugins/discourse-translator/app/services/discourse_translator/base.rb:82:in `save_translation'
plugins/discourse-translator/app/services/discourse_translator/libre_translate.rb:101:in `translate!'
plugins/discourse-translator/app/services/discourse_translator/base.rb:51:in `translate'
plugins/discourse-translator/app/controllers/discourse_translator/translator_controller.rb:44:in `translate'
actionpack (7.2.2.1) lib/action_controller/metal/basic_implicit_render.rb:8:in `send_action'
actionpack (7.2.2.1) lib/abstract_controller/base.rb:226:in `process_action'
actionpack (7.2.2.1) lib/action_controller/metal/rendering.rb:193:in `process_action'
actionpack (7.2.2.1) lib/abstract_controller/callbacks.rb:261:in `block in process_action'
activesupport (7.2.2.1) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
app/controllers/application_controller.rb:427:in `block in with_resolved_locale'
i18n (1.14.7) lib/i18n.rb:353:in `with_locale'
Checking the code:
def self.translate!(translatable, target_locale_sym = I18n.locale)
detected_lang = detect(translatable)
save_translation(translatable, target_locale_sym) do
res =
result(
translate_uri,
q: text_for_translation(translatable),
source: detected_lang,
target: SUPPORTED_LANG_MAPPING[target_locale],
format: "html",
)
res["translatedText"]
end
end
especially line 107, which is:
target: SUPPORTED_LANG_MAPPING[target_locale]
→ it seems it has to be be ‘target_locale_sym’ instead of ‘target_locale’ or am I wrong?
I changed it manually to: target: SUPPORTED_LANG_MAPPING[target_locale_sym], saved and restarted the app and it worked.