Skip to content

Commit

Permalink
feat(language-selector): add languageMapping prop to rename languages…
Browse files Browse the repository at this point in the history
… of choice

closes #116
  • Loading branch information
yassinedoghri committed Feb 26, 2023
1 parent fb946ec commit 20d94e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,21 @@ import { LanguageSelector } from "astro-i18next/components";
---
<LanguageSelector showFlag={true} class="my-select-class" />
<!-- LanguageSelector with custom language naming -->
<LanguageSelector
showFlag={true}
languageMapping={{ en: "Anglais" }}
class="my-select-class"
/>
```

#### LanguageSelector Props

| Prop name | Type (default) | Description |
| --------- | ------------------ | --------------------------------------------------------- |
| showFlag | ?boolean (`false`) | Choose to display the language emoji before language name |
| Prop name | Type (default) | Description |
| --------------- | --------------------- | ------------------------------------------------------------------------------------------- |
| showFlag | ?boolean (`false`) | Choose to display the language emoji before language name |
| languageMapping | ?object (`undefined`) | Rewrite language names by setting the locale as key and the wording of your choice as value |

### HeadHrefLangs component

Expand Down
19 changes: 17 additions & 2 deletions src/components/LanguageSelector.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,39 @@ import { localizePath } from "../..";
import localeEmoji from "locale-emoji";
import ISO6991 from "iso-639-1";
interface languageMapping {
[localeCode: string]: string;
}
export interface Props extends astroHTML.JSX.SelectHTMLAttributes {
showFlag?: boolean;
languageMapping?: languageMapping;
}
const supportedLanguages = i18next.languages;
const currentLanguage = i18next.language;
const { pathname } = Astro.url;
const { showFlag = false, ...attributes } = Astro.props;
const { showFlag = false, languageMapping, ...attributes } = Astro.props;
---

<select onchange="location = this.value;" {...attributes}>
{
supportedLanguages.map((supportedLanguage: string) => {
let value = localizePath(pathname, supportedLanguage);
const flag = showFlag ? localeEmoji(supportedLanguage) + " " : "";
const nativeName = ISO6991.getNativeName(supportedLanguage);

let nativeName = "";
if (
languageMapping &&
languageMapping.hasOwnProperty(supportedLanguage)
) {
nativeName = languageMapping[supportedLanguage];
} else {
nativeName = ISO6991.getNativeName(supportedLanguage);
}

const label = flag + nativeName;

return (
Expand Down

0 comments on commit 20d94e4

Please sign in to comment.