Skip to content

Commit

Permalink
Fix hreflan_links when links is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 24, 2022
1 parent bd7d686 commit 7fd41ba
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Cldr Routes v0.6.1

This is the changelog for Cldr Routes version 0.6.1 released on July 24th, 2022. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_routes/tags)

### Bug Fixes

* Fix `LocalizedHelpers.hreflang_links/1` to return an empty string if links is `nil`.

## Cldr Routes v0.6.0

This is the changelog for Cldr Routes version 0.6.0 released on July 24th, 2022. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_routes/tags)
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The following steps should be followed to set up the configuration for localized
The first step is to ensure there is a configured `Gettext` backend module:

```elixir
defmodule MyApp.Gettext do
defmodule MyAppWeb.Gettext do
use Gettext, otp_app: :my_app
end
```
Expand Down Expand Up @@ -68,7 +68,7 @@ end

The following routes are generated (assuming that translations are updated in the `Gettext` configuration). For this example, the `:fr` translations are the same as the `:en` text with `_fr` appended.
```bash
% mix phx.routes MyApp.Router
% mix phx.routes MyAppWeb.Router
page_de_path GET /pages_de/:page PageController :show
page_en_path GET /pages/:page PageController :show
page_fr_path GET /pages_fr/:page PageController :show
Expand Down Expand Up @@ -98,17 +98,17 @@ end

## Localized Helpers

Manually constructing the localized helper names shown in the example above would be tedious. Therefore a `LocalizedHelpers` module is geenrated at compile-time. Assuming the router module is called `MyApp.Router` then the full name of the localized helper module is `MyApp.Router.LocalizedHelpers`.
Manually constructing the localized helper names shown in the example above would be tedious. Therefore a `LocalizedHelpers` module is geenrated at compile-time. Assuming the router module is called `MyAppWeb.Router` then the full name of the localized helper module is `MyAppWeb.Router.LocalizedHelpers`.

The functions on this module are the non-localized versions that should be used by applications (they delegate ultimately to the localized routes based upon the current locale).

The functions on the `LocalizedHelpers` module all respect the current locale, based upon `Cldr.get_locale/1`, and will delegate to the appropriate localized function in the `Helpers` function created automatically at compile time. For example:
```elixir
iex> MyApp.Cldr.put_locale("en")
iex> MyApp.Router.LocalizedHelpers.page_path %Plug.Conn{}, :show, 1
iex> MyAppWeb.Router.LocalizedHelpers.page_path %Plug.Conn{}, :show, 1
"/pages/1"
iex> MyApp.Cldr.put_locale("fr")
iex> MyApp.Router.LocalizedHelpers.page_path %Plug.Conn{}, :show, 1
iex> MyAppWeb.Router.LocalizedHelpers.page_path %Plug.Conn{}, :show, 1
"/pages_fr/1"
```

Expand All @@ -135,7 +135,7 @@ This information is also used by functions in the [ex_cldr_plugs](https://hex.pm

* Identify the users locale from the route in `Cldr.Plug.PutLocale`
* Store the identified locale in the session in `Cldr.Plug.PutSession`
* Propogate the locale from the session into a LiveView process during the `on_mount/3` callback with `Cldr.Session.put_locale/2`
* Propogate the locale from the session into a LiveView process during the `on_mount/3` callback with `Cldr.Plug.put_locale_from_session/2`

### Configuring Localized Helpers as default

Expand All @@ -151,9 +151,7 @@ defp view_helpers do
end
end
```
will result in the automatic use of the localized helpers rather than the standard helpers.

The `contoller/0` function should similarly be updated:
will result in the automatic use of the localized helpers rather than the standard helpers. The `contoller/0` function should similarly be updated:

```elixir
def controller do
Expand All @@ -169,25 +167,26 @@ end

## Generating link headers

When the same content is produced in multiple languages it is important to cross-link the different editions of the content to each other. This is good practise in general but strong advised by [goggle](https://developers.google.com/search/docs/advanced/crawling/localized-versions) to reduce SEO risks for your site.
When the same content is produced in multiple languages it is important to cross-link the different editions of the content to each other. This is good practise in general but strong advised by [goggle](https://developers.google.com/search/docs/advanced/crawling/localized-versions) to reduce SEO risks for your site.

This cross-linking can be done with the aid of HTTP headers or with `<link />` tags in the `<head>` section of an HTML document. Helpers are generated by `ex_cldr_routes` to facilitate the creating of these links. These functions are generated in the `MyAppWeb.Router.LocalizedHelpers` module (where `MyAppWeb.Router` is the name of your Phoenix router module).

This cross-linking can be done with the aid of HTTP headers or with `<link />` tags in the `<head>` section of an HTML document. Helpers are generated by `ex_cldr_routes` to facilitate the creating of these links. These functions are generated in the `MyApp.Router.LocalizedHelpers` module (where `MyApp.Router` is the name of your Phoenix router module).

* `MyApp.Router.LocalizedHelpers.<helper>_links` generated a mapping of locales for a given route to the URLs for those locales.
* `MyApp.Router.LocalizedHelpers.hreflang_links/1` take the generated map and produces link headers ready for insertion into an HTML document.
* `MyAppWeb.Router.LocalizedHelpers.<helper>_links` generated a mapping of locales for a given route to the URLs for those locales.
* `MyAppWeb.Router.LocalizedHelpers.hreflang_links/1` take the generated map and produces link headers ready for insertion into an HTML document.

The functions can be used as follows:

* Update the controller to generate the links and add them to `:assigns`
1. Update the controller to generate the links and add them to `:assigns`

```elixir
defmodule MyAppWeb.UserController do
use MyAppWeb, :controller

# This alias would normally be set in the
# This alias would normally be set in the
# MyAppWeb.controller/0 and MyAppWeb.view_helpers/0
# functions
alias MyAppWeb.Router.LocalizedHelpers, as: Routes

alias MyApp.Accounts
alias MyApp.Accounts.User

Expand All @@ -199,7 +198,8 @@ defmodule MyAppWeb.UserController do
end
```

* Update the root layout to add the hreflang links
2. Update the root layout to add the hreflang links

```html
<html lang="en">
<head>
Expand Down
30 changes: 25 additions & 5 deletions lib/cldr/routes/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,34 @@ defmodule Cldr.Route.LocalizedHelpers do
See https://developers.google.com/search/docs/advanced/crawling/localized-versions#http
### Example
===> MyApp.Helpers.LocalizedHelpers.user_links(conn, :show, 1)
...> |> Cldr.Route.LocalizedHelpers.hreflang_links()
### Examples
iex> links = %{
...> "en" => "https://localhost/users/1",
...> "fr" => "https://localhost/utilisateurs/1"
...> }
iex> Cldr.Route.LocalizedHelpers.hreflang_links(links)
{:safe, [
["<link href=", "\\"https://localhost/users/1\\"",
"; rel=alternate; hreflang=", "\\"en\\"", " />"],
"\\n",
["<link href=", "\\"https://localhost/utilisateurs/1\\"",
"; rel=alternate; hreflang=", "\\"fr\\"", " />"]
]}
iex> Cldr.Route.LocalizedHelpers.hreflang_links(nil)
{:safe, []}
iex> Cldr.Route.LocalizedHelpers.hreflang_links(%{})
{:safe, []}
"""
@spec hreflang_links(%{locale_name() => url()}) :: Phoenix.HTML.safe()
def hreflang_links(url_map) do
def hreflang_links(nil) do
{:safe, []}
end

def hreflang_links(url_map) when is_map(url_map) do
links =
for {locale, url} <- url_map do
["<link href=", inspect(url), "; rel=alternate; hreflang=", inspect(locale), " />"]
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CldrRoutes.MixProject do
use Mix.Project

@version "0.6.0"
@version "0.6.1"

def project do
[
Expand Down
1 change: 1 addition & 0 deletions test/cldr_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Cldr.Route.Test do
alias Cldr.Route.Test.MyTestApp.Router

doctest Cldr.Route
doctest Cldr.Route.LocalizedHelpers
doctest MyApp.Router.LocalizedHelpers

describe "Routes" do
Expand Down

0 comments on commit 7fd41ba

Please sign in to comment.