diff --git a/lib/jekyll/polyglot/patches/jekyll/site.rb b/lib/jekyll/polyglot/patches/jekyll/site.rb index 7a7579e9..3461933b 100644 --- a/lib/jekyll/polyglot/patches/jekyll/site.rb +++ b/lib/jekyll/polyglot/patches/jekyll/site.rb @@ -130,11 +130,9 @@ def derive_lang_from_path(doc) end segments = split_on_multiple_delimiters(doc.path) - # loop through all segments and check if they match the language regex segments.each do |segment| - if @languages.include?(segment) - return segment - end + match = @languages.find { |lang| lang.downcase == segment.downcase } + return match if match end nil @@ -149,7 +147,13 @@ def coordinate_documents(docs) approved = {} docs.each do |doc| lang = doc.data['lang'] || derive_lang_from_path(doc) || @default_lang + # If the doc lang matches a config language case-insensitively, use the config case + config_lang = @languages.find { |l| l.downcase == lang.downcase } + lang = config_lang if config_lang + doc.data['lang'] = lang if doc.data['lang'] && config_lang + lang_exclusive = doc.data['lang-exclusive'] || [] + url = doc.url.gsub(regex, '/') page_id = doc.data['page_id'] || url doc.data['permalink'] = url if doc.data['permalink'].to_s.empty? && !doc.data['lang'].to_s.empty? diff --git a/spec/jekyll/polyglot/patches/jekyll/site_spec.rb b/spec/jekyll/polyglot/patches/jekyll/site_spec.rb index bb7dd16a..b777aa81 100644 --- a/spec/jekyll/polyglot/patches/jekyll/site_spec.rb +++ b/spec/jekyll/polyglot/patches/jekyll/site_spec.rb @@ -232,11 +232,12 @@ ], 'pt-br' => [ Jekyll::Document.new('about.pt-br.md', site: @site, collection: collection_pt_br), - Jekyll::Document.new('international/restaurant/pt-br/sobre.md', site: @site, collection: collection_pt_br) + Jekyll::Document.new('international/restaurant/pt-br/sobre.md', site: @site, collection: collection_pt_br), + # With case-insensitive matching, pt-BR in path should match pt-br in config + Jekyll::Document.new('missing/pt-BR/sobre.md', site: @site, collection: collection_pt_br) ], nil => [ Jekyll::Document.new('apropos.fr.md', site: @site, collection: collection_wrong), # not included in languages - Jekyll::Document.new('missing/pt-BR/sobre.md', site: @site, collection: collection_wrong), # wrong capitalization, Jekyll::Document.new('taken/blues/newspaper.md', site: @site, collection: collection_wrong), # no matches Jekyll::Document.new('es-en-pt-br/wordswordswords.html', site: @site, collection: collection_wrong) # wont split ] @@ -245,7 +246,11 @@ docs.each do |document| expect(@site.lang_from_path).to eq(true) derived = @site.derive_lang_from_path document - expect(derived).to match lang + if lang.nil? + expect(derived).to be_nil + else + expect(derived).to eq(lang) + end end end end @@ -345,6 +350,48 @@ end end + describe 'case-insensitive language matching' do + before do + @site_with_ptbr = Site.new( + Jekyll.configuration( + 'languages' => ['en', 'pt-BR', 'zh-CN'], + 'default_lang' => 'en', + 'source' => File.expand_path('fixtures', __dir__), + 'url' => 'https://test.github.io' + ) + ) + @site_with_ptbr.prepare + end + + it 'derive_lang_from_path matches language codes case-insensitively' do + @site_with_ptbr.config['lang_from_path'] = true + @site_with_ptbr.prepare + + collection = Jekyll::Collection.new(@site_with_ptbr, 'pages') + doc_lower = Jekyll::Document.new('pages/pt-br/about.md', site: @site_with_ptbr, collection: collection) + doc_upper = Jekyll::Document.new('pages/PT-BR/about.md', site: @site_with_ptbr, collection: collection) + doc_mixed = Jekyll::Document.new('pages/Pt-Br/about.md', site: @site_with_ptbr, collection: collection) + + expect(@site_with_ptbr.derive_lang_from_path(doc_lower)).to eq('pt-BR') + expect(@site_with_ptbr.derive_lang_from_path(doc_upper)).to eq('pt-BR') + expect(@site_with_ptbr.derive_lang_from_path(doc_mixed)).to eq('pt-BR') + end + + it 'coordinate_documents normalizes doc lang to config case' do + collection = Jekyll::Collection.new(@site_with_ptbr, 'pages') + doc = Jekyll::Document.new('test.md', site: @site_with_ptbr, collection: collection) + doc.data['lang'] = 'pt-br' + doc.data['permalink'] = '/test/' + + @site_with_ptbr.file_langs = {} + @site_with_ptbr.active_lang = 'pt-BR' + coordinated = @site_with_ptbr.coordinate_documents([doc]) + + expect(coordinated).not_to be_empty + expect(coordinated.first.data['lang']).to eq('pt-BR') + end + end + describe @site do it 'should spawn no more than Etc.nprocessors processes' do forks = 0 @@ -942,6 +989,28 @@ expect(output).to include(%{}) end + it 'handles case-mismatched lang in frontmatter during coordinate_documents' do + @site.config['baseurl'] = '' + @site.config['url'] = 'https://test.github.io' + @site.config['languages'] = ['en', 'pt-BR'] + @site.config['default_lang'] = 'en' + @site.prepare + + collection = Jekyll::Collection.new(@site, 'pages') + doc = Jekyll::Document.new('test.pt-br.md', site: @site, collection: collection) + doc.data['lang'] = 'pt-br' + doc.data['page_id'] = 'test-page' + doc.data['permalink'] = '/test/' + + @site.file_langs = {} + @site.active_lang = 'pt-BR' + coordinated = @site.coordinate_documents([doc]) + + expect(coordinated).not_to be_empty + # Doc lang should be normalized to config case + expect(coordinated.first.data['lang']).to eq('pt-BR') + end + describe 'rendered_lang' do before do @collection = Jekyll::Collection.new(@site, 'test')