Skip to content

Commit

Permalink
feat: add i18n for commissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Thechi2000 committed Feb 1, 2024
1 parent f30863c commit 1cfe0bc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion app/src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function Card(
img = props.commission.attributes.logo;
title = props.commission.attributes.name;
description = props.commission.attributes.small_description;
link = `/commission/${(props.commission as any).id}`;
link = `/commission/${props.commission.attributes.slug}`;
} else {
img = props.img;
title = props.title;
Expand Down
27 changes: 23 additions & 4 deletions app/src/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,55 @@ interface Translations {
by: string;
relatedContent: string;
committee: string;
commission: string;
}

const translations: { [key in Locale]: Translations } = {
type OptionsOperators = {
[key in keyof LangOptions]: (value: string) => string;
};

const translations: { [key in Locale]: Translations & OptionsOperators } = {
en: {
dateIndicator: "on",
timeIndicator: "at",
by: "by",
relatedContent: "related content",
committee: "committee",
commission: "commission",
capitalize: (s) => s.replace(/^(\s*\w)/, (s) => s.toUpperCase()),
plural: (s) => s + "s",
},
fr: {
dateIndicator: "le",
timeIndicator: "à",
by: "par",
relatedContent: "contenu lié",
committee: "comité",
commission: "commission",
capitalize: (s) => s.replace(/^(\s*\w)/, (s) => s.toUpperCase()),
plural: (s) => (s.endsWith("u") ? s + "x" : s + "s"),
},
};

interface LangOptions {
capitalize: boolean;
capitalize?: boolean;
plural?: boolean;
}

export function applyOptions(
str: string,
locale: Locale,
opts?: LangOptions
): string {
let result = str;
if (opts?.capitalize) {
result = result.replace(/(\s*\w)/, (s) => s.toUpperCase());
let l = translations[locale];

if (opts) {
Object.keys(opts).forEach((opt: string) => {
if ((opts as any)[opt] && opt in l) {
result = (l as any)[opt](result);
}
});
}

return result;
Expand Down
44 changes: 0 additions & 44 deletions app/src/pages/commission/[id].tsx

This file was deleted.

11 changes: 10 additions & 1 deletion app/src/pages/commissions.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import Card from "@/components/Card";
import { locale, translate } from "@/locales";
import strapi from "@/strapi";
import { ApiCommission } from "@/types/generated/contentTypes";
import { GetServerSideProps, InferGetServerSidePropsType } from "next";
import { useRouter } from "next/router";

export default function Commissions(
props: InferGetServerSidePropsType<typeof getServerSideProps>
) {
const router = useRouter();
return (
<div className="pageList">
<div>
<h1>Commissions</h1>
<h1>
{translate("commission", locale(router), {
capitalize: true,
plural: true,
})}
</h1>
</div>
{props.news.map((c) => (
<Card key={(c as any).id} commission={c} size="large" background />
Expand All @@ -24,6 +32,7 @@ export const getServerSideProps: GetServerSideProps<{
let res = await strapi.find<ApiCommission[]>("commissions", {
sort: "name",
populate: "logo",
locale: locale(context),
});
return { props: { news: res.data.reverse() } };
};
4 changes: 4 additions & 0 deletions app/src/pages/news/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,9 @@ export const getServerSideProps: GetServerSideProps<{ news: ApiNews }> = async (
},
});

if (news.data.length != 1) {
return { notFound: true };
}

return { props: { news: news.data[0] } };
};
7 changes: 7 additions & 0 deletions app/src/types/generated/contentTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,13 @@ export interface ApiCommission extends Schema.CollectionType {
localized: false;
};
}>;
slug: Attribute.String &
Attribute.Required &
Attribute.SetPluginOptions<{
i18n: {
localized: false;
};
}>;
createdAt: Attribute.DateTime;
updatedAt: Attribute.DateTime;
publishedAt: Attribute.DateTime;
Expand Down
12 changes: 11 additions & 1 deletion strapi/src/api/commission/content-types/commission/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"type": "string",
"required": true,
"unique": true
"unique": false
},
"news": {
"type": "relation",
Expand Down Expand Up @@ -99,6 +99,16 @@
}
},
"type": "text"
},
"slug": {
"pluginOptions": {
"i18n": {
"localized": false
}
},
"type": "string",
"required": true,
"regex": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
}
}
}
8 changes: 7 additions & 1 deletion strapi/types/generated/contentTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ export interface ApiCommissionCommission extends Schema.CollectionType {
attributes: {
name: Attribute.String &
Attribute.Required &
Attribute.Unique &
Attribute.SetPluginOptions<{
i18n: {
localized: true;
Expand Down Expand Up @@ -864,6 +863,13 @@ export interface ApiCommissionCommission extends Schema.CollectionType {
localized: false;
};
}>;
slug: Attribute.String &
Attribute.Required &
Attribute.SetPluginOptions<{
i18n: {
localized: false;
};
}>;
createdAt: Attribute.DateTime;
updatedAt: Attribute.DateTime;
publishedAt: Attribute.DateTime;
Expand Down

0 comments on commit 1cfe0bc

Please sign in to comment.