2019-04-29 20:27:42 -04:00
# frozen_string_literal: true
2017-02-24 05:35:33 -05:00
require " rails_helper "
require " i18n/duplicate_key_finder "
def extract_locale ( path )
path [ / \ .([^.]{2,}) \ .yml$ / , 1 ]
end
PLURALIZATION_KEYS || = [ 'zero' , 'one' , 'two' , 'few' , 'many' , 'other' ]
2019-05-20 17:26:22 -04:00
ENGLISH_KEYS || = [ 'one' , 'other' ]
2017-02-24 05:35:33 -05:00
def find_pluralizations ( hash , parent_key = '' , pluralizations = Hash . new )
hash . each do | key , value |
if Hash === value
current_key = parent_key . blank? ? key : " #{ parent_key } . #{ key } "
find_pluralizations ( value , current_key , pluralizations )
elsif PLURALIZATION_KEYS . include? key
pluralizations [ parent_key ] = hash
end
end
pluralizations
end
def is_yaml_compatible? ( english , translated )
english . each do | k , v |
if translated . has_key? ( k )
if Hash === v
if Hash === translated [ k ]
return false unless is_yaml_compatible? ( v , translated [ k ] )
end
else
return false unless v . class == translated [ k ] . class
end
end
end
true
end
2018-11-07 11:59:42 -05:00
def each_translation ( hash , parent_key = '' , & block )
hash . each do | key , value |
current_key = parent_key . blank? ? key : " #{ parent_key } . #{ key } "
if Hash === value
each_translation ( value , current_key , & block )
else
yield ( current_key , value . to_s )
end
end
end
2013-02-05 14:16:51 -05:00
describe " i18n integrity checks " do
2017-02-24 05:35:33 -05:00
it 'has an i18n key for each Trust Levels' do
2013-02-05 14:16:51 -05:00
TrustLevel . all . each do | ts |
2015-04-25 11:18:35 -04:00
expect ( ts . name ) . not_to match ( / translation missing / )
2013-02-05 14:16:51 -05:00
end
end
2017-02-24 05:35:33 -05:00
it " has an i18n key for each Site Setting " do
2013-02-05 14:16:51 -05:00
SiteSetting . all_settings . each do | s |
2017-02-24 05:35:33 -05:00
next if s [ :setting ] [ / ^test_ / ]
2015-04-25 11:18:35 -04:00
expect ( s [ :description ] ) . not_to match ( / translation missing / )
2013-02-05 14:16:51 -05:00
end
end
2017-02-24 05:35:33 -05:00
it " has an i18n key for each Badge description " do
2016-04-12 12:55:49 -04:00
Badge . where ( system : true ) . each do | b |
expect ( b . long_description ) . to be_present
expect ( b . description ) . to be_present
end
end
2017-06-18 22:13:23 -04:00
Dir [ " #{ Rails . root } /config/locales/{client,server}.*.yml " ] . each do | path |
it " does not contain invalid interpolation keys for ' #{ path } ' " do
matches = File . read ( path ) . scan ( / % \ {([^a-zA-Z \ s]+) \ }| \ { \ {([^a-zA-Z \ s]+) \ } \ } / )
matches . flatten!
matches . compact!
matches . uniq!
expect ( matches ) . to eq ( [ ] )
end
end
2017-02-24 05:35:33 -05:00
Dir [ " #{ Rails . root } /config/locales/client.*.yml " ] . each do | path |
it " has valid client YAML for ' #{ path } ' " do
yaml = YAML . load_file ( path )
locale = extract_locale ( path )
2013-03-23 03:57:31 -04:00
2017-02-24 05:35:33 -05:00
expect ( yaml . keys ) . to eq ( [ locale ] )
2013-03-23 03:57:31 -04:00
2017-02-24 05:35:33 -05:00
expect ( yaml [ locale ] [ " js " ] ) . to be
2019-05-15 17:43:00 -04:00
if ! LocaleSiteSetting . fallback_locale ( locale )
expect ( yaml [ locale ] [ " admin_js " ] ) . to be
expect ( yaml [ locale ] [ " wizard_js " ] ) . to be
end
2013-03-20 05:51:28 -04:00
end
end
2013-02-05 14:16:51 -05:00
2017-02-24 05:35:33 -05:00
Dir [ " #{ Rails . root } /**/locale*/*.en.yml " ] . each do | english_path |
english_yaml = YAML . load_file ( english_path ) [ " en " ]
2015-09-18 15:12:37 -04:00
2017-02-24 05:35:33 -05:00
context ( english_path ) do
it " has no duplicate keys " do
english_duplicates = DuplicateKeyFinder . new . find_duplicates ( english_path )
expect ( english_duplicates ) . to be_empty
2015-09-18 15:12:37 -04:00
end
2019-05-20 17:26:22 -04:00
context " pluralizations " do
wrong_keys = [ ]
invald_one_keys = [ ]
2015-09-18 15:12:37 -04:00
2019-05-20 17:26:22 -04:00
find_pluralizations ( english_yaml ) . each do | key , hash |
next if key [ " messages.restrict_dependent_destroy " ]
wrong_keys << key if hash . keys . sort != ENGLISH_KEYS
if one_value = hash [ 'one' ]
invald_one_keys << key if one_value . include? ( '1' ) && ! one_value . match? ( / %{count}|{{count}} / )
end
end
it " has valid pluralizations keys " do
keys = wrong_keys . join ( " \n " )
expect ( wrong_keys ) . to be_empty , << ~ MSG
Pluralized strings must have only the sub - keys 'one' and 'other' .
The following keys have missing or additional keys : \ n \ n #{keys}
MSG
end
it " should use %{count} instead of 1 in 'one' keys " do
keys = invald_one_keys . join ( " .one \n " )
expect ( invald_one_keys ) . to be_empty , << ~ MSG
The following keys contain the number 1 instead of the interpolation key %{ count } : \ n \ n #{keys}
MSG
2017-02-24 05:35:33 -05:00
end
2015-09-18 15:12:37 -04:00
end
2018-11-07 11:59:42 -05:00
context " valid translations " do
invalid_relative_links = { }
invalid_relative_image_sources = { }
each_translation ( english_yaml ) do | key , value |
if value . match? ( / href \ s*= \ s*["'] \/ [^ \/ ]| \ ] \ ( \/ [^ \/ ] /i )
invalid_relative_links [ key ] = value
elsif value . match? ( / src \ s*= \ s*["'] \/ [^ \/ ] /i )
invalid_relative_image_sources [ key ] = value
end
end
it " uses %{base_url} or %{base_path} for relative links " do
keys = invalid_relative_links . keys . join ( " \n " )
expect ( invalid_relative_links ) . to be_empty , " The following keys have relative links, but do not start with %{base_url} or %{base_path}: \n \n #{ keys } "
end
it " uses %{base_url} or %{base_path} for relative image src " do
keys = invalid_relative_image_sources . keys . join ( " \n " )
expect ( invalid_relative_image_sources ) . to be_empty , " The following keys have relative image sources, but do not start with %{base_url} or %{base_path}: \n \n #{ keys } "
end
end
2015-09-18 15:12:37 -04:00
end
2017-02-24 05:35:33 -05:00
Dir [ english_path . sub ( " .en.yml " , " .*.yml " ) ] . each do | path |
next if path [ " .en.yml " ]
2015-09-24 18:43:23 -04:00
2017-02-24 05:35:33 -05:00
context ( path ) do
locale = extract_locale ( path )
yaml = YAML . load_file ( path )
2015-09-24 18:43:23 -04:00
2017-02-24 05:35:33 -05:00
it " has no duplicate keys " do
duplicates = DuplicateKeyFinder . new . find_duplicates ( path )
expect ( duplicates ) . to be_empty
2015-09-24 18:43:23 -04:00
end
2017-02-24 05:35:33 -05:00
it " does not overwrite another locale " do
expect ( yaml . keys ) . to eq ( [ locale ] )
2015-09-18 15:12:37 -04:00
end
2015-09-24 18:43:23 -04:00
2017-02-24 05:35:33 -05:00
unless path [ " transliterate " ]
it " is compatible with english " do
expect ( is_yaml_compatible? ( english_yaml , yaml ) ) . to eq ( true )
2015-09-24 18:43:23 -04:00
end
2017-02-24 05:35:33 -05:00
2015-09-24 18:43:23 -04:00
end
2017-02-24 05:35:33 -05:00
2015-09-18 15:12:37 -04:00
end
2017-02-24 05:35:33 -05:00
2015-09-18 15:12:37 -04:00
end
end
2017-02-24 05:35:33 -05:00
2013-02-05 14:16:51 -05:00
end