feat: enable semantic address format to be localised

For example, US sites can use the US address format and Chinese sites can use the Chinese address format.

A default address_format can be set in params.toml, which can be overriden on a per page basis using a parameter of the same name in a page's front matter. Optionally, multilingual sites may wish to add a default format for each site in languages.toml.

BREAKING CHANGES
- list_delimiter removed from params.toml
- address_format added to params.toml
- [address_formats] added to params.toml

Close #1344
This commit is contained in:
George Cushen committed 2019-11-16 17:55:17 +00:00
1 parent d0fb759153
commit e6eb27deee
4 files changed
+33 -20

No files matched your search

+8 -3
View File
@@ -136,9 +136,8 @@ date_format = "Jan 2, 2006"
# Examples: "3:04 pm" or "15:04"
time_format = "3:04 PM"
# List delimiter (separates parts of an address)
# Popular separators are ", " (comma), "<br>" (new line), " " (space)
list_delimiter = ", "
# Address format (choose from the [address_formats] list below or add you own to the list).
address_format = "en-us"
############################
## Advanced
@@ -167,6 +166,12 @@ link_authors = true
# E.g. To load `/assets/js/custom.js`, set `plugins_js = ["custom"]`.
plugins_js = []
# Available address formats.
[address_formats]
en-us = {order = ['street', 'city', 'region', 'postcode'], delimiters = [', ', ', ', ' ', '']}
en-gb = {order = ['street', 'city', 'region', 'postcode'], delimiters = [', ', ', ', ', ', '']}
zh = {order = ['postcode', 'region', 'city', 'street'], delimiters = [' ', ' ', ' ', '']}
# Configuration of publication pages.
[publications]
# Date format (refer to https://sourcethemes.com/academic/docs/customization/#date-format )
@@ -0,0 +1,21 @@
{{/* Function to return a formatted address given a semantic address. */}}
{{/* Check for valid site config. */}}
{{ if not site.Params.address_format }}{{errorf "Address format missing from `params.toml`!"}}{{end}}
{{ if not site.Params.address_formats }}{{errorf "Address formats missing from `params.toml`!"}}{{end}}
{{ $page := . }}
{{ $address := .address }}
{{ $format_name := $page.Params.address_format | default site.Params.address_format | default "en-us" }}
{{ $format := index site.Params.address_formats $format_name }}
{{ $address_display := slice }}
{{ range $k, $v := $format.order }}
{{ if eq $v "street" | and $address.street }}{{$address_display = $address_display | append $address.street | append (index $format.delimiters $k | default "") }}{{end}}
{{ if eq $v "city" | and $address.city }}{{$address_display = $address_display | append $address.city | append (index $format.delimiters $k | default "") }}{{end}}
{{ if eq $v "region" | and $address.region }}{{$address_display = $address_display | append $address.region | append (index $format.delimiters $k | default "") }}{{end}}
{{ if eq $v "postcode" | and $address.postcode }}{{$address_display = $address_display | append $address.postcode | append (index $format.delimiters $k | default "") }}{{end}}
{{ if eq $v "country" | and $address.country }}{{$address_display = $address_display | append $address.country | append (index $format.delimiters $k | default "") }}{{end}}
{{end}}
{{ return (delimit $address_display "") }}
+2 -8
View File
@@ -65,16 +65,10 @@
{{ $addr_formatted := "" }}{{/* Scoping for maps. */}}
{{ if $data.address.street | or $data.address.city | or $data.address.region | or $data.address.postcode | or $data.address.country }}
{{ $addr := slice }}
{{with $data.address.street}}{{$addr = $addr | append .}}{{end}}
{{with $data.address.city}}{{$addr = $addr | append .}}{{end}}
{{with $data.address.region}}{{$addr = $addr | append .}}{{end}}
{{with $data.address.postcode}}{{$addr = $addr | append .}}{{end}}
{{with $data.address.country}}{{$addr = $addr | append .}}{{end}}
{{ $addr_formatted = delimit $addr (site.Params.list_delimiter | default ", ") }}
{{ $addr_formatted = partial "functions/get_address" (dict "root" . "address" $data.address) }}
<li>
<i class="fa-li fas fa-map-marker fa-2x" aria-hidden="true"></i>
<span id="person-address">{{ $addr_formatted }}</span>
<span id="person-address">{{$addr_formatted}}</span>
</li>
{{ end }}
+2 -9
View File
@@ -38,7 +38,7 @@
{{ .Params.event | markdownify }}
{{ if .Params.event_url }}</a>{{ end }}
</div>
</div>
</div>
</div>
<div class="col-md-1"></div>
</div>
@@ -53,15 +53,8 @@
<div class="col-12 col-md-3 pub-row-heading">{{ i18n "location" }}</div>
<div class="col-12 col-md-9">{{ .Params.location | markdownify }}</div>
{{ if .Params.address }}
{{ $addr := slice }}
{{with .Params.address.street}}{{$addr = $addr | append .}}{{end}}
{{with .Params.address.city}}{{$addr = $addr | append .}}{{end}}
{{with .Params.address.region}}{{$addr = $addr | append .}}{{end}}
{{with .Params.address.postcode}}{{$addr = $addr | append .}}{{end}}
{{with .Params.address.country}}{{$addr = $addr | append .}}{{end}}
{{ $addr_formatted := delimit $addr (site.Params.list_delimiter | default ", ") }}
<div class="col-md-3"></div>
<div class="col-12 col-md-9">{{$addr_formatted}}</div>
<div class="col-12 col-md-9">{{partial "functions/get_address" (dict "root" . "address" .Params.address) }}}}</div>
{{end}}
</div>
</div>