Apipie / apipie-rails

Ruby on Rails API documentation tool
Apache License 2.0
2.47k stars 460 forks source link

Can't include a translation in the "description" option for actions. #446

Closed grahamsutton closed 1 year ago

grahamsutton commented 8 years ago

I am trying to pass in a translation in the description option for one of my actions and the translation comes up as just plaintext. I am aware that the documentation states that the description option will be converted to HTML, but is there any support for this feature to allow me to pass translations in action descriptions?

Currently, using I18n.t method to translate in the description option sort of works. When I have config.languages set in the config/intializers/apipie.rb file, it places the links for those languages at the top right of the documentation, but when clicking on a different link, it seems to only translate on every other click when switching languages, and they become mismatched

config/initializers/apipie.rb

Apipie.configure do |config|
  config.app_name                = "Coolbeans API"
  config.api_base_url            = "/"
  config.doc_base_url            = "/docs"
  config.languages               = ['en', 'es']
  config.default_locale          = 'en'
  config.locale = lambda { |loc| loc ? I18n.locale = loc : I18n.locale }
  config.translate = lambda do |str, loc|
    old_loc = I18n.locale
    I18n.locale = loc
    trans = I18n.t(str)
    I18n.locale = old_loc
    trans
  end
  # where is your API defined?
  config.api_controllers_matcher = "#{Rails.root}/app/controllers/**/*.rb"
end

V1::Accounts Controller

api :POST, 'v1/accounts', "Creates a new account."
  description "apipie.accounts.create.below_is_an_example"   # => fails to translate
  error :code => 422, :desc => "Unprocessable Entity"
  example Account.example_request
  param :account, Hash, "The hash that holds the provided account field names and data.", :required => true do
    param :first_name,   String, "The new account's first name.", :required => true
    param :last_name,    String, "The new account's last name.", :required => true
    param :account_type, ["vendor", "member", "master", "admin"], "The new account's account type.", :required => true
    param :password,     String, "The new account's password.", :required => true
    param :active,       [true, false], "The new account's active status.", :required => true
  end

  def create
    account = Account.new(account_params)

    if account.save
      render :json => account
    else
      render :json => account.errors, :status => :unprocessable_entity
    end
  end
DaniG2k commented 4 years ago

I've also encountered this issue. It's currently impossible to translate method descriptions.

@grahamsutton Did you ever find a resolution to the problem?

Antronin commented 1 year ago

I submitted a PR just now to solve this. The reason is that the translation key is converted to HTML from markdown before the translation so it becomes <p>translation_key</p> which the I18n engine cannot find of course as it is translation_key in the language files. Just by changing this the description is displayed and translated as expected. See PR #808