Skip to content

Commit

Permalink
Support for localized API documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbacovsky committed May 12, 2014
1 parent b77e510 commit 13f51b5
Show file tree
Hide file tree
Showing 24 changed files with 233 additions and 113 deletions.
38 changes: 28 additions & 10 deletions app/controllers/apipie/apipies_controller.rb
Expand Up @@ -24,8 +24,11 @@ def index
return return
end end


@language = get_language

Apipie.reload_documentation if Apipie.configuration.reload_controllers? Apipie.reload_documentation if Apipie.configuration.reload_controllers?
@doc = Apipie.to_json(params[:version], params[:resource], params[:method]) I18n.locale = @language
@doc = Apipie.to_json(params[:version], params[:resource], params[:method], @language)


format.json do format.json do
if @doc if @doc
Expand All @@ -43,12 +46,13 @@ def index


@versions = Apipie.available_versions @versions = Apipie.available_versions
@doc = @doc[:docs] @doc = @doc[:docs]
@doc[:link_extension] = Apipie.configuration.link_extension @doc[:link_extension] = (@language ? ".#{@language}" : '')+Apipie.configuration.link_extension
if @doc[:resources].blank? if @doc[:resources].blank?
render "getting_started" and return render "getting_started" and return
end end
@resource = @doc[:resources].first if params[:resource].present? @resource = @doc[:resources].first if params[:resource].present?
@method = @resource[:methods].first if params[:method].present? @method = @resource[:methods].first if params[:method].present?
@languages = Apipie.configuration.languages


if @resource && @method if @resource && @method
render 'method' render 'method'
Expand All @@ -68,20 +72,34 @@ def apipie_checksum


private private


def get_language
lang = nil
[:resource, :method, :version].each do |par|
if params[par]
splitted = params[par].split('.')
if splitted.length > 1 && Apipie.configuration.languages.include?(splitted.last)
lang = splitted.last
params[par].sub!(".#{lang}", '')
end
end
end
lang
end

def get_format def get_format
params[:format] = :html unless params[:version].sub!('.html', '').nil? [:resource, :method, :version].each do |par|
params[:format] = :json unless params[:version].sub!('.json', '').nil? if params[par]
params[:format] = :html unless params[par].sub!('.html', '').nil?
params[:format] = :json unless params[par].sub!('.json', '').nil?
end
end
request.format = params[:format] if params[:format] request.format = params[:format] if params[:format]
end end


def render_from_cache def render_from_cache
path = Apipie.configuration.doc_base_url.dup path = Apipie.configuration.doc_base_url.dup
if [:resource, :method, :format].any? { |p| params[p].to_s =~ /\W/ } # some params can contain dot, but only one in row
head :bad_request and return if [:resource, :method, :format, :version].any? { |p| params[p].to_s.gsub(".", "") =~ /\W/ || params[p].to_s =~ /\.\./ }
end
# version can contain dot, but only one in row
if params[:version].to_s.gsub(".", "") =~ /\W/ ||
params[:version].to_s =~ /\.\./
head :bad_request and return head :bad_request and return
end end


Expand Down
6 changes: 4 additions & 2 deletions app/views/apipie/apipies/_disqus.html.erb
Expand Up @@ -7,5 +7,7 @@
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})(); })();
</script> </script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> <noscript><%= t('apipie.enable_javascript_html', :comments_href => link_to(
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a> t('apipie.comments_powered_by_disqus', :disqus => 'Disqus'),
"http://disqus.com/?ref_noscript")) %></noscript>
<a href="http://disqus.com" class="dsq-brlink"><%= t('apipie.comments_powered_by_disqus', :disqus => "<span class="logo-disqus">Disqus</span>") %></a>
6 changes: 6 additions & 0 deletions app/views/apipie/apipies/_languages.erb
@@ -0,0 +1,6 @@
<% if @languages && @languages.size > 1 %>
<li class='pull-right'>
&nbsp;[ <%= @languages.collect { |lang| link = link_to(lang, "#{doc_url}.#{lang}.html");
lang==@language ? content_tag(:b, link) : link }.join(' | ').html_safe %> ]
</li>
<% end %>
14 changes: 7 additions & 7 deletions app/views/apipie/apipies/_method_detail.erb
@@ -1,12 +1,12 @@
<%= raw method[:full_description] %> <%= raw method[:full_description] %>
<% unless method[:formats].blank? %> <% unless method[:formats].blank? %>
<%= heading('Supported Formats', h_level) %> <%= heading(t('apipie.supported_formats'), h_level) %>
<%= method[:formats].join(', ') %> <%= method[:formats].join(', ') %>
<% end %> <% end %>
<% unless method[:errors].blank? %> <% unless method[:errors].blank? %>
<%= heading('Errors', h_level) %> <%= heading(t('apipie.errors'), h_level) %>
<% method[:errors].each do |err| %> <% method[:errors].each do |err| %>
<%= err[:code] %> <%= err[:code] %>
<%= err[:description] %> <%= err[:description] %>
Expand All @@ -19,24 +19,24 @@
<% end %> <% end %>
<% unless method[:metadata].blank? %> <% unless method[:metadata].blank? %>
<%= heading('Metadata', h_level) %> <%= heading(t('apipie.metadata'), h_level) %>
<%= render(:partial => "metadata", :locals => {:meta => method[:metadata]}) %> <%= render(:partial => "metadata", :locals => {:meta => method[:metadata]}) %>
<% end %> <% end %>
<% unless method[:examples].blank? %> <% unless method[:examples].blank? %>
<%= heading('Examples', h_level) %> <%= heading(t('apipie.examples'), h_level) %>
<% method[:examples].each do |example| %> <% method[:examples].each do |example| %>
<pre class="prettyprint"><%= example %></pre> <pre class="prettyprint"><%= example %></pre>
<% end %> <% end %>
<% end %> <% end %>
<% unless method[:params].blank? %> <% unless method[:params].blank? %>
<%= heading('Params', h_level) %> <%= heading(t('apipie.params'), h_level) %>
<table class='table'> <table class='table'>
<thead> <thead>
<tr> <tr>
<th>Param name</th> <th><%= t('aapipie.param_name') %></th>
<th>Description</th> <th><%= t('apipie.description') %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
Expand Down
4 changes: 2 additions & 2 deletions app/views/apipie/apipies/_params.html.erb
Expand Up @@ -9,8 +9,8 @@
<td> <td>
<strong><%= param[:full_name] %> </strong><br> <strong><%= param[:full_name] %> </strong><br>
<small> <small>
<%= param[:required] ? 'required' : 'optional' %> <%= param[:required] ? t('apipie.required') : t('apipie.optional') %>
<%= param[:allow_nil] ? ', nil allowed' : '' %> <%= param[:allow_nil] ? ', '+t('apipie.nil_allowed') : '' %>
</small> </small>
</td> </td>
<td> <td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/apipie/apipies/_params_plain.html.erb
Expand Up @@ -7,8 +7,8 @@
<li> <li>
<strong><%= param[:name] %> </strong>: <strong><%= param[:name] %> </strong>:
<small> <small>
<%= param[:required] ? 'required' : 'optional' %> <%= param[:required] ? t('apipie.required') : t('apipie.optional') %>
<%= param[:allow_nil] ? ', nil allowed' : '' %> <%= param[:allow_nil] ? ', '+t('apipie.nil_allowed') : '' %>
<% if param[:validator] %> <% if param[:validator] %>
[ <%= param[:validator] %> ] [ <%= param[:validator] %> ]
<% end %> <% end %>
Expand Down
11 changes: 7 additions & 4 deletions app/views/apipie/apipies/apipie_404.html.erb
@@ -1,14 +1,17 @@
<h1 class='page-header'> <h1 class='page-header'>
Oops! <%= t('apipie.oops') %>
<small> <small>
<% if @resource == 'null' %> <% if @resource == 'null' %>
Resource <code><%= params[:resource] %></code> not found. <%= t('apipie.resource_not_found_html', :resource => "<code>#{params[:resource]}</code>") %>
<% else %> <% else %>
Method <code><%= params[:method] %></code> not found for resource <code><%= params[:resource] %></code>. <%= t('apipie.method_not_found_html', :resource => "<code>#{params[:resource]}</code>",
:method => "<code>#{params[:resource]}</code>" %>
<% end %> <% end %>
</small> </small>
</h1> </h1>


<% if @doc %> <% if @doc %>
Try going to <a href='<%= @doc[:doc_url] %><%= @doc[:link_extension] %>'><%= @doc[:name] %> API documentation homepage</a> <%= t('apipie.goto_homepage_html', :href = link_to(
t('apipie.goto_homepage_href', :app_name => @doc[:name]),
File.join(@doc[:doc_url], @doc[:link_extension]))) %>
<% end %> <% end %>
8 changes: 5 additions & 3 deletions app/views/apipie/apipies/getting_started.html.erb
@@ -1,4 +1,6 @@
<h1 class='page-header'>No documentation found</h1> <h1 class='page-header'><%= t('apipie.no_doc_found') %></h1>
<p>We have not found any documentation for your API.</p> <p><%= t('apipie.no_docs_found_descr') %></p>
<p>Follow <a href="https://github.com/Pajk/apipie-rails#getting-started" target="_blank">further instructions</a> on how to describe your controllers.</p> <p><%= t('apipie.follow_instructions_html',
:href => link_to(t('apipie.follow_instructions_href'),
"https://github.com/Pajk/apipie-rails#getting-started", :target => "_blank")) %></p>


9 changes: 5 additions & 4 deletions app/views/apipie/apipies/index.html.erb
@@ -1,15 +1,16 @@
<ul class='breadcrumb'> <ul class='breadcrumb'>
<li class='active'><a href='<%= @doc[:doc_url] %><%= @doc[:link_extension] %>'><%= @doc[:name] %> <%= @doc[:resources].values.first[:version] %></a></li> <li class='active'><a href='<%= @doc[:doc_url] %><%= @doc[:link_extension] %>'><%= @doc[:name] %> <%= @doc[:resources].values.first[:version] %></a></li>
<%= render(:partial => "languages", :locals => {:doc_url => @doc[:doc_url]}) %>
<% if @versions && @versions.size > 1 %> <% if @versions && @versions.size > 1 %>
<li class='pull-right'> <li class='pull-right'>
<%= @versions.collect { |v| link_to v, Apipie.full_url(v) }.join(' / ').html_safe %> <%= @versions.collect { |v| link_to v, Apipie.full_url(v+@doc[:link_extension]) }.join(' / ').html_safe %>
</li> </li>
<% end %> <% end %>
</ul> </ul>


<div><%= raw @doc[:info] %></div> <div><%= raw @doc[:info] %></div>


<h1 class='page-header'>Resources</h1> <h1 class='page-header'><%= t('apipie.resources') %></h1>


<% @doc[:resources].sort_by(&:first).each do |key, api| %> <% @doc[:resources].sort_by(&:first).each do |key, api| %>
<h2> <h2>
Expand All @@ -21,8 +22,8 @@
<table class='table'> <table class='table'>
<thead> <thead>
<tr> <tr>
<th>Resource</th> <th><%= t('apipie.resource') %></th>
<th>Description</th> <th><%= t('apipie.description') %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
Expand Down
2 changes: 2 additions & 0 deletions app/views/apipie/apipies/method.html.erb
Expand Up @@ -11,6 +11,8 @@
<span class='divider'>/</span> <span class='divider'>/</span>
</li> </li>
<li class='active'><%= @method[:name] %></li> <li class='active'><%= @method[:name] %></li>
<%= render(:partial => "languages", :locals => {:doc_url => @method[:doc_url]}) %>

</ul> </ul>


<% @method[:apis].each do |api| %> <% @method[:apis].each do |api| %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/apipie/apipies/plain.html.erb
Expand Up @@ -45,14 +45,14 @@
<div> <div>
<%= raw method[:full_description] %> <%= raw method[:full_description] %>
<% unless method[:examples].blank? %> <% unless method[:examples].blank? %>
<h4>Examples</h4> <h4><%= t('apipie.examples') %></h4>
<% method[:examples].each do |example| %> <% method[:examples].each do |example| %>
<pre class="wiki"><%= example %></pre> <pre class="wiki"><%= example %></pre>
<% end %> <% end %>
<% end %> <% end %>
<% unless method[:errors].blank? %> <% unless method[:errors].blank? %>
<h4>Errors</h4> <h4><%= t('apipie.errors') %></h4>
<% method[:errors].each do |err| %> <% method[:errors].each do |err| %>
<%= err[:code] %> <%= err[:code] %>
<%= err[:description] %> <%= err[:description] %>
Expand All @@ -62,7 +62,7 @@
<% end %> <% end %>
<% unless method[:params].blank? %> <% unless method[:params].blank? %>
<h4>Params</h4> <h4><%= t('apipie.params') %></h4>
<%= render(:partial => "params_plain", :locals => {:params => method[:params]}) %> <%= render(:partial => "params_plain", :locals => {:params => method[:params]}) %>
<% end %> <% end %>
</div> </div>
Expand Down
1 change: 1 addition & 0 deletions app/views/apipie/apipies/resource.html.erb
Expand Up @@ -7,6 +7,7 @@
<%= @resource[:name] %> <%= @resource[:name] %>
<% if @resource[:version] %><% end %> <% if @resource[:version] %><% end %>
</li> </li>
<%= render(:partial => "languages", :locals => {:doc_url => @resource[:doc_url]}) %>
</ul> </ul>


<div class='page-header'> <div class='page-header'>
Expand Down
10 changes: 5 additions & 5 deletions app/views/apipie/apipies/static.html.erb
Expand Up @@ -62,14 +62,14 @@
<div> <div>
<%= raw method[:full_description] %> <%= raw method[:full_description] %>
<% unless method[:examples].blank? %> <% unless method[:examples].blank? %>
<h2>Examples</h2> <h2><%= t('apipie.examples') %></h2>
<% method[:examples].each do |example| %> <% method[:examples].each do |example| %>
<pre><%= example %></pre> <pre><%= example %></pre>
<% end %> <% end %>
<% end %> <% end %>
<% unless method[:errors].blank? %> <% unless method[:errors].blank? %>
<h2>Errors</h2> <h2><%= t('apipie.errors') %></h2>
<% method[:errors].each do |err| %> <% method[:errors].each do |err| %>
<%= err[:code] %> <%= err[:code] %>
<%= err[:description] %> <%= err[:description] %>
Expand All @@ -79,12 +79,12 @@
<% end %> <% end %>
<% unless method[:params].blank? %> <% unless method[:params].blank? %>
<h2>Params</h2> <h2><%= t('apipie.params') %></h2>
<table class='table'> <table class='table'>
<thead> <thead>
<tr> <tr>
<th>Param name</th> <th><%= t('apipie.param_name') %></th>
<th>Description</th> <th><%= t('apipie.description') %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/apipie/apipie.html.erb
@@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>API documentation</title> <title><%= t('apipie.api_documentation') %></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<%= Apipie.include_stylesheets %> <%= Apipie.include_stylesheets %>
<link type='text/css' rel='stylesheet' href='<%= Apipie.full_url("stylesheets/application.css")%>'/> <link type='text/css' rel='stylesheet' href='<%= Apipie.full_url("stylesheets/application.css")%>'/>
Expand Down
27 changes: 27 additions & 0 deletions config/locales/en.yml
@@ -0,0 +1,27 @@
en:
apipie:
resources: Resources
resource: Resource
description: Description
no_docs_found: No documentation found
no_docs_found_descr: We have not found any documentation for your API.
follow_instructions_html: Follow %{href} on how to describe your controllers.
follow_instructions_href: further instructions
oops: Oops!!
resource_not_found_html: Resource %{res} not found.
method_not_found_html: Method %{method} not found for resource %{resource}.
goto_homepage_html: Try going to %{href}
goto_homepage_href: "%{app_name} API documentation homepage"
required: required
optional: optional
nil_allowed: nil allowed
param_name: Param name
params: Params
examples: Examples
metadata: Metadata
errors: Errors
supported_formats: Supported Formats
enable_javascript_html: Please enable JavaScript to view the %{comments_href}.
comments_powered_by_disqus: comments powered by %{disqus}
api_documentation: API documentation

1 change: 1 addition & 0 deletions lib/apipie-rails.rb
@@ -1,3 +1,4 @@
require 'i18n'
require "apipie/routing" require "apipie/routing"
require "apipie/markup" require "apipie/markup"
require "apipie/apipie_module" require "apipie/apipie_module"
Expand Down
4 changes: 2 additions & 2 deletions lib/apipie/apipie_module.rb
Expand Up @@ -8,9 +8,9 @@ def self.app
@application ||= Apipie::Application.new @application ||= Apipie::Application.new
end end


def self.to_json(version = nil, resource_name = nil, method_name = nil) def self.to_json(version = nil, resource_name = nil, method_name = nil, lang = nil)
version ||= Apipie.configuration.default_version version ||= Apipie.configuration.default_version
app.to_json(version, resource_name, method_name) app.to_json(version, resource_name, method_name, lang)
end end


# all calls delegated to Apipie::Application instance # all calls delegated to Apipie::Application instance
Expand Down

0 comments on commit 13f51b5

Please sign in to comment.