Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
grahamsutton opened this issue Apr 3, 2016 · 2 comments
Closed

Comments

@grahamsutton
Copy link

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
Copy link

DaniG2k commented Aug 13, 2020

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
Copy link
Contributor

Antronin commented Jan 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants