Permalink
Browse files

Support for localized API documentation

  • Loading branch information...
1 parent b77e510 commit 13f51b58639891ae809a5abb69963740e92028a3 @mbacovsky mbacovsky committed Apr 15, 2014
@@ -24,8 +24,11 @@ def index
return
end
+ @language = get_language
+
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
if @doc
@@ -43,12 +46,13 @@ def index
@versions = Apipie.available_versions
@doc = @doc[:docs]
- @doc[:link_extension] = Apipie.configuration.link_extension
+ @doc[:link_extension] = (@language ? ".#{@language}" : '')+Apipie.configuration.link_extension
if @doc[:resources].blank?
render "getting_started" and return
end
@resource = @doc[:resources].first if params[:resource].present?
@method = @resource[:methods].first if params[:method].present?
+ @languages = Apipie.configuration.languages
if @resource && @method
render 'method'
@@ -68,20 +72,34 @@ def apipie_checksum
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
- params[:format] = :html unless params[:version].sub!('.html', '').nil?
- params[:format] = :json unless params[:version].sub!('.json', '').nil?
+ [:resource, :method, :version].each do |par|
+ 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]
end
def render_from_cache
path = Apipie.configuration.doc_base_url.dup
- if [:resource, :method, :format].any? { |p| params[p].to_s =~ /\W/ }
- head :bad_request and return
- end
- # version can contain dot, but only one in row
- if params[:version].to_s.gsub(".", "") =~ /\W/ ||
- params[:version].to_s =~ /\.\./
+ # some params can contain dot, but only one in row
+ if [:resource, :method, :format, :version].any? { |p| params[p].to_s.gsub(".", "") =~ /\W/ || params[p].to_s =~ /\.\./ }
head :bad_request and return
end
@@ -7,5 +7,7 @@
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
-<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
-<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
+<noscript><%= t('apipie.enable_javascript_html', :comments_href => link_to(
+ 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>
@@ -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 %>
@@ -1,12 +1,12 @@
<%= raw method[:full_description] %>
<% unless method[:formats].blank? %>
- <%= heading('Supported Formats', h_level) %>
+ <%= heading(t('apipie.supported_formats'), h_level) %>
<%= method[:formats].join(', ') %>
<% end %>
<% unless method[:errors].blank? %>
- <%= heading('Errors', h_level) %>
+ <%= heading(t('apipie.errors'), h_level) %>
<% method[:errors].each do |err| %>
<%= err[:code] %>
<%= err[:description] %>
@@ -19,24 +19,24 @@
<% end %>
<% unless method[:metadata].blank? %>
- <%= heading('Metadata', h_level) %>
+ <%= heading(t('apipie.metadata'), h_level) %>
<%= render(:partial => "metadata", :locals => {:meta => method[:metadata]}) %>
<% end %>
<% unless method[:examples].blank? %>
- <%= heading('Examples', h_level) %>
+ <%= heading(t('apipie.examples'), h_level) %>
<% method[:examples].each do |example| %>
<pre class="prettyprint"><%= example %></pre>
<% end %>
<% end %>
<% unless method[:params].blank? %>
- <%= heading('Params', h_level) %>
+ <%= heading(t('apipie.params'), h_level) %>
<table class='table'>
<thead>
<tr>
- <th>Param name</th>
- <th>Description</th>
+ <th><%= t('aapipie.param_name') %></th>
+ <th><%= t('apipie.description') %></th>
</tr>
</thead>
<tbody>
@@ -9,8 +9,8 @@
<td>
<strong><%= param[:full_name] %> </strong><br>
<small>
- <%= param[:required] ? 'required' : 'optional' %>
- <%= param[:allow_nil] ? ', nil allowed' : '' %>
+ <%= param[:required] ? t('apipie.required') : t('apipie.optional') %>
+ <%= param[:allow_nil] ? ', '+t('apipie.nil_allowed') : '' %>
</small>
</td>
<td>
@@ -7,8 +7,8 @@
<li>
<strong><%= param[:name] %> </strong>:
<small>
- <%= param[:required] ? 'required' : 'optional' %>
- <%= param[:allow_nil] ? ', nil allowed' : '' %>
+ <%= param[:required] ? t('apipie.required') : t('apipie.optional') %>
+ <%= param[:allow_nil] ? ', '+t('apipie.nil_allowed') : '' %>
<% if param[:validator] %>
[ <%= param[:validator] %> ]
<% end %>
@@ -1,14 +1,17 @@
<h1 class='page-header'>
- Oops!
+ <%= t('apipie.oops') %>
<small>
<% if @resource == 'null' %>
- Resource <code><%= params[:resource] %></code> not found.
+ <%= t('apipie.resource_not_found_html', :resource => "<code>#{params[:resource]}</code>") %>
<% 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 %>
</small>
</h1>
<% 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 %>
@@ -1,4 +1,6 @@
-<h1 class='page-header'>No documentation found</h1>
-<p>We have not found any documentation for your API.</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>
+<h1 class='page-header'><%= t('apipie.no_doc_found') %></h1>
+<p><%= t('apipie.no_docs_found_descr') %></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>
@@ -1,15 +1,16 @@
<ul class='breadcrumb'>
<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 %>
<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>
<% end %>
</ul>
<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| %>
<h2>
@@ -21,8 +22,8 @@
<table class='table'>
<thead>
<tr>
- <th>Resource</th>
- <th>Description</th>
+ <th><%= t('apipie.resource') %></th>
+ <th><%= t('apipie.description') %></th>
</tr>
</thead>
<tbody>
@@ -11,6 +11,8 @@
<span class='divider'>/</span>
</li>
<li class='active'><%= @method[:name] %></li>
+ <%= render(:partial => "languages", :locals => {:doc_url => @method[:doc_url]}) %>
+
</ul>
<% @method[:apis].each do |api| %>
@@ -45,14 +45,14 @@
<div>
<%= raw method[:full_description] %>
<% unless method[:examples].blank? %>
- <h4>Examples</h4>
+ <h4><%= t('apipie.examples') %></h4>
<% method[:examples].each do |example| %>
<pre class="wiki"><%= example %></pre>
<% end %>
<% end %>
<% unless method[:errors].blank? %>
- <h4>Errors</h4>
+ <h4><%= t('apipie.errors') %></h4>
<% method[:errors].each do |err| %>
<%= err[:code] %>
<%= err[:description] %>
@@ -62,7 +62,7 @@
<% end %>
<% unless method[:params].blank? %>
- <h4>Params</h4>
+ <h4><%= t('apipie.params') %></h4>
<%= render(:partial => "params_plain", :locals => {:params => method[:params]}) %>
<% end %>
</div>
@@ -7,6 +7,7 @@
<%= @resource[:name] %>
<% if @resource[:version] %><% end %>
</li>
+ <%= render(:partial => "languages", :locals => {:doc_url => @resource[:doc_url]}) %>
</ul>
<div class='page-header'>
@@ -62,14 +62,14 @@
<div>
<%= raw method[:full_description] %>
<% unless method[:examples].blank? %>
- <h2>Examples</h2>
+ <h2><%= t('apipie.examples') %></h2>
<% method[:examples].each do |example| %>
<pre><%= example %></pre>
<% end %>
<% end %>
<% unless method[:errors].blank? %>
- <h2>Errors</h2>
+ <h2><%= t('apipie.errors') %></h2>
<% method[:errors].each do |err| %>
<%= err[:code] %>
<%= err[:description] %>
@@ -79,12 +79,12 @@
<% end %>
<% unless method[:params].blank? %>
- <h2>Params</h2>
+ <h2><%= t('apipie.params') %></h2>
<table class='table'>
<thead>
<tr>
- <th>Param name</th>
- <th>Description</th>
+ <th><%= t('apipie.param_name') %></th>
+ <th><%= t('apipie.description') %></th>
</tr>
</thead>
<tbody>
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>API documentation</title>
+ <title><%= t('apipie.api_documentation') %></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<%= Apipie.include_stylesheets %>
<link type='text/css' rel='stylesheet' href='<%= Apipie.full_url("stylesheets/application.css")%>'/>
View
@@ -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
+
View
@@ -1,3 +1,4 @@
+require 'i18n'
require "apipie/routing"
require "apipie/markup"
require "apipie/apipie_module"
@@ -8,9 +8,9 @@ def self.app
@application ||= Apipie::Application.new
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
- app.to_json(version, resource_name, method_name)
+ app.to_json(version, resource_name, method_name, lang)
end
# all calls delegated to Apipie::Application instance
Oops, something went wrong.

0 comments on commit 13f51b5

Please sign in to comment.