bridgetownrb / bridgetown

A next-generation progressive site generator & fullstack framework, powered by Ruby
https://www.bridgetownrb.com
MIT License
1.13k stars 114 forks source link

"resource.data.locale" returns Symbol or String #661

Closed svoop closed 1 year ago

svoop commented 1 year ago

Some resources return the locale as Symbol, others as String:

irb(#<Bridgetown::ERBView:0x000000010b0f1c50>):012:0> collections.pages.resources
=>
[#<Bridgetown::Resource::Base repo://pages.collection/_pages/404.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/500.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/contact/index.de.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/contact/index.en.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/index.de.erb>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/index.en.erb>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/products/index.de.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/products/index.en.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/sap/index.de.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/sap/index.en.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/web-and-infrastructure/index.de.md>,
 #<Bridgetown::Resource::Base repo://pages.collection/_pages/web-and-infrastructure/index.en.md>]

irb(#<Bridgetown::ERBView:0x000000010b0f1c50>):014:0> collections.pages.resources.map { _1.data.locale }
=> [:en, :en, "de", "en", "de", "en", "de", "en", "de", "en", "de", "en"]

No biggie, but it's a trap when you're not aware and compare resource.data.locale e.g. against I18n.locale (Symbol).

The two in _pages return Symbol, the remaining in subdirs return String. Maybe a coincidence, but sure looks like a pattern.

Bridgetown Version: v1.1.0

To Reproduce I can bootstrap an example site if need be.

Current behavior Returns Symbol or String

Expected behavior Returns Symbol

Computing environment (please complete the following information):

neilvanbeinum commented 1 year ago

Nice writeup! 👍

I've reproduced this on a site using v1.2 beta4 and pushed it here https://github.com/vvveebs/BT-Locale-Testing

After poking a bit, I think the pattern here is - does the resource have a locale in the filename? If so, the locale is returned as a string.

Still in the guessing stages, but I think the behaviour could originate in the Permalink Processor:

https://github.com/bridgetownrb/bridgetown/blob/6377bc71643e747c070a53afe5a7250495f3b43f/bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb#LL145C86-L145C86