Browse Source

Allow to access to a language directly though instance.tld/:lang

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
tags/2.0.0-beta.1
Thomas Citharel 1 year ago
parent
commit
5de0cee025
No known key found for this signature in database GPG Key ID: A061B9DDE0CA0773
5 changed files with 46 additions and 7 deletions
  1. +15
    -0
      js/src/components/Utils/HomepageRedirectComponent.vue
  2. +16
    -5
      js/src/router/index.ts
  3. +2
    -0
      js/src/utils/i18n.ts
  4. +11
    -0
      lib/web/plugs/set_locale_plug.ex
  5. +2
    -2
      lib/web/views/utils.ex

+ 15
- 0
js/src/components/Utils/HomepageRedirectComponent.vue View File

@@ -0,0 +1,15 @@
<template>
<div>a</div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import RouteName from "@/router/name";

@Component
export default class HomepageRedirectComponent extends Vue {
created(): void {
this.$router.replace({ name: RouteName.HOME });
}
}
</script>

+ 16
- 5
js/src/router/index.ts View File

@@ -13,7 +13,7 @@ import { groupsRoutes } from "./groups";
import { discussionRoutes } from "./discussion";
import { userRoutes } from "./user";
import RouteName from "./name";
import { i18n } from "@/utils/i18n";
import { AVAILABLE_LANGUAGES, i18n } from "@/utils/i18n";

Vue.use(Router);

@@ -183,12 +183,23 @@ export const routes = [
announcer: { message: (): string => i18n.t("Page not found") as string },
},
},
{
path: "*",
redirect: { name: RouteName.PAGE_NOT_FOUND },
},
];

for (const locale of AVAILABLE_LANGUAGES) {
routes.push({
path: `/${locale}`,
component: (): Promise<ImportedComponent> =>
import(
/* webpackChunkName: "HomepageRedirectComponent" */ "../components/Utils/HomepageRedirectComponent.vue"
),
});
}

routes.push({
path: "*",
redirect: { name: RouteName.PAGE_NOT_FOUND },
});

const router = new Router({
scrollBehavior,
mode: "history",


+ 2
- 0
js/src/utils/i18n.ts View File

@@ -10,6 +10,8 @@ const DEFAULT_LOCALE = "en_US";

const localeInLocalStorage = getLocaleData();

export const AVAILABLE_LANGUAGES = Object.keys(langs);

console.debug("localeInLocalStorage", localeInLocalStorage);

let language =


+ 11
- 0
lib/web/plugs/set_locale_plug.ex View File

@@ -18,11 +18,13 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
def call(conn, _) do
locale =
[
eventual_path_locale(conn.path_info),
conn.assigns[:user_locale],
conn.assigns[:detected_locale],
default_locale(),
"en"
]
|> Enum.filter(& &1)
|> Enum.map(&determine_best_locale/1)
|> Enum.filter(&supported_locale?/1)
|> hd()
@@ -31,6 +33,15 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
assign(conn, :locale, locale)
end

defp eventual_path_locale(path_info) do
with [locale] <- path_info,
true <- supported_locale?(locale) do
locale
else
_ -> nil
end
end

@spec supported_locale?(String.t()) :: boolean()
defp supported_locale?(locale) do
GettextBackend


+ 2
- 2
lib/web/views/utils.ex View File

@@ -50,8 +50,8 @@ defmodule Mobilizon.Web.Views.Utils do
index_content
|> replace_meta(tags)
|> String.replace(
"<html lang=\"en\" dir=\"auto\">",
"<html lang=\"#{locale}\" dir=\"#{get_language_direction(locale)}\">"
~s(<html lang="en" dir="auto">),
~s(<html lang="#{locale}" dir="#{get_language_direction(locale)}">)
)
end



Loading…
Cancel
Save