Skip to content

Commit

Permalink
Refactored controllers, views etc to support now standard Locale and …
Browse files Browse the repository at this point in the history
…Translations classes. Restructured dirs. Made database_spec run with rake spec:plugins.
  • Loading branch information
Shane Mingins authored and Shane Mingins committed Feb 3, 2009
1 parent d5d3d4e commit 4e9a125
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 169 deletions.
7 changes: 1 addition & 6 deletions init.rb
@@ -1,6 +1 @@
require 'i18n_backend_database'
require File.dirname(__FILE__) + '/lib/locale'
require File.dirname(__FILE__) + '/lib/translation'
require File.dirname(__FILE__) + '/lib/routing'
require File.dirname(__FILE__) + '/lib/locales_controller'
ActionController::Routing::RouteSet::Mapper.send(:include, I18n::BackendDatabase::Routing)
require 'i18n_backend_database'
@@ -1,6 +1,5 @@
class LocalesController < ActionController::Base
prepend_view_path(File.join(File.dirname(__FILE__), "..", "views"))

# GET /locales
# GET /locales.xml
def index
Expand Down Expand Up @@ -42,7 +41,7 @@ def edit
# POST /locales
# POST /locales.xml
def create
@locale = Locale.new(params[:i18n_locale])
@locale = Locale.new(params[:locale])

respond_to do |format|
if @locale.save
Expand All @@ -62,7 +61,7 @@ def update
@locale = Locale.find_by_code(params[:id])

respond_to do |format|
if @locale.update_attributes(params[:i18n_locale])
if @locale.update_attributes(params[:locale])
flash[:notice] = 'Locale was successfully updated.'
format.html { redirect_to(@locale) }
format.xml { head :ok }
Expand All @@ -80,7 +79,7 @@ def destroy
@locale.destroy

respond_to do |format|
format.html { redirect_to(i18n_locales_url) }
format.html { redirect_to(locales_url) }
format.xml { head :ok }
end
end
Expand Down
@@ -1,4 +1,4 @@
class TranslationsController < ApplicationController
class TranslationsController < ActionController::Base
prepend_view_path(File.join(File.dirname(__FILE__), "..", "views"))
before_filter :find_locale

Expand Down Expand Up @@ -43,12 +43,12 @@ def edit
# POST /translations
# POST /translations.xml
def create
@translation = @locale.translations.build(params[:i18n_translation])
@translation = @locale.translations.build(params[:translation])

respond_to do |format|
if @translation.save
flash[:notice] = 'Translation was successfully created.'
format.html { redirect_to i18n_locale_translation_path(@locale, @translation) }
format.html { redirect_to locale_translation_path(@locale, @translation) }
format.xml { render :xml => @translation, :status => :created, :location => @translation }
else
format.html { render :action => "new" }
Expand All @@ -63,9 +63,9 @@ def update
@translation = @locale.translations.find(params[:id])

respond_to do |format|
if @translation.update_attributes(params[:i18n_translation])
if @translation.update_attributes(params[:translation])
flash[:notice] = 'Translation was successfully updated.'
format.html { redirect_to i18n_locale_translation_path(@locale, @translation) }
format.html { redirect_to locale_translation_path(@locale, @translation) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
Expand All @@ -81,7 +81,7 @@ def destroy
@translation.destroy

respond_to do |format|
format.html { redirect_to(i18n_locale_translations_url) }
format.html { redirect_to(locale_translations_url) }
format.xml { head :ok }
end
end
Expand Down
59 changes: 7 additions & 52 deletions lib/i18n_backend_database.rb
@@ -1,52 +1,7 @@
module I18n
module Backend
class Database
attr_accessor :locale
attr_accessor :cache_store

def initialize(options = {})
store = options.delete(:cache_store)
@cache_store = store ? ActiveSupport::Cache.lookup_store(store) : Rails.cache
@locale = Locale.find_by_code(I18n.locale.to_s)
end

def cache_store=(store)
@cache_store = ActiveSupport::Cache.lookup_store(store)
end

def translate(locale, key, options = {})
# allow for arbitrary locale lookups. use the cached locale otherwise.
tmp_locale = Locale.find_by_code(locale.to_s) unless (@locale.code == locale)
cache_key = build_cache_key((tmp_locale || @locale), key, options)

# check for key and return it if it exists
value = @cache_store.read(cache_key)
return value if value

# find or create translation record
locale_in_context = (tmp_locale || @locale)
value = locale_in_context.find_or_create_translation(cache_key, options).value

# NOTE: raw ok with non-memcache stores?
@cache_store.write(cache_key, value, :raw => true)

value || key
end

def available_locales
Locale.available_locales
end

def reload!
# get's called on initialization
# let's not do anything yet
end

protected
# locale:"key":pluralization_index
def build_cache_key(locale, key, options)
"#{locale.code}:#{key}:#{(options[:pluralization_index] || 1)}"
end
end
end
end
require File.dirname(__FILE__) + '/models/locale'
require File.dirname(__FILE__) + '/models/translation'
require File.dirname(__FILE__) + '/routing'
require File.dirname(__FILE__) + '/controllers/locales_controller'
require File.dirname(__FILE__) + '/controllers/translations_controller'
require File.dirname(__FILE__) + '/i18n_backend_database/database'
ActionController::Routing::RouteSet::Mapper.send(:include, I18n::BackendDatabase::Routing)
52 changes: 52 additions & 0 deletions lib/i18n_backend_database/database.rb
@@ -0,0 +1,52 @@
module I18n
module Backend
class Database
attr_accessor :locale
attr_accessor :cache_store

def initialize(options = {})
store = options.delete(:cache_store)
@cache_store = store ? ActiveSupport::Cache.lookup_store(store) : Rails.cache
@locale = Locale.find_by_code(I18n.locale.to_s)
end

def cache_store=(store)
@cache_store = ActiveSupport::Cache.lookup_store(store)
end

def translate(locale, key, options = {})
# allow for arbitrary locale lookups. use the cached locale otherwise.
tmp_locale = Locale.find_by_code(locale.to_s) unless (@locale.code == locale)
cache_key = build_cache_key((tmp_locale || @locale), key, options)

# check for key and return it if it exists
value = @cache_store.read(cache_key)
return value if value

# find or create translation record
locale_in_context = (tmp_locale || @locale)
value = locale_in_context.find_or_create_translation(cache_key, options).value

# NOTE: raw ok with non-memcache stores?
@cache_store.write(cache_key, value, :raw => true)

value || key
end

def available_locales
Locale.available_locales
end

def reload!
# get's called on initialization
# let's not do anything yet
end

protected
# locale:"key":pluralization_index
def build_cache_key(locale, key, options)
"#{locale.code}:#{key}:#{(options[:pluralization_index] || 1)}"
end
end
end
end
26 changes: 0 additions & 26 deletions lib/i18n_backend_database/locale.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/i18n_backend_database/translation.rb

This file was deleted.

File renamed without changes.
File renamed without changes.
Expand Up @@ -17,4 +17,4 @@
<% end %>
<%= link_to 'Show', @locale %> |
<%= link_to 'Back', i18n_locales_path %>
<%= link_to 'Back', locales_path %>
Expand Up @@ -11,12 +11,12 @@
<td><%=h locale.code %></td>
<td><%=h locale.name %></td>
<td><%= link_to 'Show', locale %></td>
<td><%= link_to 'Edit', edit_i18n_locale_path(locale) %></td>
<td><%= link_to 'Edit', edit_locale_path(locale) %></td>
<td><%= link_to 'Destroy', locale, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New locale', new_i18n_locale_path %>
<%= link_to 'New locale', new_locale_path %>
Expand Up @@ -16,4 +16,4 @@
</p>
<% end %>
<%= link_to 'Back', i18n_locales_path %>
<%= link_to 'Back', locales_path %>
14 changes: 14 additions & 0 deletions lib/views/locales/show.html.erb
@@ -0,0 +1,14 @@
<p>
<b>Code:</b>
<%=h @locale.code %>
</p>

<p>
<b>Name:</b>
<%=h @locale.name %>
</p>


<%= link_to 'Edit', edit_locale_path(@locale) %> |
<%= link_to 'Translations', locale_translations_path(@locale) %> |
<%= link_to 'Back', locales_path %>
@@ -1,6 +1,6 @@
<h1>Editing translation for <%= @locale.code %></h1>

<% form_for(@translation, :url => i18n_locale_translation_path(@locale, @translation)) do |f| %>
<% form_for([@locale, @translation]) do |f| %>
<%= f.error_messages %>

<p>
Expand All @@ -20,5 +20,5 @@
</p>
<% end %>
<%= link_to 'Show', i18n_locale_translation_path(@locale, @translation) %> |
<%= link_to 'Back', i18n_locale_translations_path %>
<%= link_to 'Show', locale_translation_path(@locale, @translation) %> |
<%= link_to 'Back', locale_translations_path %>
25 changes: 25 additions & 0 deletions lib/views/translations/index.html.erb
@@ -0,0 +1,25 @@
<h1>Listing translations for <%= @locale.code %></h1>

<table>
<tr>
<th>Key</th>
<th>Value</th>
<th>Pluralization index</th>
</tr>

<% for translation in @translations %>
<tr>
<td><%=h translation.key %></td>
<td><%=h translation.value %></td>
<td><%=h translation.pluralization_index %></td>
<td><%= link_to 'Show', locale_translation_path(@locale, translation) %></td>
<td><%= link_to 'Edit', edit_locale_translation_path(@locale, translation) %></td>
<td><%= link_to 'Destroy', locale_translation_path(@locale, translation), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New translation', new_locale_translation_path %> |
<%= link_to 'Back', locales_path %>
@@ -1,6 +1,6 @@
<h1>New translation</h1>

<% form_for(@translation, :url => i18n_locale_translations_path(@locale)) do |f| %>
<% form_for([@locale, @translation]) do |f| %>
<%= f.error_messages %>

<p>
Expand All @@ -20,4 +20,4 @@
</p>
<% end %>
<%= link_to 'Back', i18n_locale_translations_path %>
<%= link_to 'Back', locale_translations_path %>
Expand Up @@ -19,5 +19,5 @@
</p>


<%= link_to 'Edit', edit_i18n_locale_translation_path(@locale, @translation) %> |
<%= link_to 'Back', i18n_locale_translations_path %>
<%= link_to 'Edit', edit_locale_translation_path(@locale, @translation) %> |
<%= link_to 'Back', locale_translations_path %>
2 changes: 1 addition & 1 deletion routes.rb
@@ -1,3 +1,3 @@
map.resources :locales, :name_prefix => 'i18n_' do |locales|
map.resources :locales do |locales|
locales.resources :translations
end

0 comments on commit 4e9a125

Please sign in to comment.