forked from dylanz/i18n_backend_database
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored controllers, views etc to support now standard Locale and …
…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
Showing
25 changed files
with
138 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
</p> | ||
<% end %> | ||
|
||
<%= link_to 'Back', i18n_locales_path %> | ||
<%= link_to 'Back', locales_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
map.resources :locales, :name_prefix => 'i18n_' do |locales| | ||
map.resources :locales do |locales| | ||
locales.resources :translations | ||
end |
Oops, something went wrong.