Skip to content

Commit

Permalink
Implement simple search on primary locale translations. Really useful…
Browse files Browse the repository at this point in the history
… 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
yaroslav authored and Craig Davey committed Apr 21, 2010
1 parent e149078 commit 6d9de0d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/controllers/tolk/locales_controller.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions app/models/tolk/locale.rb
Expand Up @@ -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?(".")
Expand Down
16 changes: 15 additions & 1 deletion app/views/tolk/locales/show.html.erb
Expand Up @@ -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 %>
Expand All @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions public/tolk/screen.css
Expand Up @@ -291,6 +291,10 @@ table.translations tr.active td {
background: #edf9fe;
}

table.translations .highlight {
background-color: yellow;
}

/*-------------------------------------------------
Pagination
-------------------------------------------------*/
Expand Down
5 changes: 5 additions & 0 deletions test/unit/locale_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 6d9de0d

Please sign in to comment.