diff --git a/components/RenderField/index.js b/components/RenderField/index.js index 08118b71..068cf69c 100644 --- a/components/RenderField/index.js +++ b/components/RenderField/index.js @@ -2,13 +2,10 @@ import { useTranslation } from 'react-i18next' export default function RenderField({ object, field, isHtml, maxSize }) { const { i18n } = useTranslation() - let content + const defaultLanguage = 'en' + const language = i18n.resolvedLanguage || defaultLanguage - if (object?.metadata) { - content = object.metadata[i18n.resolvedLanguage || 'en']?.[field] - } else { - content = object?.[field] - } + let content = object?.metadata?.[language]?.[field] ?? object?.[field] if (maxSize && content?.length > maxSize) { content = `${content.substring(0, maxSize)}...` diff --git a/lib/course.js b/lib/course.js index 41510b0c..804285da 100644 --- a/lib/course.js +++ b/lib/course.js @@ -61,11 +61,12 @@ export async function getHomeCourse() { } export function getFieldContent(object, field, i18n) { - let content + const defaultLanguage = 'en' + const language = i18n.resolvedLanguage || defaultLanguage - if (object?.metadata) { - content = object.metadata[i18n.resolvedLanguage || 'en']?.[field] - } else { + let content = object?.metadata?.[language]?.[field] + + if (content === undefined) { content = object?.[field] } diff --git a/pages/courses/[id].js b/pages/courses/[id].js index 3b0ff31e..a6b7097b 100644 --- a/pages/courses/[id].js +++ b/pages/courses/[id].js @@ -21,6 +21,7 @@ import Loading from '../../components/Loading' import { dateFormat } from '../../lib/dateFormat' import { useTranslation } from 'react-i18next' import RenderField from '../../components/RenderField' +import { toast } from 'react-toastify' function Course({ course, currentDate }) { if (!course.active) return @@ -35,6 +36,12 @@ function Course({ course, currentDate }) { const { t, i18n } = useTranslation() const language = i18n.resolvedLanguage + useEffect(() => { + if (course?.metadata && !course.metadata.hasOwnProperty(language)) { + toast.error(t('messages.language_not_available')) + } + }, [language]) + let counter = 0 useEffect(async () => { setCohorts(await getAllCohorts()) diff --git a/pages/courses/[id]/[section]/[lesson].js b/pages/courses/[id]/[section]/[lesson].js index af52e714..d51fad6e 100644 --- a/pages/courses/[id]/[section]/[lesson].js +++ b/pages/courses/[id]/[section]/[lesson].js @@ -40,6 +40,12 @@ function Lessons({ course, section, lesson, content, currentDate }) { const { t, i18n } = useTranslation() const language = i18n.resolvedLanguage + useEffect(() => { + if (course?.metadata && !course.metadata.hasOwnProperty(language)) { + toast.error(t('messages.language_not_available')) + } + }, [language]) + useEffect(async () => { if (auth.currentUser) { const userSession = await getUserFromFirestore(auth.currentUser) diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index b56ec24c..83330050 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -128,7 +128,8 @@ "exercise_not_submitted": "You have not submitted the exercise for this lesson yet", "already_on_first_lesson": "You are already on the first lesson.", "data_updated_success": "Data updated successfully!", - "error_updating_data": "Error updating data" + "error_updating_data": "Error updating data", + "language_not_available": "Content not available for the selected language. Using the default language." }, "twitter-share": "Networking is everything, how about sharing your progress with your friends on Twitter?", "buttons": { diff --git a/public/locales/pt-BR/translation.json b/public/locales/pt-BR/translation.json index 9ea7a4a5..cf093081 100644 --- a/public/locales/pt-BR/translation.json +++ b/public/locales/pt-BR/translation.json @@ -128,7 +128,8 @@ "exercise_not_submitted": "Você ainda não enviou o exercício desta lição", "already_on_first_lesson": "Você já está na primeira lição.", "data_updated_success": "Dados atualizados com sucesso!", - "error_updating_data": "Erro ao atualizar dados" + "error_updating_data": "Erro ao atualizar dados", + "language_not_available": "Conteúdo não disponível para o idioma selecionado. Usando o idioma padrão." }, "twitter-share": "Networking é tudo, que tal compartilhar seu progresso com os seus amigos no Twitter?", "link_github_account": "Deseja vincular o github à sua conta já cadastrada?",