Permalink
Browse files

Implement simple search on primary locale translations. Really useful…

… when translating while actually looking at working interface. LIKE %% is slow as hell, but Tolk is not an end-user tool anyway ;-)
  • Loading branch information...
1 parent e149078 commit 6d9de0d68309a9b778e6b9dee533f5534361d2dc @yaroslav yaroslav committed with Craig Davey Apr 14, 2010
@@ -9,7 +9,13 @@ def index
def show
respond_to do |format|
- format.html { @phrases = @locale.phrases_without_translation(params[:page]) }
+ format.html do
+ if params[:q].present?
+ @phrases = @locale.search_phrases_without_translation(params[:q], params[:page])
+ else
+ @phrases = @locale.phrases_without_translation(params[:page])
+ end
+ end
format.atom { @phrases = @locale.phrases_without_translation(params[:page], :per_page => 50) }
format.yml { render :text => @locale.to_hash.ya2yaml(:syck_compatible => true) }
end
View
@@ -110,6 +110,20 @@ def phrases_without_translation(page = nil, options = {})
result
end
+ def search_phrases_without_translation(query, page = nil, options = {})
+ return phrases_without_translation(page, options) unless query.present?
+
+ phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')
+
+ found_translations_ids = Tolk::Locale.primary_locale.translations.all(:conditions => ["tolk_translations.text LIKE ?", "%#{query}%"], :select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
+ existing_ids = self.translations.all(:select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
+ phrases = phrases.scoped(:conditions => ['tolk_phrases.id NOT IN (?) AND tolk_phrases.id IN(?)', existing_ids, found_translations_ids]) if existing_ids.present?
+
+ result = phrases.paginate({:page => page}.merge(options))
+ Tolk::Phrase.send :preload_associations, result, :translations
+ result
+ end
+
def to_hash
{ name => translations.each_with_object({}) do |translation, locale|
if translation.phrase.key.include?(".")
@@ -8,6 +8,16 @@
<h3 class="switch">Phrases missing translation (<%= @locale.count_phrases_without_translation %>) <span>(<%= link_to 'See completed translations', all_tolk_locale_path(@locale) %>)</span></h3>
+<h4 class="search">
+ <% form_tag @locale, :method => :get do %>
+ Search for a phrase in <span><%= Tolk::Locale.primary_language_name -%></span>: <%= text_field_tag :q, params[:q] %>
+ <%= submit_tag "Search", :name => nil %>
+ <% if params[:q].present? %>
+ (<%= link_to "Show all", @locale %>)
+ <% end %>
+ <% end %>
+</h4>
+
<% if @locale.has_updated_translations? && action_name != 'updated' %>
<span class="notice">Some phrases have changed. <%= link_to "Update translations", updated_tolk_locale_path(@locale) %>.</span>
<% end %>
@@ -30,7 +40,11 @@
<%= text_area_tag :"translations[][text]", translation.text, :class => "locale", :id => "#{translation.object_id}_text", :onfocus => "$(this).up('tr').addClassName('active');", :onblur => "$(this).up('tr').removeClassName('active');" %>
</td>
<td class="phrase">
- <%= format_i18n_value(phrase.translations.primary.text) -%>
+ <% if params[:q].present? -%>
+ <%= highlight(format_i18n_value(phrase.translations.primary.text), params[:q]) -%>
+ <% else -%>
+ <%= format_i18n_value(phrase.translations.primary.text) -%>
+ <% end -%>
<span class="key" title="<%= phrase.key %>"><%= truncate(phrase.key, :length => 100) %></span>
</td>
</tr>
View
@@ -291,6 +291,10 @@ table.translations tr.active td {
background: #edf9fe;
}
+table.translations .highlight {
+ background-color: yellow;
+}
+
/*-------------------------------------------------
Pagination
-------------------------------------------------*/
View
@@ -19,6 +19,11 @@ class LocaleTest < ActiveSupport::TestCase
assert tolk_locales(:en).phrases_without_translation.include?(tolk_phrases(:cozy))
end
+ test "searching phrases without translations" do
+ assert tolk_locales(:en).search_phrases_without_translation("cozy").include?(tolk_phrases(:cozy))
+ assert !tolk_locales(:en).search_phrases_without_translation("cozy").include?(tolk_phrases(:hello_world))
+ end
+
test "paginating phrases without translations" do
Tolk::Phrase.per_page = 2
locale = tolk_locales(:se)

0 comments on commit 6d9de0d

Please sign in to comment.