@prezly/theme-kit-nextjs 3.2.2 → 3.2.3-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/build/components-nextjs/PageSeo/PageSeo.js +8 -13
  2. package/build/components-nextjs/PageSeo/PageSeo.js.map +1 -1
  3. package/build/components-nextjs/document.js.map +1 -1
  4. package/build/intl/getFallbackLocale.d.ts +10 -0
  5. package/build/intl/getFallbackLocale.js +24 -0
  6. package/build/intl/getFallbackLocale.js.map +1 -0
  7. package/build/intl/index.d.ts +2 -1
  8. package/build/intl/index.js +2 -1
  9. package/build/intl/index.js.map +1 -1
  10. package/build/intl/locale.d.ts +3 -0
  11. package/build/intl/locale.js.map +1 -1
  12. package/build/intl/localeObject.d.ts +5 -0
  13. package/build/intl/localeObject.js +7 -0
  14. package/build/intl/localeObject.js.map +1 -1
  15. package/build/seo/getAlternateLanguageLinks.d.ts +4 -0
  16. package/build/seo/getAlternateLanguageLinks.js +84 -0
  17. package/build/seo/getAlternateLanguageLinks.js.map +1 -0
  18. package/build/seo/index.d.ts +1 -0
  19. package/build/seo/index.js +18 -0
  20. package/build/seo/index.js.map +1 -0
  21. package/build/seo/utils/bindLanguagesWithLocales.d.ts +11 -0
  22. package/build/seo/utils/bindLanguagesWithLocales.js +23 -0
  23. package/build/seo/utils/bindLanguagesWithLocales.js.map +1 -0
  24. package/build/seo/utils/createAlternateLanguageLink.d.ts +5 -0
  25. package/build/seo/utils/createAlternateLanguageLink.js +15 -0
  26. package/build/seo/utils/createAlternateLanguageLink.js.map +1 -0
  27. package/build/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +4 -4
  29. package/src/components-nextjs/PageSeo/PageSeo.tsx +15 -23
  30. package/src/components-nextjs/document.tsx +0 -1
  31. package/src/intl/getFallbackLocale.ts +29 -0
  32. package/src/intl/index.ts +2 -1
  33. package/src/intl/locale.ts +5 -1
  34. package/src/intl/localeObject.ts +8 -0
  35. package/src/seo/getAlternateLanguageLinks.test.ts +164 -0
  36. package/src/seo/getAlternateLanguageLinks.ts +115 -0
  37. package/src/seo/index.ts +1 -0
  38. package/src/seo/utils/bindLanguagesWithLocales.ts +31 -0
  39. package/src/seo/utils/createAlternateLanguageLink.ts +17 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prezly/theme-kit-nextjs",
3
- "version": "3.2.2",
3
+ "version": "3.2.3-1",
4
4
  "description": "Data layer and utility library for developing Prezly themes with NextJS",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -56,19 +56,19 @@
56
56
  },
57
57
  "devDependencies": {
58
58
  "@prezly/eslint-config": "4.3.2",
59
- "@sentry/nextjs": "7.34.0",
59
+ "@sentry/nextjs": "7.35.0",
60
60
  "@types/jest": "29.4.0",
61
61
  "@types/node": "17.0.14",
62
62
  "@types/parse-data-url": "3.0.0",
63
63
  "@types/react": "18.0.27",
64
64
  "@types/react-dom": "18.0.10",
65
- "eslint": "8.32.0",
65
+ "eslint": "8.33.0",
66
66
  "jest": "29.4.1",
67
67
  "np": "7.6.3",
68
68
  "prettier": "2.8.3",
69
69
  "rimraf": "3.0.2",
70
70
  "ts-jest": "29.0.5",
71
- "typescript": "4.9.4"
71
+ "typescript": "4.9.5"
72
72
  },
73
73
  "publishConfig": {
74
74
  "access": "public"
@@ -3,7 +3,7 @@ import type { NextSeoProps } from 'next-seo';
3
3
  import { useRouter } from 'next/router';
4
4
  import { useMemo } from 'react';
5
5
 
6
- import { getUsedLanguages, LocaleObject } from '../../intl';
6
+ import { getUsedLanguages } from '../../intl';
7
7
  import {
8
8
  useCompanyInformation,
9
9
  useCurrentLocale,
@@ -13,6 +13,7 @@ import {
13
13
  useLanguages,
14
14
  useNewsroom,
15
15
  } from '../../newsroom-context';
16
+ import { getAlternateLanguageLinks } from '../../seo';
16
17
  import { getNewsroomLogoUrl } from '../../utils';
17
18
 
18
19
  import { getAbsoluteUrl } from './lib/getAbsoluteUrl';
@@ -65,31 +66,22 @@ export function PageSeo({
65
66
  return [];
66
67
  }
67
68
 
68
- const alternateLanguages = getUsedLanguages(languages).filter(
69
- (language) => language.code !== currentLocale.toUnderscoreCode(),
70
- );
69
+ const usedLanguages = getUsedLanguages(languages);
71
70
 
72
- return alternateLanguages
73
- .map((language) => {
74
- const locale = LocaleObject.fromAnyCode(language.code);
71
+ return getAlternateLanguageLinks(usedLanguages, (locale) => {
72
+ const translationUrl = getTranslationUrl(locale);
75
73
 
76
- const translationLink = getTranslationUrl(locale, true);
74
+ if (!translationUrl) {
75
+ return undefined;
76
+ }
77
77
 
78
- if (!translationLink) {
79
- return undefined;
80
- }
81
-
82
- return {
83
- hrefLang: locale.toHyphenCode(),
84
- href: getAbsoluteUrl(
85
- translationLink,
86
- site.url,
87
- currentStory && translationLink !== '/' ? false : getLinkLocaleSlug(locale),
88
- ),
89
- };
90
- })
91
- .filter<AlternateLanguageLink>(Boolean as any);
92
- }, [currentLocale, getLinkLocaleSlug, getTranslationUrl, languages, site.url, currentStory]);
78
+ return getAbsoluteUrl(
79
+ translationUrl,
80
+ site.url,
81
+ currentStory && translationUrl !== '/' ? false : getLinkLocaleSlug(locale),
82
+ );
83
+ });
84
+ }, [currentStory, getLinkLocaleSlug, getTranslationUrl, languages, site.url]);
93
85
 
94
86
  return (
95
87
  <NextSeo
@@ -26,7 +26,6 @@ export class PrezlyThemeDocument extends Document {
26
26
  <Html lang={locale.toHyphenCode()} dir={direction}>
27
27
  <Head>
28
28
  <meta name="og:locale" content={locale.toHyphenCode()} />
29
- {/* TODO: Add alternate locales */}
30
29
  </Head>
31
30
  <body>
32
31
  <Main />
@@ -0,0 +1,29 @@
1
+ import type { LangCode } from './locale';
2
+ import { LocaleObject } from './localeObject';
3
+
4
+ const localeFallback: Record<LangCode, LocaleObject[]> = {
5
+ en: [LocaleObject.fromAnyCode('en-US'), LocaleObject.fromAnyCode('en-GB')],
6
+ fr: [LocaleObject.fromAnyCode('fr-FR'), LocaleObject.fromAnyCode('fr-CA')],
7
+ };
8
+
9
+ /**
10
+ * Fallback locales for each region independent locale.
11
+ * For example, a story has en-CA en-AU and en-GB locales. As you can see there is no region independent locale.
12
+ * We will use a list of possible fallbacks to determine region independent locale alias.
13
+ * In the provided example the result will be en-GB since it is defined in the localeFallback list
14
+ * If the list will be en-CA en-AU and en-US then the result will be en-US since it is the first one and hence takes precedence.
15
+ */
16
+ export function getFallbackLocale(
17
+ langCode: LangCode,
18
+ availableLocales: LocaleObject[],
19
+ ): LocaleObject | undefined {
20
+ const fallbacks = localeFallback[langCode];
21
+
22
+ if (!fallbacks) {
23
+ return undefined;
24
+ }
25
+
26
+ return fallbacks.find((fallback) =>
27
+ Boolean(availableLocales.find((available) => available.isEqual(fallback))),
28
+ );
29
+ }
package/src/intl/index.ts CHANGED
@@ -1,4 +1,5 @@
1
+ export * from './getFallbackLocale';
1
2
  export * from './languages';
2
- export * from './locale';
3
3
  export { default as localeConfig } from './localeConfig';
4
+ export * from './locale';
4
5
  export * from './localeObject';
@@ -8,11 +8,15 @@ export const DEFAULT_LOCALE = 'en';
8
8
  // We use pseudo locale used for localization testing, to reliably determine if we need to fallback to the default newsroom language
9
9
  export const DUMMY_DEFAULT_LOCALE = 'qps-ploc';
10
10
 
11
+ export type LangCode = string;
12
+ export type RegionCode = string;
13
+ export type LocaleCode = string;
14
+
11
15
  /**
12
16
  * This a list of locales that Prezly has translations for. Each code represents a language file in the `@prezly/themes-intl-messages` package.
13
17
  * See https://github.com/prezly/themes-intl-messages for more info.
14
18
  */
15
- const SUPPORTED_LOCALES = [
19
+ const SUPPORTED_LOCALES: LocaleCode[] = [
16
20
  'af',
17
21
  'ar',
18
22
  'az',
@@ -12,6 +12,12 @@ export class LocaleObject {
12
12
  */
13
13
  private localeCode: string;
14
14
 
15
+ /**
16
+ * Whether the locale is region independent (without region/culture part).
17
+ * I.E. `en` is region independent, while `en-US` is not.
18
+ */
19
+ public isRegionIndependent: boolean;
20
+
15
21
  private constructor(localeCode: string) {
16
22
  if (!/^([a-z]{2,3})(-[a-z\d]{1,4})?$/i.test(localeCode)) {
17
23
  throw new Error('Invalid locale code provided!');
@@ -20,11 +26,13 @@ export class LocaleObject {
20
26
  const [language, region] = localeCode.split('-');
21
27
 
22
28
  if (region) {
29
+ this.isRegionIndependent = false;
23
30
  // Workaround for `zh-Hant` locale, which is the only locale with non-uppercase region part
24
31
  const regionSuffix = region === 'hant' ? 'Hant' : region.toUpperCase();
25
32
 
26
33
  this.localeCode = [language, regionSuffix].join('-');
27
34
  } else {
35
+ this.isRegionIndependent = true;
28
36
  this.localeCode = language;
29
37
  }
30
38
  }
@@ -0,0 +1,164 @@
1
+ import { NewsroomLanguageSettings } from '@prezly/sdk';
2
+ import { LocaleCode, LocaleObject } from '../intl';
3
+ import { getAlternateLanguageLinks } from './getAlternateLanguageLinks';
4
+
5
+ function lang(code: LocaleCode, isDefault = false) {
6
+ return { code, is_default: isDefault } as NewsroomLanguageSettings;
7
+ }
8
+
9
+ function getTranslationUrl(locale: LocaleObject) {
10
+ return `translationUrl/${locale.toHyphenCode()}`;
11
+ }
12
+
13
+ function generateTranslationUrl(locale: LocaleObject) {
14
+ const translationUrl = getTranslationUrl(locale);
15
+
16
+ return `http://localhost:3000/${locale.toNeutralLanguageCode()}/${translationUrl}`;
17
+ }
18
+
19
+ describe('getAlternateLanguageLinks', () => {
20
+ it('should handle one en-GB non-default language', () => {
21
+ const links = getAlternateLanguageLinks([lang('en-GB')], generateTranslationUrl);
22
+
23
+ expect(links).toMatchObject([
24
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
25
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
26
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
27
+ ]);
28
+ });
29
+
30
+ it('should handle one en-GB default language', () => {
31
+ const links = getAlternateLanguageLinks([lang('en-GB', true)], generateTranslationUrl);
32
+
33
+ expect(links).toMatchObject([
34
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
35
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
36
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
37
+ ]);
38
+ });
39
+
40
+ it('should handle two full locales', () => {
41
+ const links = getAlternateLanguageLinks(
42
+ [lang('en-GB', true), lang('fr-FR')],
43
+ generateTranslationUrl,
44
+ );
45
+
46
+ expect(links).toMatchObject([
47
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
48
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
49
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr' },
50
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
51
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
52
+ ]);
53
+ });
54
+
55
+ it('should use en-GB locale as x-default even if other locale is set as default', () => {
56
+ const links = getAlternateLanguageLinks(
57
+ [lang('en-GB'), lang('fr-FR', true)],
58
+ generateTranslationUrl,
59
+ );
60
+
61
+ expect(links).toMatchObject([
62
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
63
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
64
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr' },
65
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
66
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
67
+ ]);
68
+ });
69
+
70
+ it('should use default locale as x-default when there is eng locale', () => {
71
+ const links = getAlternateLanguageLinks(
72
+ [lang('ru-RU'), lang('fr-FR', true)],
73
+ generateTranslationUrl,
74
+ );
75
+
76
+ expect(links).toMatchObject([
77
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr' },
78
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
79
+ { href: 'http://localhost:3000/ru/translationUrl/ru-RU', hrefLang: 'ru' },
80
+ { href: 'http://localhost:3000/ru/translationUrl/ru-RU', hrefLang: 'ru-RU' },
81
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'x-default' },
82
+ ]);
83
+ });
84
+
85
+ it('should include provided region independent locale', () => {
86
+ const links = getAlternateLanguageLinks(
87
+ [lang('en-US'), lang('fr'), lang('fr-FR', true)],
88
+ generateTranslationUrl,
89
+ );
90
+
91
+ expect(links).toMatchObject([
92
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en' },
93
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
94
+ { href: 'http://localhost:3000/fr/translationUrl/fr', hrefLang: 'fr' },
95
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
96
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'x-default' },
97
+ ]);
98
+ });
99
+
100
+ it('should use provided en region independent locale as x-default', () => {
101
+ const links = getAlternateLanguageLinks(
102
+ [lang('en'), lang('en-US'), lang('fr'), lang('fr-FR')],
103
+ generateTranslationUrl,
104
+ );
105
+
106
+ expect(links).toMatchObject([
107
+ { href: 'http://localhost:3000/en/translationUrl/en', hrefLang: 'en' },
108
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
109
+ { href: 'http://localhost:3000/fr/translationUrl/fr', hrefLang: 'fr' },
110
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
111
+ { href: 'http://localhost:3000/en/translationUrl/en', hrefLang: 'x-default' },
112
+ ]);
113
+ });
114
+
115
+ it('should fallback en link to en-US locale when there is en-GB locale', () => {
116
+ const links = getAlternateLanguageLinks(
117
+ [lang('en-GB'), lang('en-US')],
118
+ generateTranslationUrl,
119
+ );
120
+
121
+ expect(links).toMatchObject([
122
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en' },
123
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
124
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
125
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'x-default' },
126
+ ]);
127
+ });
128
+
129
+ it('should should omit locales if getTranslationUrl is undefined', () => {
130
+ const links = getAlternateLanguageLinks(
131
+ [lang('en-US'), lang('fr'), lang('fr-FR', true)],
132
+ (locale) => {
133
+ const translationUrl =
134
+ locale.toHyphenCode() === 'en-US' ? undefined : getTranslationUrl(locale);
135
+
136
+ if (translationUrl) {
137
+ return generateTranslationUrl(locale);
138
+ }
139
+
140
+ return undefined;
141
+ },
142
+ );
143
+
144
+ expect(links).toMatchObject([
145
+ { href: 'http://localhost:3000/fr/translationUrl/fr', hrefLang: 'fr' },
146
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
147
+ { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'x-default' },
148
+ ]);
149
+ });
150
+
151
+ it('should should not duplicate links when equal languages passed', () => {
152
+ const links = getAlternateLanguageLinks(
153
+ [lang('en-GB'), lang('en-GB'), lang('en-US'), lang('en-US')],
154
+ generateTranslationUrl,
155
+ );
156
+
157
+ expect(links).toMatchObject([
158
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en' },
159
+ { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
160
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
161
+ { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'x-default' },
162
+ ]);
163
+ });
164
+ });
@@ -0,0 +1,115 @@
1
+ import type { NewsroomLanguageSettings } from '@prezly/sdk';
2
+
3
+ import type { AlternateLanguageLink } from '../components-nextjs/PageSeo';
4
+ import type { LocaleObject } from '../intl';
5
+ import { getFallbackLocale } from '../intl';
6
+
7
+ import { bindLanguagesWithLocales } from './utils/bindLanguagesWithLocales';
8
+ import { createAlternateLanguageLink } from './utils/createAlternateLanguageLink';
9
+
10
+ export function getAlternateLanguageLinks(
11
+ availableLanguages: NewsroomLanguageSettings[],
12
+ generateTranslationUrl: (locale: LocaleObject) => string | undefined,
13
+ ): AlternateLanguageLink[] {
14
+ const languagesWithLocales = bindLanguagesWithLocales(availableLanguages);
15
+ const hrefLangLinks: Array<AlternateLanguageLink> = [];
16
+
17
+ function pushLocaleToLinks(locale: LocaleObject, hrefLang?: string) {
18
+ const link = createAlternateLanguageLink(locale, generateTranslationUrl);
19
+
20
+ if (!link) {
21
+ return;
22
+ }
23
+
24
+ if (hrefLang) {
25
+ hrefLangLinks.push({ ...link, hrefLang });
26
+ } else {
27
+ hrefLangLinks.push(link);
28
+ }
29
+ }
30
+
31
+ function populateDirectLinks() {
32
+ languagesWithLocales.forEach((binds) =>
33
+ binds.forEach(({ locale }) => pushLocaleToLinks(locale)),
34
+ );
35
+ }
36
+
37
+ function populateRegionIndependentLinks() {
38
+ languagesWithLocales.forEach((binds, regionIndependentLocaleCode) => {
39
+ // When there is no region independent locale of provided locales
40
+ // we still need something as a region independent locale
41
+ const localesArray = Array.from(binds.values()).map(({ locale }) => locale);
42
+
43
+ const hasRegionIndependentLocale = localesArray.some(
44
+ (locale) => locale.isRegionIndependent,
45
+ );
46
+
47
+ if (hasRegionIndependentLocale) {
48
+ return;
49
+ }
50
+
51
+ // Try to find some possible fallback for the regionIndependentLocaleCode from hardcoded list
52
+ // We try to find fallback from provided translations of current regionIndependentLocaleCode
53
+ // If there is no defined fallback in the list we will use just first translation as a region independent translation
54
+ const fallback =
55
+ getFallbackLocale(regionIndependentLocaleCode, localesArray) ?? localesArray.at(0);
56
+
57
+ if (fallback) {
58
+ pushLocaleToLinks(fallback, regionIndependentLocaleCode);
59
+ }
60
+ });
61
+ }
62
+
63
+ function populateDefault() {
64
+ // We use english locale as fallback since English is the most common language
65
+ const engFallback = 'en';
66
+ const defaultHrefLang = 'x-default';
67
+
68
+ let isDefaultAddedToLinks = false;
69
+
70
+ // First we try to find region independent english locale (en)
71
+ // If the story has just region independent `en` translation we will use it as default hreflang
72
+ languagesWithLocales.forEach((binds) =>
73
+ binds.forEach(({ locale }) => {
74
+ if (locale.isRegionIndependent && locale.toNeutralLanguageCode() === engFallback) {
75
+ pushLocaleToLinks(locale, defaultHrefLang);
76
+ isDefaultAddedToLinks = true;
77
+ }
78
+ }),
79
+ );
80
+
81
+ if (!isDefaultAddedToLinks) {
82
+ // If there is no explicit region independent `en` translation for this story
83
+ // we will try to find some link that already has `en` fallback
84
+ const fallbackFromRegionIndependentLocales = hrefLangLinks.find(
85
+ (alternate) => alternate.hrefLang === engFallback,
86
+ );
87
+
88
+ if (fallbackFromRegionIndependentLocales) {
89
+ hrefLangLinks.push({
90
+ ...fallbackFromRegionIndependentLocales,
91
+ hrefLang: defaultHrefLang,
92
+ });
93
+ isDefaultAddedToLinks = true;
94
+ }
95
+ }
96
+
97
+ if (!isDefaultAddedToLinks) {
98
+ // If there is no english locale at all we will use is_default language (provided from server)
99
+ languagesWithLocales.forEach((binds) =>
100
+ binds.forEach(({ locale, language }) => {
101
+ if (language.is_default) {
102
+ pushLocaleToLinks(locale, defaultHrefLang);
103
+ isDefaultAddedToLinks = true;
104
+ }
105
+ }),
106
+ );
107
+ }
108
+ }
109
+
110
+ populateDirectLinks();
111
+ populateRegionIndependentLinks();
112
+ populateDefault();
113
+
114
+ return hrefLangLinks.sort((a, b) => a.hrefLang.localeCompare(b.hrefLang));
115
+ }
@@ -0,0 +1 @@
1
+ export * from './getAlternateLanguageLinks';
@@ -0,0 +1,31 @@
1
+ import type { NewsroomLanguageSettings } from '@prezly/sdk';
2
+
3
+ import type { LangCode, LocaleCode } from '../../intl';
4
+ import { LocaleObject } from '../../intl';
5
+
6
+ interface LocaleWithLanguage {
7
+ language: NewsroomLanguageSettings;
8
+ locale: LocaleObject;
9
+ }
10
+
11
+ /**
12
+ * Group languages by their region independent language code (e.g. "en" for "en-US" and "en-GB").
13
+ */
14
+ export function bindLanguagesWithLocales(languages: NewsroomLanguageSettings[]) {
15
+ const localesByLangCode = new Map<LangCode, Map<LocaleCode, LocaleWithLanguage>>();
16
+
17
+ languages.forEach((language) => {
18
+ const languageLocale = LocaleObject.fromAnyCode(language.code);
19
+ const regionIndependentLanguageCode = languageLocale.toNeutralLanguageCode();
20
+ let currentLocale = localesByLangCode.get(regionIndependentLanguageCode);
21
+
22
+ if (!currentLocale) {
23
+ currentLocale = new Map();
24
+ localesByLangCode.set(regionIndependentLanguageCode, currentLocale);
25
+ }
26
+
27
+ currentLocale.set(languageLocale.toHyphenCode(), { language, locale: languageLocale });
28
+ });
29
+
30
+ return localesByLangCode;
31
+ }
@@ -0,0 +1,17 @@
1
+ import type { LocaleObject } from '../../intl';
2
+
3
+ export function createAlternateLanguageLink(
4
+ locale: LocaleObject,
5
+ generateTranslationUrl: (locale: LocaleObject) => string | undefined,
6
+ ) {
7
+ const translationUrl = generateTranslationUrl(locale);
8
+
9
+ if (!translationUrl) {
10
+ return undefined;
11
+ }
12
+
13
+ return {
14
+ hrefLang: locale.toHyphenCode(),
15
+ href: translationUrl,
16
+ };
17
+ }