autochthon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a1f254369355f08917225c85eaddcdcefb8bd9c
4
+ data.tar.gz: 30406f867d0a4ace7af61e00eea0a72db6fd1cdc
5
+ SHA512:
6
+ metadata.gz: a26e383f6411d3fa116d19ff66f6296f48942e43cd90c75542c7bafc49b7f28864192b87179b1d18765c38c7c1dba5e9202c5f7348f2c5161fc5e60f74448a3f
7
+ data.tar.gz: 762f221b17a4b6d5e11d5c506d0ac7bdc74cea6a2bbaf9f44f7b890cf454e5332bb3426388f53dc85f0457b649fdcb784585147092200f740445f1955793e4f8
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /web/node_modules/
11
+ *.tern-port
12
+ .ruby-version
13
+ local
14
+ capybara-*
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord'
4
+ gem 'actionview'
5
+
6
+ gem 'i18n'
7
+
8
+ gem 'i18n-active_record',
9
+ :require => 'i18n/active_record'
10
+
11
+ gem 'redis-i18n', '~> 0.6.5'
12
+
13
+ group :test do
14
+ gem "pry"
15
+ gem "byebug"
16
+ gem "sqlite3"
17
+ gem "rspec"
18
+ gem 'capybara-webkit'
19
+ gem 'selenium-webdriver'
20
+ end
21
+
22
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Adam Sokolnicki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,160 @@
1
+ # Autochthon
2
+
3
+ A simple Sinatra app for managing translations stored in YAML files, database or Redis.
4
+
5
+ Look at this as [localeapp](https://www.localeapp.com/) hosted in your application.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'autochthon', github: 'asok/autochthon
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Rails application with ActiveRecord backend
18
+
19
+ #### Setup backend
20
+
21
+ Add this to your Gemfile:
22
+
23
+ ```rb
24
+ gem 'i18n-active_record', :require => 'i18n/active_record'
25
+ ```
26
+
27
+ Create file `config/initializers/autochthon.rb` with content:
28
+
29
+ ```rb
30
+ require 'i18n/backend/active_record'
31
+
32
+ I18n.backend = Autochthon.backend = Autochthon::Backend::ActiveRecord.new
33
+
34
+ Autochthon.mount_point = "your_mount_point"
35
+ ```
36
+
37
+ Probably you will want to use memoize so you don't generate a bunch of queries on each request:
38
+
39
+ ```rb
40
+ require 'i18n/backend/active_record'
41
+
42
+ if I18n::Backend::ActiveRecord::Translation.table_exists?
43
+ I18n.backend = Autochthon.backend = Autochthon::Backend::ActiveRecord.new
44
+ I18n.backend.class.include(I18n::Backend::Memoize)
45
+ end
46
+
47
+ Autochthon.mount_point = "your_mount_point"
48
+ ```
49
+
50
+ NOTE: this will not work when you have your web server running several ruby processes.
51
+ That is the process in which you'll update the translation will see the new value for it. But other requests might be served by another process which will have the old value.
52
+ If that's the case consider:
53
+ * not using memoization
54
+ * caching the translation with an expiration option
55
+ * using redis backend without memoization
56
+
57
+ #### Create table
58
+
59
+ Create the translations table:
60
+
61
+ ```
62
+ bundle exec rake autochthon:create
63
+ ```
64
+
65
+ ### Rails application with Redis backend
66
+
67
+ #### Setup backend
68
+
69
+ Add this to your Gemfile:
70
+
71
+ ```rb
72
+ gem 'redis-i18n'
73
+ ```
74
+
75
+ Create file `config/initializers/autochthon.rb` with content:
76
+
77
+ ```rb
78
+ require 'i18n/backend/redis'
79
+
80
+ I18n.backend = Autochthon.backend = Autochthon::Backend::Redis.new
81
+
82
+ Autochthon.mount_point = "your_mount_point"
83
+ ```
84
+
85
+ ### Rails application with Simple (YAML) backend
86
+
87
+ NOTE: this backend operates in memory only. Meaning that your translations will not be persisted anywhere.
88
+
89
+ Create file `config/initializers/autochthon.rb` with content:
90
+
91
+ ```rb
92
+ I18n.backend = Autochthon.backend = Autochthon::Backend::Simple.new
93
+
94
+ Autochthon.mount_point = "your_mount_point"
95
+ ```
96
+
97
+ ### Mount
98
+
99
+ Add this to the routes:
100
+
101
+ ```rb
102
+ mount Autochthon::Web => Autochthon.mount_point, :as => 'autochthon''
103
+ ```
104
+
105
+ Probably you will want to authenticate. In case you are using [devise](https://github.com/plataformatec/devise) you can do:
106
+
107
+ ```rb
108
+ authenticate(:admin) do
109
+ mount Autochthon::Web => '/autochthon', :as Autochthon.mount_point
110
+ end
111
+ ```
112
+
113
+ ### Import
114
+
115
+ You can import existing translations from the I18n's simple backend (YAML files):
116
+
117
+ ```
118
+ rake autochthon:import
119
+ ```
120
+
121
+ If you want to only import specific locales you can do so:
122
+
123
+ ```
124
+ LOCALES="en,fr" rake autochthon:import
125
+ ```
126
+
127
+ ### Filling missing translations via right click
128
+
129
+ In your `app/assets/javascripts/application.js` file do:
130
+
131
+ ```js
132
+ //= require 'autochthon/application'
133
+ ```
134
+
135
+ Now you can right click on the missing translations to fill them in.
136
+
137
+ ## Examples
138
+
139
+ * rails application using ActiveRecord backend https://github.com/asok/autochthon_rails_example_app
140
+
141
+ ## Development
142
+
143
+ In the dir `web/` run `npm install` fallowed by `npm run watch`.
144
+
145
+ That will compile the js files and put them in `public/bundle.js`.
146
+
147
+ Note: for a release you can run `npm run build`.
148
+
149
+ ### Testing
150
+
151
+ `bundle exec rspec`
152
+
153
+ ## Contributing
154
+
155
+ Bug reports and pull requests are welcome on GitHub at https://github.com/asok/autochthon.
156
+
157
+
158
+ ## License
159
+
160
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autochthon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autochthon"
8
+ spec.version = Autochthon::VERSION
9
+ spec.authors = ["Adam Sokolnicki"]
10
+ spec.email = ["adam.sokolnicki@gmail.com"]
11
+
12
+ spec.summary = %q{Sinatra app for managing I18n translations}
13
+ spec.description = %q{Sinatra app for managing I18n translations stored in YAML files, database or Redis}
14
+ spec.homepage = "https://github.com/asok/autochthon"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_dependency "sinatra"
26
+ spec.add_dependency "sinatra-contrib"
27
+ spec.add_dependency "i18n"
28
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "local"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,3 @@
1
+ require './lib/autochthon/web'
2
+
3
+ run Autochthon::Web
@@ -0,0 +1,28 @@
1
+ require "autochthon/version"
2
+ require "autochthon/web"
3
+ require 'autochthon/engine'
4
+ require 'autochthon/backend/simple'
5
+ require 'autochthon/backend/active_record'
6
+ require 'autochthon/backend/redis'
7
+
8
+ module Autochthon
9
+ module Methods
10
+ def backend=(backend)
11
+ @@backend = backend
12
+ end
13
+
14
+ def backend
15
+ @@backend
16
+ end
17
+
18
+ def mount_point=(mount_point)
19
+ @@mount_point = mount_point
20
+ end
21
+
22
+ def mount_point
23
+ @@mount_point
24
+ end
25
+ end
26
+
27
+ extend Methods
28
+ end
@@ -0,0 +1,36 @@
1
+ require 'autochthon/backend/fetching_all'
2
+
3
+ begin
4
+ require 'i18n/backend/active_record'
5
+
6
+ module Autochthon
7
+ module Backend
8
+ class ActiveRecord
9
+ include I18n::Backend::ActiveRecord::Implementation
10
+
11
+ include FetchingAll
12
+
13
+ def all_for_locale(locale)
14
+ Hash[I18n::Backend::ActiveRecord::Translation
15
+ .where(locale: locale)
16
+ .pluck(:key, :value)]
17
+ end
18
+
19
+ class Migration < ::ActiveRecord::Migration
20
+ def change
21
+ create_table :translations do |t|
22
+ t.string :locale
23
+ t.string :key
24
+ t.text :value
25
+ t.text :interpolations
26
+ t.boolean :is_proc, default: false
27
+
28
+ t.timestamps null: false
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ rescue LoadError
36
+ end
@@ -0,0 +1,20 @@
1
+ module Autochthon
2
+ module Backend
3
+ module FetchingAll
4
+ include I18n::Backend::Flatten
5
+
6
+ def all(locales = available_locales)
7
+ locales.inject([]) do |out, locale|
8
+ flatten_keys(all_for_locale(locale), false) do |key, value|
9
+ out << {key: key, value: value, locale: locale}
10
+ end
11
+ out
12
+ end
13
+ end
14
+
15
+ def all_for_locale(locale)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'i18n/backend/redis'
3
+
4
+ module Autochthon
5
+ module Backend
6
+ class Redis < I18n::Backend::Redis
7
+ include FetchingAll
8
+
9
+ def all_for_locale(locale)
10
+ store.keys("#{locale}.*").inject({}) do |translations, key|
11
+ main_key = key[(locale.size+1)..-1]
12
+ translations.merge(main_key => translate(locale, main_key))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ rescue LoadError
19
+ end
@@ -0,0 +1,15 @@
1
+ require 'autochthon/backend/fetching_all'
2
+
3
+ module Autochthon
4
+ module Backend
5
+ class Simple
6
+ include I18n::Backend::Simple::Implementation
7
+
8
+ include FetchingAll
9
+
10
+ def all_for_locale(locale)
11
+ translate(locale, ".")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Autochthon
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Autochthon
4
+
5
+ rake_tasks do
6
+ load 'autochthon/tasks/rails.rake'
7
+ end
8
+ end
9
+ end if defined?(::Rails)
@@ -0,0 +1,28 @@
1
+ require 'autochthon/backend/active_record'
2
+
3
+ namespace :autochthon do
4
+ desc "Create translations table"
5
+ task create: :environment do
6
+ Autochthon::Backend::ActiveRecord::Migration.new.change
7
+ end
8
+
9
+ desc "Reads all translations from yml files and inserts them into db"
10
+ task import: :environment do
11
+ backend = Autochthon::Backend::Simple.new
12
+
13
+ locales = (ENV['LOCALES'] || '').split(',')
14
+ locales = backend.available_locales if locales.empty?
15
+
16
+ fn = -> do
17
+ backend.all(locales).each do |t|
18
+ Autochthon.backend.store_translations(t[:locale], {t[:key] => t[:value]})
19
+ end
20
+ end
21
+
22
+ if Autochthon.backend.class == Autochthon::Backend::ActiveRecord
23
+ ActiveRecord::Base.transaction(&fn)
24
+ else
25
+ fn.call
26
+ end
27
+ end
28
+ end