Permalink
Browse files

Revive Tolk

  • Loading branch information...
1 parent f765f17 commit 1c8ba781f796b22d681413dca380bcd875ac347c @lifo lifo committed Apr 7, 2010
View
@@ -0,0 +1 @@
+tmp/**/*
@@ -4,10 +4,12 @@ def index
end
def show
- @locale = Locale.find(params[:id])
+ @locale = Locale.find_by_name!(params[:id])
+ render :primary_locale if @locale.primary?
end
def create
- redirect_to(Locale.create(params[:locale]))
+ Locale.create!(params[:locale])
+ redirect_to :action => :index
end
end
@@ -2,13 +2,4 @@ class PhrasesController < ApplicationController
def index
@phrases = Phrase.all
end
-
- def create
- translation_parameters = params[:phrase].delete(:translation)
-
- phrase = Phrase.all.create(params[:phrase])
- translation = Translation.create(translation_parameters.merge(:phrase => phrase))
-
- redirect_to(translation.locale)
- end
end
@@ -1,7 +1,21 @@
class TranslationsController < ApplicationController
+ before_filter :ensure_no_primary_locale, :only => :update
+
def create
+ locale_id = params[:translation].delete(:locale_id)
+
+ @locale = Locale.find(locale_id)
+ @translation = @locale.translations.new(params[:translation])
+
+ if @translation.save
+ flash[:notice] = 'Translation saved'
+ else
+ flash[:alert] = 'Translation could not be saved'
+ end
+
+ redirect_to @locale
end
-
+
def update
@translation = Translation.find(params[:id])
@translation.update_attributes!(params[:translation])
View
@@ -2,19 +2,35 @@ class Locale < ActiveRecord::Base
has_many :phrases, :through => :translations
has_many :translations, :include => :phrase
- LOCALES_CONFIG_PATH = "#{Rails.root}/config/locales"
+ cattr_accessor :locales_config_path
+ self.locales_config_path = "#{Rails.root}/config/locales"
+
+ cattr_accessor :primary_locale_name
+
+ include Tolk::Sync
+
+ validates_uniqueness_of :name
class << self
- def dump_all(to = LOCALES_CONFIG_PATH)
- all.each do |locale|
+ def primary_locale
+ raise "Primary locale is not set. Please set Locale.primary_locale_name in your application's config file" unless self.primary_locale_name
+
+ Locale.find_or_create_by_name(self.primary_locale_name)
+ end
+
+ def secondary_locales
+ Locale.all - [primary_locale]
+ end
+
+ def dump_all(to = self.locales_config_path)
+ secondary_locales.each do |locale|
File.open("#{to}/#{locale.name}.yml", "w+") do |file|
YAML.dump(locale.to_hash, file)
end
end
end
end
-
def phrases_with_translation
translations.collect do |translation|
translation.phrase.translation = translation
@@ -35,7 +51,15 @@ def to_hash
end
end }
end
-
+
+ def to_param
+ name.parameterize
+ end
+
+ def primary?
+ name == self.class.primary_locale_name
+ end
+
private
def unsquish(string, value)
if string.is_a?(String)
View
@@ -1,5 +1,13 @@
class Phrase < ActiveRecord::Base
- has_many :translations
-
+ has_many :translations, :dependent => :destroy do
+ def primary
+ to_a.detect {|t| t.locale_id == Locale.primary_locale.id}
+ end
+
+ def secondary(locale)
+ to_a.detect {|t| t.locale_id == locale.id}
+ end
+ end
+
attr_accessor :translation
end
@@ -1,4 +1,6 @@
class Translation < ActiveRecord::Base
+ validates_presence_of :text
+
belongs_to :phrase
belongs_to :locale
end
@@ -11,6 +11,9 @@
</head>
<body>
+ <ul>
+ <li><%= link_to "Home", locales_path %></li>
+ </ul>
<%= yield %>
</body>
</html>
@@ -1,7 +1,23 @@
<h1>Locales</h1>
<ul>
- <% for locale in @locales %>
- <li><%= link_to locale.name, locale %></li>
+ <% @locales.each do |locale| %>
+ <li>
+ <%= link_to locale.name, locale %><%= " <b>(primary)</b>" if locale.primary? %>
+ </li>
<% end %>
-</ul>
+</ul>
+
+<h1>Create a new Locale</h1>
+
+<% form_for(Locale.new) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.submit 'Create' %>
+ </p>
+<% end %>
@@ -0,0 +1,19 @@
+<h1><%= @locale.name %> <b>(primary)</b></h1>
+
+<h4>Tolk treats primary translations as readonly</h4>
+
+<h2>All translations</h2>
+
+<table>
+ <tr><th>Phrase</th><th>Translation</th></tr>
+ <% @locale.phrases_with_translation.each do |phrase| %>
+ <tr>
+ <td>
+ <%= phrase.key %>
+ </td>
+ <td>
+ <%= phrase.translation.text %>
+ </td>
+ </tr>
+ <% end %>
+</table>
@@ -11,13 +11,14 @@
<%= phrase.key %>
</td>
<td>
- <% remote_form_for(Translation.new(:locale => @locale, :phrase => phrase)) do |f| %>
+ <% form_for(Translation.new(:locale => @locale, :phrase => phrase)) do |f| %>
<%= f.hidden_field :locale_id %>
<%= f.hidden_field :phrase_id %>
<%= f.text_field :text %>
- <%= f.submit "Update" %>
+ <%= f.submit "Update" %> ( <i>Hint:</i> <%= phrase.translations.primary.text -%> )
<% end %>
+
</td>
</tr>
<% end %>
@@ -36,28 +37,9 @@
<td>
<% remote_form_for(phrase.translation) do |f| %>
<%= f.text_field :text %>
- <%= f.submit "Update" %>
+ <%= f.submit "Update" %> ( <i>Hint:</i> <%= phrase.translations.primary.text -%> )
<% end %>
</td>
</tr>
<% end %>
</table>
-
-<table>
-<tr><th>Phrase</th><th>Translation</th></tr>
-<% form_for([ @application, Phrase.new ]) do |f| %>
- <tr>
- <td>
- <%= f.text_field :key %>
- </td>
- <td>
- <% f.fields_for(Translation.new) do |ff| %>
- <%= ff.hidden_field :locale_id, :value => @locale.id %>
- <%= ff.text_field :text %>
- <% end %>
-
- <%= f.submit "Create" %>
- </td>
- </tr>
-<% end %>
-<table>
View
@@ -5,7 +5,7 @@
# ENV['RAILS_ENV'] ||= 'production'
# Specifies gem version of Rails to use when vendor/rails is not present
-RAILS_GEM_VERSION = '2.2.0' unless defined? RAILS_GEM_VERSION
+RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
View
@@ -1,7 +1,7 @@
ActionController::Routing::Routes.draw do |map|
- map.resources :locales, :has_many => :translations
- map.resources :phrases, :has_many => :translations
- map.resources :translations
-
- map.root :locales
+ map.with_options(:path_prefix => '/tolk') do |tolk|
+ tolk.resources :locales, :has_many => :translations
+ tolk.resources :phrases, :has_many => :translations
+ tolk.resources :translations
+ end
end
View
@@ -0,0 +1,2 @@
+module Tolk
+end
View
@@ -0,0 +1,69 @@
+module Tolk
+ module Sync
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ def sync!
+ raise "Primary locale is not set. Please set Locale.primary_locale_name in your application's config file" unless self.primary_locale_name
+
+ translations = read_primary_locale_file
+ sync_phrases(translations)
+ end
+
+ def read_primary_locale_file
+ primary_file = "#{self.locales_config_path}/#{self.primary_locale_name}.yml"
+ raise "Primary locale file #{primary_file} does not exists" unless File.exists?(primary_file)
+
+ flat_hash(YAML::load(IO.read(primary_file))[self.primary_locale_name])
+ end
+
+ private
+
+ def sync_phrases(translations)
+ primary_locale = self.primary_locale
+ secondary_locales = self.secondary_locales
+
+ # Handle deleted phrases
+ translations.present? ? Phrase.destroy_all(["phrases.key NOT IN (?)", translations.keys]) : Phrase.destroy_all
+
+ phrases = Phrase.all
+
+ translations.each do |key, value|
+ # Create phrase and primary translation if missing
+ existing_phrase = phrases.detect {|p| p.key == key} || Phrase.create!(:key => key)
+ translation = existing_phrase.translations.primary || primary_locale.translations.build(:phrase_id => existing_phrase.id)
+
+ # Update primary translation if it's been changed
+ if value.present? && translation.text != value
+ translation.text = value
+ translation.save!
+ end
+
+ # Make sure the translation record exists for all the locales
+ # secondary_locales.each do |locale|
+ # existing_translation = existing_phrase.translations.detect {|t| t.locale_id == locale.id }
+ # locale.translations.create!(:phrase_id => existing_phrase.id) unless existing_translation
+ # end
+ end
+ end
+
+ def flat_hash(data, prefix = '', result = {})
+ data.each do |key, value|
+ current_prefix = prefix.present? ? "#{prefix}.#{key}" : key
+
+ if value.is_a?(Hash)
+ flat_hash(value, current_prefix, result)
+ else
+ result[current_prefix] = value
+ end
+ end
+
+ result
+ end
+
+ end
+
+ end
+end
@@ -0,0 +1,6 @@
+---
+da:
+ cozy: Hyggeligt
+ nested:
+ hello_world: Nedarvet Hej Verden
+ hello_world: Hej Verden
@@ -0,0 +1,6 @@
+---
+en:
+ nested:
+ hello_world: Nested Hello World
+ hello_country: Nested Hello Country
+ hello_world: Hello World
@@ -0,0 +1,3 @@
+---
+se:
+ hello_world: Hejsan Verdon
View
@@ -0,0 +1,5 @@
+---
+en:
+ nested:
+ hello_country: Nested Hello Country
+ hello_world: Hello World
Oops, something went wrong.

0 comments on commit 1c8ba78

Please sign in to comment.