Skip to content

Commit 31217fa

Browse files
committed
pseudo-localizer for i18n testing
run canvas with LOLCALIZE=true (no, that's not a typo) so you can easily see which strings still need i18n. anything passing through ruby or js I18n.t will be lol-calized (schizo-case, spurious lols, and exclamation marks) test plan: N/A, this doesn't affect normal canvas usage Change-Id: I206efa9ba6e5c67c09b0eef5ea73e0e17e2d3127 Reviewed-on: https://gerrit.instructure.com/23074 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com> Product-Review: Marc LeGendre <marc@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com>
1 parent 5a2d446 commit 31217fa

5 files changed

Lines changed: 84 additions & 2 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
define [], ->
2+
3+
formatter =
4+
0: 'toUpperCase'
5+
1: 'toLowerCase'
6+
7+
# see also lib/i18n/lolcalize.rb
8+
letThereBeLols = (str) ->
9+
# don't want to mangle placeholders, wrappers, etc.
10+
pattern = /(\s*%\{[^\}]+\}\s*|\s*[\n\\`\*_\{\}\[\]\(\)\#\+\-!]+\s*|^\s+)/
11+
result = for token in str.split(pattern)
12+
if token.match(pattern)
13+
token
14+
else
15+
s = ''
16+
for i in [0...token.length]
17+
s += token[i][formatter[i % 2]]()
18+
s = s.replace(/\.( |$)/, '!!?! ')
19+
s = s.replace(/^(\w+)$/, '$1!')
20+
s += " LOL!" if s.length > 2
21+
s
22+
result.join('')
23+
24+
i18nLolcalize = (strOrObj) ->
25+
if typeof strOrObj is 'string'
26+
letThereBeLols strOrObj
27+
else
28+
result = {}
29+
for key, value of strOrObj
30+
result[key] = letThereBeLols(value)
31+
result
32+

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def js_env(hash = {})
8888
:AUTHENTICITY_TOKEN => form_authenticity_token,
8989
:files_domain => HostUrl.file_host(@domain_root_account || Account.default, request.host_with_port),
9090
}
91+
@js_env[:lolcalize] = true if ENV['LOLCALIZE']
9192
end
9293

9394
hash.each do |k,v|

config/initializers/i18n.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
I18n.backend.init_translations
1111
end
1212

13+
I18n.send :extend, I18n::Lolcalize if ENV['LOLCALIZE']
14+
1315
module I18nUtilities
1416
def before_label(text_or_key, default_value = nil, *args)
1517
if default_value

lib/i18n/lolcalize.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module I18n
2+
module Lolcalize
3+
def translate_with_lols(key, options = {})
4+
if options[:default]
5+
key = :lols # so that it doesn't find a real translation
6+
options[:default] = let_there_be_lols(options[:default]) if options[:default].present?
7+
end
8+
translate_without_lols(key, options)
9+
end
10+
11+
# see also app/coffeescripts/str/i18nLolcalize.coffee
12+
def let_there_be_lols(str)
13+
# don't want to mangle placeholders, wrappers, etc.
14+
pattern = /(\s*%\{[^\}]+\}\s*|\s*[\n\\`\*_\{\}\[\]\(\)\#\+\-!]+\s*|^\s+)/
15+
result = str.split(pattern).map do |token|
16+
if token =~ pattern
17+
token
18+
else
19+
s = ''
20+
token.chars.each_with_index do |c, i|
21+
s << (i % 2 == 1 ? c.upcase : c.downcase)
22+
end
23+
s.gsub!(/\.( |\z)/, '!!?! ')
24+
s.sub!(/\A(\w+)\z/, '\1!')
25+
s << " LOL!" if s.length > 2
26+
s
27+
end
28+
end
29+
result.join('')
30+
end
31+
32+
def self.extended(klass)
33+
klass.class_eval do
34+
class << self
35+
alias_method_chain :translate, :lols
36+
alias_method :t, :translate
37+
end
38+
end
39+
end
40+
end
41+
end

public/javascripts/i18nObj.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ define([
33
'str/htmlEscape',
44
'str/pluralize',
55
'str/escapeRegex',
6+
'compiled/str/i18nLolcalize',
67
'vendor/date' /* Date.parse, Date.UTC */
7-
], function($, htmlEscape, pluralize, escapeRegex) {
8+
], function($, htmlEscape, pluralize, escapeRegex, i18nLolcalize) {
89

910
// Instantiate the object, export globally for tinymce/specs
1011
var I18n = window.I18n = {};
@@ -532,6 +533,11 @@ I18n.l = I18n.localize;
532533
I18n.p = I18n.pluralize;
533534

534535

536+
var normalizeDefault = function(str) { return str };
537+
if (ENV && ENV.lolcalize) {
538+
normalizeDefault = i18nLolcalize;
539+
}
540+
535541
I18n.scoped = function(scope, callback) {
536542
var i18n_scope = new I18n.scope(scope);
537543
if (callback) {
@@ -558,7 +564,7 @@ I18n.scope.prototype = {
558564
if (typeof(options.count) != 'undefined' && typeof(defaultValue) == "string" && defaultValue.match(/^[\w\-]+$/)) {
559565
defaultValue = pluralize.withCount(options.count, defaultValue);
560566
}
561-
options.defaultValue = defaultValue;
567+
options.defaultValue = normalizeDefault(defaultValue);
562568
return I18n.translate(this.resolveScope(scope), options);
563569
},
564570
localize: function(scope, value) {

0 commit comments

Comments
 (0)