Skip to content

Commit

Permalink
fix: use the translated country and language names
Browse files Browse the repository at this point in the history
Use the translated country and language names in combo boxes.
  • Loading branch information
bfabio committed Feb 1, 2024
1 parent 7ee2f11 commit 77fb35e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 261 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"cldr-data": "^36.0.1",
"clean-deep": "^3.4.0",
"copy-to-clipboard": "^3.3.3",
"countries-list": "^3.0.6",
"deepmerge": "^4.3.1",
"design-react-kit": "^5.0.0-7",
"dotenv": "^16.3.1",
Expand Down
251 changes: 0 additions & 251 deletions src/app/contents/countries.ts

This file was deleted.

19 changes: 12 additions & 7 deletions src/app/contents/fields/generic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import categories from "../categories";
import scopes from "../scopes";
import licenses from "../../../generated/licenses.json";
import { allLangs } from '../../../i18n';
import countries from "../countries";
import { allLangs, allCountries } from '../../../i18n';

const developmentStatus_list = [
"concept",
Expand Down Expand Up @@ -303,8 +302,10 @@ const fields = (): Array<Field> => {
type: "array",
items: {
title: "item",
type: "string",
enum: countries,
type: "object",
dataKey: "value",
textField: "text",
enum: allCountries(),
},
section: 6,
group: "intendedAudience",
Expand All @@ -315,8 +316,10 @@ const fields = (): Array<Field> => {
type: "array",
items: {
title: "item",
type: "string",
enum: countries,
type: "object",
dataKey: "value",
textField: "text",
enum: allCountries(),
},
section: 6,
group: "intendedAudience",
Expand Down Expand Up @@ -358,7 +361,9 @@ const fields = (): Array<Field> => {
type: "array",
items: {
title: "item",
type: "string",
type: "object",
dataKey: "value",
textField: "text",
enum: allLangs(),
},
widget: "tags",
Expand Down
5 changes: 5 additions & 0 deletions src/app/form/widgets/TagWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const TagWidget = (props) => {
});
const className = classNames(["form-group", { "has-error": invalid }]);

const multiSelectDataOpts = (props.schema.items.dataKey && props.schema.items.textField)
? { dataKey: props.schema.items.dataKey, textField: props.schema.items.textField }
: {};

return (
<div className={className}>
<label className="control-label active text-muted" htmlFor={id}>
Expand All @@ -27,6 +31,7 @@ const TagWidget = (props) => {

<Multiselect
{...inputProps}
{...multiSelectDataOpts}
ref={ref}
id={id}
onBlur={() => inputProps.onBlur()}
Expand Down
14 changes: 11 additions & 3 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import locales from "locale-codes";
import { countries } from "countries-list";
import LanguageDetector from 'i18next-browser-languagedetector';

import { FALLBACK_LANGUAGE } from "../app/contents/constants";
import en from "./locales/en.json";
import fr from "./locales/fr.json";
import it from "./locales/it.json";

type LocalizedEntity = 'language' | 'region';

const resources = {
en: { translation: en },
fr: { translation: fr },
Expand Down Expand Up @@ -37,16 +40,21 @@ i18n
}
});

export const displayName = (tag: string, locale: string = i18n.language) => {
export const displayName = (tag: string, locale: string = i18n.language, entity: LocalizedEntity) => {
try {
return new Intl.DisplayNames([locale], { type: 'language' }).of(tag);
return new Intl.DisplayNames([locale], { type: entity }).of(tag);
} catch (e) {
return null;
}
}

export const allLangs = (locale: string = i18n.language) => {
return locales.all
.map(l => ({ text: displayName(l.tag, locale), value: l.tag }))
.map(l => ({ text: displayName(l.tag, locale, 'language'), value: l.tag }))
.filter(e => e !== null)
}

export const allCountries = (locale: string = i18n.language) => {
return Object.keys(countries)
.map((countryCode) => ({ text: displayName(countryCode, locale, 'region'), value: countryCode }))
}

0 comments on commit 77fb35e

Please sign in to comment.