Skip to content

Commit 07e41ad

Browse files
committed
add i18next functionality
1 parent e64e9a4 commit 07e41ad

File tree

8 files changed

+124
-39
lines changed

8 files changed

+124
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
[![Actions Status](https://github.com/olgarozmetova/frontend-project-11/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/olgarozmetova/frontend-project-11/actions)
99

10-
### View a website:
10+
### View an app:
1111

1212
https://frontend-project-11-pi-seven.vercel.app

package-lock.json

Lines changed: 43 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dependencies": {
3333
"@popperjs/core": "^2.11.8",
3434
"bootstrap": "^5.3.8",
35+
"i18next": "^25.6.1",
3536
"on-change": "^6.0.1",
3637
"yup": "^1.7.1"
3738
}

src/i18n.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import i18next from 'i18next'
2+
import ru from './locales/ru.js'
3+
4+
export const initI18n = () =>
5+
i18next.init({
6+
lng: 'ru',
7+
debug: true,
8+
resources: {
9+
ru,
10+
},
11+
})

src/locales/ru.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
translation: {
3+
form: {
4+
success: 'RSS успешно загружен',
5+
},
6+
errors: {
7+
invalidUrl: 'Ссылка должна быть валидным URL',
8+
exists: 'RSS уже существует',
9+
},
10+
},
11+
}

src/main.js

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,55 @@
11
import './style.css'
2-
import { buildSchema } from './validation.js'
2+
3+
import { initI18n } from './i18n.js'
4+
import { configureYup, buildSchema } from './validation.js'
35
import { initView } from './view.js'
46

5-
const state = {
6-
feeds: [],
7-
form: {
8-
status: 'filling',
9-
error: null,
10-
},
11-
}
7+
initI18n()
8+
.then(() => {
9+
configureYup()
1210

13-
const elements = {
14-
form: document.querySelector('.rss-form'),
15-
input: document.querySelector('#url-input'),
16-
feedback: document.querySelector('.feedback'),
17-
}
11+
const state = {
12+
feeds: [],
13+
form: {
14+
status: 'filling',
15+
error: null,
16+
},
17+
}
1818

19-
const watched = initView(state, elements)
19+
const elements = {
20+
form: document.querySelector('.rss-form'),
21+
input: document.querySelector('#url-input'),
22+
feedback: document.querySelector('.feedback'),
23+
}
2024

21-
elements.form.addEventListener('submit', (e) => {
22-
e.preventDefault()
25+
const watched = initView(state, elements)
2326

24-
const formData = new FormData(e.target)
25-
const url = formData.get('url').trim()
27+
elements.form.addEventListener('submit', (e) => {
28+
e.preventDefault()
2629

27-
const data = { url }
28-
const schema = buildSchema(watched.feeds)
30+
const formData = new FormData(e.target)
31+
const url = formData.get('url').trim()
2932

30-
watched.form.status = 'filling'
31-
watched.form.error = null
33+
const data = { url }
34+
const schema = buildSchema(watched.feeds)
3235

33-
schema
34-
.validate(data)
35-
.then(({ url }) => {
36-
// successful
37-
watched.feeds.push(url)
38-
watched.form.status = 'success'
36+
watched.form.status = 'filling'
3937
watched.form.error = null
38+
39+
schema
40+
.validate(data)
41+
.then(({ url }) => {
42+
// successful
43+
watched.feeds.push(url)
44+
watched.form.status = 'success'
45+
})
46+
.catch((err) => {
47+
// errors
48+
watched.form.status = 'invalid'
49+
watched.form.error = err.message
50+
})
4051
})
41-
.catch((err) => {
42-
// errors
43-
watched.form.status = 'invalid'
44-
watched.form.error = err.message
45-
})
46-
})
52+
})
53+
.catch((err) => {
54+
console.error('Ошибка инициализации i18next:', err)
55+
})

src/validation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import * as yup from 'yup'
22

3+
export const configureYup = () => {
4+
yup.setLocale({
5+
string: {
6+
url: 'errors.invalidUrl',
7+
},
8+
mixed: {
9+
notOneOf: 'errors.exists',
10+
},
11+
})
12+
}
13+
314
export const buildSchema = urls =>
415
yup.object().shape({
516
url: yup.string().trim().required().url().notOneOf(urls),

src/view.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import onChange from 'on-change'
2+
import i18next from 'i18next'
23

34
export const initView = (state, elements) => {
45
const watched = onChange(state, (path, value) => {
@@ -9,7 +10,7 @@ export const initView = (state, elements) => {
910
input.classList.add('is-invalid')
1011
feedback.classList.remove('text-success')
1112
feedback.classList.add('text-danger')
12-
feedback.textContent = 'Ссылка должна быть валидным URL'
13+
feedback.textContent = i18next.t(value)
1314
}
1415
else {
1516
input.classList.remove('is-invalid')
@@ -20,7 +21,7 @@ export const initView = (state, elements) => {
2021
if (path === 'form.status' && value === 'success') {
2122
feedback.classList.remove('text-danger')
2223
feedback.classList.add('text-success')
23-
feedback.textContent = 'RSS успешно загружен'
24+
feedback.textContent = i18next.t('form.success')
2425

2526
form.reset()
2627
input.focus()

0 commit comments

Comments
 (0)