Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
merged from wat.master
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Hochfilzer committed Nov 14, 2012
2 parents 27e90c2 + eec82ea commit c06cc6f
Show file tree
Hide file tree
Showing 25 changed files with 267 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ config/settings.yml
/config/environments/*.local.yml
/config/servers/configs
config/servers/configs
/config/redis.yml
config/redis.yml

# bundler state
/.bundle
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ gem 'rails_config'
gem 'capistrano'
gem "ckeditor", "~> 3.7.1"
gem "will_paginate", "3.0.pre4"
gem "redis"


# Authentication
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ GEM
rdoc (3.12)
json (~> 1.4)
redcarpet (2.1.1)
redis (3.0.2)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
Expand Down Expand Up @@ -345,6 +346,7 @@ DEPENDENCIES
rails_config
rb-fsevent
redcarpet
redis
rspec-rails (>= 2.8.1)
ruby_gntp
sass-rails (~> 3.2.3)
Expand Down
16 changes: 16 additions & 0 deletions app/assets/javascripts/bootbox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions app/assets/javascripts/translations.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
jQuery ->

$('.locale-line').live 'click', (event) ->
event.preventDefault()
translator = new Translator($(this).data('key'),$(this).data('locale'),$(this).data('value'))

class Translator
constructor: (_key,_locale,_default) ->
@locale = _locale
@default = _default
@key = _key
@is_counter = @default instanceof Object

bootbox.dialog( @dialog, [
{"label": "OK", "class":"btn-primary", "callback": @update},
{"label": "Cancel", "class":"btn-default", "callback": @cancel},
])

dialog: =>
html_form = "<h1>Translate</h1>" +
"<p>Locale: #{@locale}<br/>Key: #{@key}</p><form>"
if !@is_counter
html_form += "<textarea id='new-translation' style='width: 90%'>#{@default}</textarea>"
else
html_form += "Zero:<br/><input type='text' name='zero' style='width: 90%' id='new-translation-zero' value='"
html_form += @remove_extra_chars(@default['zero']) + "'>"
html_form += "<br/><br/>One:<br/><input name='one' type='text' style='width: 90%' id='new-translation-one' value='"
html_form += @remove_extra_chars(@default['one']) + "'>"
html_form += "<br/></br>Other:<br/><input name='other' type='text' style='width: 90%' id='new-translation-other' value='"
html_form += @remove_extra_chars(@default['other']) + "'>"
html_form += "</form>"
return html_form

update: =>
if @is_counter
result = {
'zero': $('#new-translation-zero').val(),
'one': $('#new-translation-one').val(),
'other':$('#new-translation-other').val()
}
else
result = $('#new-translation').val()
$.ajax("/translations", {
type: 'POST',
data: {
translation: {
locale: @locale,
key: @key,
value: result
}
}
})

cancel: =>
# NOOP

remove_extra_chars: (_str) =>
_str.replace(/^\[\"/, '').replace(/\"\]$/,'').replace(/'/g, '&#39;')


5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
before_filter :ensure_locale
before_filter :initialize_timeline_session
before_filter :setup_timeline
before_filter :track_locales

helper_method :current_user
helper_method :user_signed_in?
Expand Down Expand Up @@ -179,4 +180,8 @@ def ckeditor_authenticate
# return true
# end

def track_locales
I18n.track_locales = true if can_read?('Locale Admin')
end

end
53 changes: 53 additions & 0 deletions app/controllers/translations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- encoding : utf-8 -*-"
class TranslationsController < ApplicationController

before_filter :ensure_translator_facility

def index
@translations = TRANSLATION_STORE
end

def show
@translations = TRANSLATION_STORE
render :index
end


# {"translation"=>{"locale"=>"en", "key"=>"close", "value"=>"close"}, "action"=>"create", "controller"=>"translations"}):
def create
@locale = params[:translation][:locale]
@key = params[:translation][:key]
@value = save_by_key(@locale, @key, params[:translation][:value])
end

private
def ensure_translator_facility
redirect_to signin_path, alert: t(:access_denied) unless can_read?('Locale Admin')
end

def save_by_key(locale, key, value)
I18n.backend.store_translations(locale, {key => value}, :escape => false)
value
end

def prepare_locale_value_for_js(locale,key)
_locale=I18n.locale
I18n.locale = locale
_rc = (_in = I18n.t(key.to_sym)).class == Hash ? combine_store_keys(locale,key,_in) : _in
I18n.locale=_locale
_rc
end
helper_method :prepare_locale_value_for_js

def combine_store_keys(locale,key,_in)
_zero = I18n.backend.backends.first.store.get( [locale,key,"zero"].join('.') )
if _zero
_one = I18n.backend.backends.first.store.get( [locale,key,"one"].join('.') )
_other = I18n.backend.backends.first.store.get( [locale,key,"other"].join('.') )
{ :zero => _zero, :one => _one, :other => _other }.to_json
else
_in.to_json
end
end

end
7 changes: 4 additions & 3 deletions app/models/facility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ class Facility

unless defined?(WAT_APPLICATION_FACILITIES)
WAT_APPLICATION_FACILITIES = [
[I18n.translate(:admin), 'Admin'],
[I18n.translate(:author), 'Author'],
[I18n.translate(:maintainer), 'Maintainer']
['Admin', 'Admin'],
['Author', 'Author'],
['Maintainer', 'Maintainer'],
['Locale Admin', 'Locale Admin']
]
end

Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
.span12
= yield



= render "translations/used_locales" if can_read?('Locale Admin') && params[:controller] != 'translations'
%footer
.container
= render 'shared/section_footer'
Expand Down
2 changes: 1 addition & 1 deletion app/views/pages/index.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $(".pagination").html("<%= escape_javascript( page_navigation_links( @pages, 'pa
$('#load_more').html('<p style="text-align:center; margin-top: 20px;"><%= escape_javascript(t(:end_of_list)) -%></p>');
$(".pagination").hide();
<% else %>
$('#load_more').html( '<%= escape_javascript(link_to_function(t(:load_more),"insert_load_button( 'load_more','#{t(:loading)}',"+
$('#load_more').html( '<%= escape_javascript(link_to_function(t(:load_more_link),"insert_load_button( 'load_more','#{t(:loading)}',"+
"'#{pages_path(:page => (params[:page] ? (params[:page].to_i+1) : 2),:order => (params[:order] ? params[:order].to_sym : nil), :direction => (params[:direction] ? params[:direction].to_sym : nil))}')", :id => "load_more_link" )) -%>');
$('#page<%= @pages.current_page -%>').slideDown();
setTimeout("checkScroll()", 500);
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_load_more.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%p#load_more.load_more
- if resources.total_pages > resources.current_page
= link_to_function( t(:load_more), |
= link_to_function( t(:load_more_link), |
"insert_load_button( 'load_more','#{t(:loading)}',"+ |
"'#{path}?page=#{resources.current_page+1}&order=#{params[:order]}&direction=#{params[:direction]}'"+ |
");return false;", |
Expand Down
4 changes: 4 additions & 0 deletions app/views/translations/_locale_tr.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%tr{id: "translation-tr-#{locale}-#{key.gsub(/\./, '-')}", class: 'locale-line', 'data-locale' => locale, 'data-key' => key, 'data-value' => _json = prepare_locale_value_for_js(locale,key) }
%td= locale.to_s
- I18n.locale = locale
%td{id: "translation-#{locale}-#{key}"}= _json
14 changes: 14 additions & 0 deletions app/views/translations/_used_locales.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.well
.pull-right
= form_tag edit_page_translations_path do
= hidden_field_tag :used_keys, used_keys: I18n.used_locales
= submit_tag t(:edit_translations), class: "btn btn-primary"
%h3= t(:used_locales)

%ul
- I18n.used_locales.each do |key|
%li
%strong= key
="=>"
= t(key)

1 change: 1 addition & 0 deletions app/views/translations/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$("#translation-tr-<%= @locale %>-<%= @key.gsub(/\./, '-') %>").replaceWith('<%= j render "locale_tr", locale: @locale, key: @key %>');
12 changes: 12 additions & 0 deletions app/views/translations/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%h1= title t(:translations) + " (#{I18n.locale})"

- gui_locale = I18n.locale
%table.full-width.table.stripped
- eval(params[:used_keys])[:used_keys].map {|k| k.to_s.gsub(/\A..\./,'')}.uniq.each do |key|
%tr
%td{colspan: 1+I18n.available_locales.count}
%h3= key
- I18n.available_locales.each do |locale|
- I18n.locale=locale
= render 'locale_tr', locale: locale, key: key
- I18n.locale=gui_locale
2 changes: 1 addition & 1 deletion app/views/users/index.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $(".pagination").html("<%= escape_javascript( page_navigation_links( @users, 'pa
$('#load_more').html('<p style="text-align:center; margin-top: 20px;"><%= escape_javascript(t(:end_of_list)) -%></p>');
$(".pagination").hide();
<% else %>
$('#load_more').html( '<%= escape_javascript(link_to_function(t(:load_more),"insert_load_button( 'load_more','#{t(:loading)}',"+
$('#load_more').html( '<%= escape_javascript(link_to_function(t(:load_more_link),"insert_load_button( 'load_more','#{t(:loading)}',"+
"'#{users_path(:page => (params[:page] ? (params[:page].to_i+1) : 2),:order => (params[:order] ? params[:order].to_sym : nil), :direction => (params[:direction] ? params[:direction].to_sym : nil))}')", :id => "load_more_link" )) -%>');
$('#page<%= @users.current_page -%>').slideDown();
setTimeout("checkScroll()", 500);
Expand Down
22 changes: 22 additions & 0 deletions config/initializers/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module I18n

def self.track_locales=(new_value)
@track_locales = new_value
@used_locales = []
end

def self.track_locales?
@track_locales ||= false
end

def self.translate(*args)
return super unless track_locales?
@used_locales += [args[0]]
super
end

def self.used_locales
@used_locales.sort{|a,b| a.to_s <=> b.to_s}.uniq
end

end
15 changes: 15 additions & 0 deletions config/initializers/i18n_backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def get_redis_db_id
_yml_file = File.expand_path('../../redis.yml', __FILE__)
if File.exist?(_yml_file)
yml = YAML.load( File.read(_yml_file) )
db = yml[Rails.env]['db']
Rails.logger.info("Using Redis database #{db} as defined in #{_yml_file}")
db
else
Rails.logger.warn("File #{_yml_file} doesn't exist. Using default Redis database (0)")
0
end
end

TRANSLATION_STORE = Redis.new(db: get_redis_db_id)
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(TRANSLATION_STORE), I18n.backend)
3 changes: 2 additions & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,6 @@ de:
has_accepted_invitation: "Hat die Einladung akzeptiert."
scroll_to_top: 'Zum Anfang der Seite'
loading: 'Laden …'
load_more: 'Mehr anzeigen …'
load_more_link: 'Mehr anzeigen …'
end_of_list: ' -- ENDE -- '
edit_translations: "Diese Übersetzungen bearbeiten"
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,6 @@ en:
has_accepted_invitation: "Has accepted invitation."
scroll_to_top: 'Scroll to top'
loading: 'Loading …'
load_more: 'Show more …'
load_more_link: 'Show more …'
end_of_list: ' -- END -- '
edit_translations: "Edit this translations"
8 changes: 8 additions & 0 deletions config/redis.sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test:
db: 0

development:
db: 1

production:
db: 1
10 changes: 9 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
# Default
root :to => "home#index"

# Authentication Routs
# Translations
resources :translations do
collection do
post '/edit_page_translations' => :index, :as => :edit_page
end
end


# Authentication Routes
match '/auth/:provider/callback' => 'sessions#create'
match '/auth/identity/register' => 'users#create'
match '/signin' => 'sessions#new', :as => :signin
Expand Down
5 changes: 2 additions & 3 deletions config/settings/development.yml_sample
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ developer_mail_address: "[email protected]"
# show_user_menu: true
# supress_global_search: false

# default, when not defined = 4
paginate_users_per_page: 2
paginate_pages_per_page: 2
# paginate_users_per_page: 4
# paginate_pages_per_page: 4

# TIMELINE BEHAVIOR

Expand Down
13 changes: 13 additions & 0 deletions spec/integration/translations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- encoding : utf-8 -*-"
require "rspec"

describe "Translations with redis" do

it "should reads from redis" do
I18n.backend.store_translations(:en, {"test_redis" => "Test redis backend"}, :escape => false)
I18n.t(:test_redis).should == "Test redis backend"
I18n.backend.store_translations(:en, {"test_redis" => nil}, :escape => false)
I18n.t(:test_redis).should == "translation missing: en.test_redis"
end

end
15 changes: 15 additions & 0 deletions spec/requests/translations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- encoding : utf-8 -*-"
require "rspec"

describe TranslationsController do

before(:each) do
user=test_user "Translator", "babelfish", ["Locale Admin"]
sign_in_user name: "Translator", password: "babelfish"
end

it "should list translations keys" do
click_button "Edit this translations"
page.should have_content "edit_profile en Edit profile de Benutzerkonto bearbeiten"
end
end

0 comments on commit c06cc6f

Please sign in to comment.