r18n-rails 4.0.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog.md +383 -0
- data/README.md +73 -59
- data/lib/r18n-rails.rb +32 -20
- data/lib/r18n-rails/filters.rb +2 -2
- data/lib/r18n-rails/helpers.rb +5 -6
- data/lib/r18n-rails/{controller.rb → hooks_helper.rb} +39 -15
- data/lib/r18n-rails/translated.rb +5 -4
- data/lib/r18n-rails/version.rb +7 -0
- metadata +217 -69
- data/.rspec +0 -1
- data/Rakefile +0 -15
- data/r18n-rails.gemspec +0 -32
- data/spec/app/app/controllers/application_controller.rb +0 -5
- data/spec/app/app/controllers/test_controller.rb +0 -50
- data/spec/app/app/i18n/en.yml +0 -6
- data/spec/app/app/i18n/filters.rb +0 -5
- data/spec/app/app/i18n/ru.yml +0 -2
- data/spec/app/app/mailers/test_mailer.rb +0 -9
- data/spec/app/app/models/post.rb +0 -6
- data/spec/app/app/views/test/format.html.erb +0 -1
- data/spec/app/app/views/test/helpers.html.erb +0 -4
- data/spec/app/app/views/test/safe.html.erb +0 -1
- data/spec/app/app/views/test_mailer/test.text.erb +0 -3
- data/spec/app/config.ru +0 -6
- data/spec/app/config/application.rb +0 -28
- data/spec/app/config/boot.rb +0 -6
- data/spec/app/config/environment.rb +0 -7
- data/spec/app/config/environments/test.rb +0 -14
- data/spec/app/config/initializers/r18n.rb +0 -4
- data/spec/app/config/initializers/secret_token.rb +0 -3
- data/spec/app/config/locales/en.yml +0 -7
- data/spec/app/config/locales/ru.yml +0 -20
- data/spec/app/config/routes.rb +0 -15
- data/spec/app/db/migrate/20091218123631_create_posts.rb +0 -14
- data/spec/app/db/schema.rb +0 -22
- data/spec/rails_spec.rb +0 -183
- data/spec/spec_helper.rb +0 -10
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--format documentation --colour --warnings --require spec_helper
|
data/Rakefile
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
|
5
|
-
require 'bundler/setup'
|
6
|
-
Bundler::GemHelper.install_tasks
|
7
|
-
|
8
|
-
require 'rspec/core/rake_task'
|
9
|
-
RSpec::Core::RakeTask.new
|
10
|
-
task default: :spec
|
11
|
-
|
12
|
-
task :clobber_package do
|
13
|
-
rm_rf 'pkg'
|
14
|
-
end
|
15
|
-
task clobber: [:clobber_package]
|
data/r18n-rails.gemspec
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../r18n-core/lib/r18n-core/version'
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.platform = Gem::Platform::RUBY
|
7
|
-
s.name = 'r18n-rails'
|
8
|
-
s.version = R18n::VERSION.dup
|
9
|
-
s.date = Time.now.strftime('%Y-%m-%d')
|
10
|
-
|
11
|
-
s.summary = 'R18n for Rails'
|
12
|
-
s.description = <<-DESC
|
13
|
-
Out-of-box R18n support for Ruby on Rails.
|
14
|
-
It is just a wrapper for R18n Rails API and R18n core libraries.
|
15
|
-
R18n has nice Ruby-style syntax, filters, flexible locales, custom loaders,
|
16
|
-
translation support for any classes, time and number localization, several
|
17
|
-
user language support, agnostic core package with out-of-box support for
|
18
|
-
Rails, Sinatra and desktop applications.
|
19
|
-
DESC
|
20
|
-
|
21
|
-
s.files = `git ls-files`.split("\n")
|
22
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
-
s.extra_rdoc_files = ['README.md', 'LICENSE']
|
24
|
-
s.require_path = 'lib'
|
25
|
-
|
26
|
-
s.author = 'Andrey Sitnik'
|
27
|
-
s.email = 'andrey@sitnik.ru'
|
28
|
-
s.homepage = 'https://github.com/r18n/r18n/tree/master/r18n-rails'
|
29
|
-
s.license = 'LGPL-3.0'
|
30
|
-
|
31
|
-
s.add_dependency 'r18n-rails-api', "= #{R18n::VERSION}"
|
32
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class TestController < ApplicationController
|
4
|
-
layout false
|
5
|
-
|
6
|
-
before_action :reload_r18n, only: :filter
|
7
|
-
|
8
|
-
def locales
|
9
|
-
render plain: R18n.get.locales.map(&:code).join(', ')
|
10
|
-
end
|
11
|
-
|
12
|
-
def translations
|
13
|
-
render plain: "R18n: #{R18n.get.r18n.translations}. " \
|
14
|
-
"Rails I18n: #{R18n.get.i18n.translations}"
|
15
|
-
end
|
16
|
-
|
17
|
-
def available
|
18
|
-
render plain: R18n.get.available_locales.map(&:code).sort.join(' ')
|
19
|
-
end
|
20
|
-
|
21
|
-
def helpers
|
22
|
-
@from_controller = r18n.user.name
|
23
|
-
render
|
24
|
-
end
|
25
|
-
|
26
|
-
def untranslated
|
27
|
-
render plain: R18n.get.user.not.exists.to_s
|
28
|
-
end
|
29
|
-
|
30
|
-
def controller
|
31
|
-
render plain: "#{t.user.name} #{r18n.t.user.name} #{t('user.name')}"
|
32
|
-
end
|
33
|
-
|
34
|
-
def time
|
35
|
-
render plain:
|
36
|
-
l(Time.at(0).utc) + "\n" + l(Time.at(0).utc, format: :short)
|
37
|
-
end
|
38
|
-
|
39
|
-
def human_time
|
40
|
-
render plain: l(Time.now, :human)
|
41
|
-
end
|
42
|
-
|
43
|
-
def filter
|
44
|
-
render plain: t.ruby
|
45
|
-
end
|
46
|
-
|
47
|
-
def format
|
48
|
-
render
|
49
|
-
end
|
50
|
-
end
|
data/spec/app/app/i18n/en.yml
DELETED
data/spec/app/app/i18n/ru.yml
DELETED
data/spec/app/app/models/post.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
<%= number_to_currency(1000.12345, precision: 1) %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= t.html %> <%= t.user.no_tr %>
|
data/spec/app/config.ru
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'boot'
|
4
|
-
|
5
|
-
require 'active_record/railtie'
|
6
|
-
require 'action_controller/railtie'
|
7
|
-
require 'action_mailer/railtie'
|
8
|
-
require 'rails/test_unit/railtie'
|
9
|
-
|
10
|
-
if defined?(Bundler)
|
11
|
-
Bundler.require(*Rails.groups(assets: %w[development test]))
|
12
|
-
end
|
13
|
-
|
14
|
-
module App
|
15
|
-
class Application < Rails::Application
|
16
|
-
config.time_zone = 'UTC'
|
17
|
-
config.i18n.default_locale = :ru
|
18
|
-
config.encoding = 'utf-8'
|
19
|
-
|
20
|
-
config.active_record.sqlite3.represent_boolean_as_integer = true
|
21
|
-
|
22
|
-
def config.database_configuration
|
23
|
-
sqlite = { 'adapter' => 'sqlite3', 'database' => ':memory:' }
|
24
|
-
sqlite['adapter'] = 'jdbcsqlite3' if RUBY_PLATFORM == 'java'
|
25
|
-
{ 'development' => sqlite, 'test' => sqlite, 'production' => sqlite }
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/spec/app/config/boot.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
App::Application.configure do
|
4
|
-
config.cache_classes = true
|
5
|
-
config.serve_static_files = true
|
6
|
-
config.static_cache_control = 'public, max-age=3600'
|
7
|
-
config.eager_load = false
|
8
|
-
config.consider_all_requests_local = true
|
9
|
-
config.action_controller.perform_caching = false
|
10
|
-
config.action_dispatch.show_exceptions = false
|
11
|
-
config.action_controller.allow_forgery_protection = false
|
12
|
-
config.active_support.deprecation = :stderr
|
13
|
-
config.action_mailer.delivery_method = :test
|
14
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
ru:
|
2
|
-
number:
|
3
|
-
format:
|
4
|
-
delimiter: ' '
|
5
|
-
precision: 3
|
6
|
-
separator: ','
|
7
|
-
significant: false
|
8
|
-
strip_insignificant_zeros: false
|
9
|
-
precision:
|
10
|
-
format:
|
11
|
-
delimiter: ' '
|
12
|
-
currency:
|
13
|
-
format:
|
14
|
-
delimiter: ' '
|
15
|
-
format: '%n %u'
|
16
|
-
precision: 2
|
17
|
-
separator: ','
|
18
|
-
significant: false
|
19
|
-
strip_insignificant_zeros: false
|
20
|
-
unit: руб.
|
data/spec/app/config/routes.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
App::Application.routes.draw do
|
4
|
-
get '/locales', to: 'test#locales'
|
5
|
-
get '/translations', to: 'test#translations'
|
6
|
-
get '/available', to: 'test#available'
|
7
|
-
get '/helpers', to: 'test#helpers'
|
8
|
-
get '/untranslated', to: 'test#untranslated'
|
9
|
-
get '/controller', to: 'test#controller'
|
10
|
-
get '/time', to: 'test#time'
|
11
|
-
get '/human_time', to: 'test#human_time'
|
12
|
-
get '/filter', to: 'test#filter'
|
13
|
-
get '/format', to: 'test#format'
|
14
|
-
get '/safe', to: 'test#safe'
|
15
|
-
end
|
data/spec/app/db/schema.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# This file is auto-generated from the current state of the database. Instead
|
4
|
-
# of editing this file, please use the migrations feature of Active Record to
|
5
|
-
# incrementally modify your database, and then regenerate
|
6
|
-
# this schema definition.
|
7
|
-
#
|
8
|
-
# Note that this schema.rb definition is the authoritative source for your
|
9
|
-
# database schema. If you need to create the application database on another
|
10
|
-
# system, you should be using db:schema:load, not running all the migrations
|
11
|
-
# from scratch. The latter is a flawed and unsustainable approach
|
12
|
-
# (the more migrations you'll amass, the slower it'll run
|
13
|
-
# and the greater likelihood for issues).
|
14
|
-
#
|
15
|
-
# It's strongly recommended to check this file into your version control system.
|
16
|
-
|
17
|
-
ActiveRecord::Schema.define(version: 20091218123631) do
|
18
|
-
create_table 'posts', force: true do |t|
|
19
|
-
t.string 'title_en'
|
20
|
-
t.string 'title_ru'
|
21
|
-
end
|
22
|
-
end
|
data/spec/rails_spec.rb
DELETED
@@ -1,183 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe TestController, type: :controller do
|
4
|
-
render_views
|
5
|
-
|
6
|
-
it 'uses default locale' do
|
7
|
-
get :locales
|
8
|
-
expect(response).to have_http_status(200)
|
9
|
-
expect(response.body).to eq 'ru'
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'gets locale from param' do
|
13
|
-
get :locales, params: { locale: 'ru' }
|
14
|
-
expect(response).to have_http_status(200)
|
15
|
-
expect(response.body).to eq 'ru, en'
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'gets locale from session' do
|
19
|
-
get :locales, session: { locale: 'ru' }
|
20
|
-
expect(response).to have_http_status(200)
|
21
|
-
expect(response.body).to eq 'ru, en'
|
22
|
-
end
|
23
|
-
|
24
|
-
describe 'locales from http' do
|
25
|
-
it 'gets from regular locales' do
|
26
|
-
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ru,fr;q=0.9'
|
27
|
-
get :locales
|
28
|
-
expect(response).to have_http_status(200)
|
29
|
-
expect(response.body).to eq 'ru, fr, en'
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'gets from wildcard' do
|
33
|
-
request.env['HTTP_ACCEPT_LANGUAGE'] = '*'
|
34
|
-
get :locales
|
35
|
-
expect(response).to have_http_status(200)
|
36
|
-
expect(response.body).to eq 'ru'
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'gets from regular locales with wildcard' do
|
40
|
-
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ru, fr;q=0.9, *;q=0.5'
|
41
|
-
get :locales
|
42
|
-
expect(response).to have_http_status(200)
|
43
|
-
expect(response.body).to eq 'ru, fr, en'
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'loads translations' do
|
48
|
-
get :translations, params: { locale: 'en' }
|
49
|
-
expect(response).to have_http_status(200)
|
50
|
-
expect(response.body).to eq 'R18n: supported. Rails I18n: supported'
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'returns available translations' do
|
54
|
-
get :available
|
55
|
-
expect(response).to have_http_status(200)
|
56
|
-
expect(response.body).to eq 'en ru'
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'adds helpers' do
|
60
|
-
get :helpers, params: { locale: 'en' }
|
61
|
-
expect(response).to have_http_status(200)
|
62
|
-
expect(response.body).to eq "Name\nName\nName\nName\n"
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'formats untranslated' do
|
66
|
-
get :untranslated
|
67
|
-
expect(response).to have_http_status(200)
|
68
|
-
expect(response.body).to eq(
|
69
|
-
'user.<span style="color: red">[not.exists]</span>'
|
70
|
-
)
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'adds methods to controller' do
|
74
|
-
get :controller, params: { locale: 'en' }
|
75
|
-
expect(response).to have_http_status(200)
|
76
|
-
expect(response.body).to eq 'Name Name Name'
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'localizes time by Rails I18n' do
|
80
|
-
get :time, params: { locale: 'en' }
|
81
|
-
expect(response).to have_http_status(200)
|
82
|
-
expect(response.body).to eq "Thu, 01 Jan 1970 00:00:00 +0000\n01 Jan 00:00"
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'localizes time by R18n' do
|
86
|
-
get :human_time, params: { locale: 'en' }
|
87
|
-
expect(response).to have_http_status(200)
|
88
|
-
expect(response.body).to eq 'now'
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'translates models' do
|
92
|
-
ActiveRecord::Schema.verbose = false
|
93
|
-
ActiveRecord::Schema.define(version: 20_091_218_130_034) do
|
94
|
-
create_table 'posts', force: true do |t|
|
95
|
-
t.string 'title_en'
|
96
|
-
t.string 'title_ru'
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
expect(Post.unlocalized_getters(:title)).to eq(
|
101
|
-
'ru' => 'title_ru',
|
102
|
-
'en' => 'title_en'
|
103
|
-
)
|
104
|
-
expect(Post.unlocalized_setters(:title)).to eq(
|
105
|
-
'ru' => 'title_ru=',
|
106
|
-
'en' => 'title_en='
|
107
|
-
)
|
108
|
-
|
109
|
-
@post = Post.new
|
110
|
-
@post.title_en = 'Record'
|
111
|
-
|
112
|
-
R18n.set('ru')
|
113
|
-
expect(@post.title).to eq 'Record'
|
114
|
-
|
115
|
-
@post.title = 'Запись'
|
116
|
-
expect(@post.title_ru).to eq 'Запись'
|
117
|
-
expect(@post.title_en).to eq 'Record'
|
118
|
-
expect(@post.title).to eq 'Запись'
|
119
|
-
end
|
120
|
-
|
121
|
-
it 'sets default places' do
|
122
|
-
expect(R18n.default_places).to eq([
|
123
|
-
Rails.root.join('app/i18n'), R18n::Loader::Rails.new
|
124
|
-
])
|
125
|
-
|
126
|
-
R18n.set('en')
|
127
|
-
expect(R18n.get.user.name).to eq 'Name'
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'translates mails' do
|
131
|
-
R18n.set('en')
|
132
|
-
email = TestMailer.test.deliver_now
|
133
|
-
expect(email.body.to_s).to eq "Name\nName\nName\n"
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'reloads filters from app directory' do
|
137
|
-
get :filter, params: { locale: 'en' }
|
138
|
-
expect(response).to have_http_status(200)
|
139
|
-
expect(response.body).to eq 'Rails'
|
140
|
-
expect(R18n::Rails::Filters.loaded).to eq [:rails_custom_filter]
|
141
|
-
|
142
|
-
R18n::Filters.defined[:rails_custom_filter].block = proc { 'No' }
|
143
|
-
get :filter, params: { locale: 'en' }
|
144
|
-
|
145
|
-
expect(response).to have_http_status(200)
|
146
|
-
expect(response.body).to eq 'Rails'
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'escapes html inside R18n' do
|
150
|
-
get :safe, params: { locale: 'en' }
|
151
|
-
expect(response).to have_http_status(200)
|
152
|
-
expect(response.body).to eq(
|
153
|
-
"<b> user.<span style=\"color: red\">[no_tr]</span>\n"
|
154
|
-
)
|
155
|
-
end
|
156
|
-
|
157
|
-
it 'works with Rails build-in herlpers' do
|
158
|
-
get :format
|
159
|
-
expect(response).to have_http_status(200)
|
160
|
-
expect(response.body).to eq "1 000,1 руб.\n"
|
161
|
-
end
|
162
|
-
|
163
|
-
it 'caches I18n object' do
|
164
|
-
R18n.clear_cache!
|
165
|
-
|
166
|
-
get :translations
|
167
|
-
expect(R18n.cache.keys.length).to eq 1
|
168
|
-
|
169
|
-
get :translations
|
170
|
-
expect(R18n.cache.keys.length).to eq 1
|
171
|
-
|
172
|
-
get :translations
|
173
|
-
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ru,fr;q=0.9'
|
174
|
-
expect(R18n.cache.keys.length).to eq 1
|
175
|
-
|
176
|
-
get :translations, params: { locale: 'en' }
|
177
|
-
expect(R18n.cache.keys.length).to eq 2
|
178
|
-
end
|
179
|
-
|
180
|
-
it 'parameterizes strigns' do
|
181
|
-
expect('One two три'.parameterize).to eq 'one-two'
|
182
|
-
end
|
183
|
-
end
|