go_translate_yourself 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -22,8 +22,8 @@ spec = Gem::Specification.new do |s|
22
22
  s.name = "go_translate_yourself"
23
23
  s.summary = "Insert GoTranslateYourself summary."
24
24
  s.description = "Insert GoTranslateYourself description."
25
- s.files = FileList["[A-Z]*", "lib/**/*"]
26
- s.version = "0.0.1"
25
+ s.files = FileList["[A-Z]*", "lib/**/*", "app/**/*", "config/routes.rb"]
26
+ s.version = "0.0.3"
27
27
  end
28
28
 
29
29
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -32,4 +32,4 @@ end
32
32
  desc "Install the gem #{spec.name}-#{spec.version}.gem"
33
33
  task :install do
34
34
  system("gem install pkg/#{spec.name}-#{spec.version}.gem --no-ri --no-rdoc")
35
- end
35
+ end
@@ -0,0 +1,27 @@
1
+ module GoTranslateYourself
2
+ class SiteTranslationsController < ApplicationController
3
+ before_filter :auth
4
+
5
+ def edit
6
+ @keys = GoTranslateYourself.current_store.keys_without_prefix
7
+ @locales = GoTranslateYourself.locales
8
+ render :layout => GoTranslateYourself.layout_name
9
+ end
10
+
11
+ def update
12
+ if params[:translations] && !params[:translations].empty?
13
+ params[:translations].each do |key, value|
14
+ GoTranslateYourself.current_store[key] = value
15
+ end
16
+ end
17
+ redirect_to site_translations_path
18
+ end
19
+
20
+ private
21
+
22
+ def auth
23
+ GoTranslateYourself.auth_handler.bind(self).call if GoTranslateYourself.auth_handler.is_a? Proc
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ module GoTranslateYourself
4
+ class BaseStore
5
+
6
+ def keys
7
+ if @dev_translations.nil? || Rails.env.development?
8
+ load_dev_translations
9
+
10
+ @keys = GoTranslateYourself.locales.collect {|lang| keys_without_prefix.collect {|key| "#{lang}.#{key}"} }.flatten
11
+ end
12
+
13
+ @keys
14
+ end
15
+
16
+ def default_translation(key)
17
+ load_dev_translations unless @dev_translations
18
+ @dev_translations[key.to_s.gsub(/^[a-z]*\./, "")]
19
+ end
20
+
21
+ def keys_without_prefix
22
+ load_dev_translations unless @dev_translations
23
+ @dev_translations.keys
24
+ end
25
+
26
+ protected
27
+
28
+ def load_dev_translations
29
+ dev_translations = YAML.load_file(File.join(Rails.root, "config", "locales", "dev.yml"))
30
+ @dev_translations = {}
31
+ flatten_keys(nil, dev_translations["dev"], @dev_translations)
32
+ end
33
+
34
+ def flatten_keys(current_key, hash, dest_hash)
35
+ hash.each do |key, value|
36
+ full_key = [current_key, key].compact.join('.')
37
+ if value.kind_of?(Hash)
38
+ flatten_keys full_key, value, dest_hash
39
+ else
40
+ dest_hash[full_key] = value
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,28 @@
1
+ module GoTranslateYourself
2
+ class MongoStore < BaseStore
3
+ def initialize(collection)
4
+ @collection = collection
5
+ end
6
+
7
+ def []=(key, value)
8
+ collection.update({_id: key}, {'$set' => {:value => value}}, upsert: true, safe: true)
9
+ end
10
+
11
+ def [](key)
12
+ if document = collection.find_one(_id: key)
13
+ document["value"]
14
+ else
15
+ default_translation(key)
16
+ end
17
+ end
18
+
19
+ def clear_database
20
+ collection.drop
21
+ end
22
+
23
+ private
24
+
25
+ def collection; @collection; end
26
+ end
27
+ end
28
+
@@ -0,0 +1,27 @@
1
+ <%= form_tag update_site_translations_path, :method => :post do %>
2
+ <table>
3
+ <tr>
4
+ <th>Key</th>
5
+ <th>Default</th>
6
+ <% @locales.each do |locale| %>
7
+ <th><%= locale %></th>
8
+ <% end %>
9
+ </tr>
10
+ <% @keys.each do |key| %>
11
+ <tr>
12
+ <td>
13
+ <%= key %>
14
+ </td>
15
+ <td>
16
+ <%= GoTranslateYourself.current_store.default_translation("#{dev}.#{key}") %>
17
+ </td>
18
+ <% @locales.each do |locale| %>
19
+ <td>
20
+ <%= text_field_tag "translations[#{locale}.#{key}]", GoTranslateYourself.current_store["#{locale}.#{key}"] %>
21
+ </td>
22
+ <% end %>
23
+ </tr>
24
+ <% end %>
25
+ </table>
26
+ <%= submit_tag "Save" %>
27
+ <% end %>
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ match 'site_translations' => 'GoTranslateYourself::SiteTranslations#edit', :as => :site_translations, :method => :get
3
+ match 'site_translations/save' => 'GoTranslateYourself::SiteTranslations#update', :as => :update_site_translations, :method => :post
4
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors: []
12
12
 
@@ -34,6 +34,11 @@ files:
34
34
  - Rakefile
35
35
  - lib/go_translate_yourself/engine.rb
36
36
  - lib/go_translate_yourself.rb
37
+ - app/controllers/go_translate_yourself/site_translations_controller.rb
38
+ - app/modles/go_translate_yourself/base_store.rb
39
+ - app/modles/go_translate_yourself/mongo_store.rb
40
+ - app/views/go_translate_yourself/site_translations/edit.html.erb
41
+ - config/routes.rb
37
42
  has_rdoc: true
38
43
  homepage:
39
44
  licenses: []