Localize client API responses by lang query param.

Client endpoints now return a single-language string (default tm) so the frontend can pass ?lang= and skip picking from nested tm/ru/en objects.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-06 12:27:10 +05:00
parent 76848f4982
commit ddd898078c
5 changed files with 202 additions and 110 deletions

View File

@@ -39,19 +39,13 @@ function getData(url = '') {
.then(response => response.json());
}
function localizedContent(item) {
if (window.locale === 'ru') {
return item.contentRu
}
if (window.locale === 'en') {
return item.contentEn
}
return item.contentTm
function getCurrentLang() {
return window.locale || 'tm';
}
function getCurrentLang()
{
return window.location.search.includes('lang=ru') ? 'ru' : '';
function withLang(url) {
const sep = url.includes('?') ? '&' : '?';
return url + sep + 'lang=' + getCurrentLang();
}
function isHomepage() {
@@ -127,7 +121,7 @@ async function fetchMenuItems() {
const container = $_ID('menu-items-container')
const sideBarMenuItems = $_ID('sidebar-menu-items')
const response = await getData('/api/v1/client/users/categories')
const response = await getData(withLang('/api/v1/client/users/categories'))
response.data.forEach(menuItem => {
let subcategoriesTemplate = '';
@@ -140,7 +134,7 @@ async function fetchMenuItems() {
<a class="bg-trasparent lg:bg-white" href="${
'/page.html?id=' + subcategory.id + '&lang=' + getCurrentLang()
}">
${subcategory.name[window.locale]}
${subcategory.name}
</a>
</li>
`;
@@ -148,7 +142,7 @@ async function fetchMenuItems() {
})
}
const categoryName = (menuItem.name[window.locale] || '').toUpperCase()
const categoryName = (menuItem.name || '').toUpperCase()
container.insertAdjacentHTML('beforeend', `
<div class="dropdown" style="z-index:1000">
@@ -222,16 +216,16 @@ async function fetchMiniSliders() {
async function fetchMiniCategories() {
const miniCategoryContainer = $_ID('mini-category-content-container')
const response = await getData('/api/v1/client/users/static')
const response = await getData(withLang('/api/v1/client/users/static'))
removeByClassName('mini-categories-loader')
response.data.statics.forEach(category => {
miniCategoryContainer.insertAdjacentHTML('beforeend', `
<a href="/action.html?id=${category.id + '&lang=' + getCurrentLang()}">
<a href="/action.html?id=${category.id}&lang=${getCurrentLang()}">
<div class="bg-gray-100 flex flex-1 flex-col items-center gap-3 py-7 px-3 mt-2 rounded-md">
<img src="${baseUrl() + category.logo}" class="w-16 h-16" alt="..." loading="lazy">
<h1 class="text-gray-700 uppercase text-center">${category.title[window.locale]}</h1>
<h1 class="text-gray-700 uppercase text-center">${category.title}</h1>
</div>
</a>
`)
@@ -240,10 +234,10 @@ async function fetchMiniCategories() {
async function fetchCurrencies() {
const currencyTable = $_ID('currency-table')
const response = await getData('/api/v1/client/users/currency')
const response = await getData(withLang('/api/v1/client/users/currency'))
removeByClassName('currency-table-loader')
currencyTable.innerHTML = response.data.content[window.locale];
currencyTable.innerHTML = response.data.content;
currencyTable.querySelector('table').classList.add('table-auto', 'bg-gray-500', 'text-sm', 'text-white', 'w-full')
Array.from(currencyTable.getElementsByTagName('th')).forEach(item => {
@@ -258,22 +252,22 @@ async function fetchCurrencies() {
}
})
$_ID('currency-message-container').innerHTML = response.data.title[window.locale];
$_ID('currency-message-container').innerHTML = response.data.title;
}
async function fetchNews() {
const newsContainer = $_ID('news-content-container')
const response = await getData('/api/v1/client/users/news?limit=6&page=1')
const response = await getData(withLang('/api/v1/client/users/news?limit=6&page=1'))
removeByClassName('news-loaders')
response.data.news.forEach(news => {
newsContainer.insertAdjacentHTML('beforeend', `
<a href="news.html?id=${news.id + '&lang=' + getCurrentLang()}">
<a href="news.html?id=${news.id}&lang=${getCurrentLang()}">
<div class="flex flex-col px-3 mt-3">
<div class="h-60">
<img src="${baseUrl() + news.imagePath}" class="news-img-size" alt="..." loading="lazy">
</div>
<h1 class="text-base text-gray-800 mt-2 py-2 px-1 text-center">${news.title[window.locale]}</h1>
<h1 class="text-base text-gray-800 mt-2 py-2 px-1 text-center">${news.title}</h1>
</div>
</a>
`)
@@ -289,17 +283,17 @@ async function fetchSubCategory() {
const pageContent = $_ID('page-content');
const response = await getData('/api/v1/client/users/sub-categories/' + pageURL.searchParams.get('id'))
const response = await getData(withLang('/api/v1/client/users/sub-categories/' + pageURL.searchParams.get('id')))
removeByClassName('page-content-loader')
pageContent.innerHTML = `
<h1 id="page-title" class="uppercase mt-5 mb-5 text-center text-lg green-color text-bold">
${response.data.name[window.locale]}
${response.data.name}
</h1>
<div>
${response.data.content[window.locale]}
${response.data.content}
</div>
`;
}
@@ -313,7 +307,7 @@ async function fetchBannerContent() {
const pageContent = $_ID('page-content');
const response = await getData('/api/v1/client/users/banners/' + pageURL.searchParams.get('id'))
const response = await getData(withLang('/api/v1/client/users/banners/' + pageURL.searchParams.get('id')))
const banner = response.data
removeByClassName('page-content-loader')
@@ -324,7 +318,7 @@ async function fetchBannerContent() {
pageContent.innerHTML = `
<h1 id="page-title" class="uppercase mt-5 mb-5 text-center text-lg green-color text-bold">
${banner.title[window.locale]}
${banner.title}
</h1>
<div>
@@ -332,7 +326,7 @@ async function fetchBannerContent() {
</div>
<div>
${localizedContent(banner)}
${banner.content || ''}
</div>
`;
}
@@ -346,17 +340,17 @@ async function fetchMiniCategory() {
const pageContent = $_ID('page-content');
const response = await getData('/api/v1/client/users/static/' + pageURL.searchParams.get('id'))
const response = await getData(withLang('/api/v1/client/users/static/' + pageURL.searchParams.get('id')))
removeByClassName('page-content-loader')
pageContent.innerHTML = `
<h1 id="page-title" class="uppercase mt-5 mb-5 text-center text-lg green-color text-bold">
${response.data.title[window.locale]}
${response.data.title}
</h1>
<div>
${localizedContent(response.data)}
${response.data.content || ''}
</div>
`;
}
@@ -370,13 +364,13 @@ async function fetchNewsContent() {
const pageContent = $_ID('page-content');
const response = await getData('/api/v1/client/users/news/' + pageURL.searchParams.get('id'))
const response = await getData(withLang('/api/v1/client/users/news/' + pageURL.searchParams.get('id')))
removeByClassName('page-content-loader')
pageContent.innerHTML = `
<h1 id="page-title" class="uppercase mt-5 mb-5 text-center text-lg green-color text-bold">
${response.data.title[window.locale]}
${response.data.title}
</h1>
<div class="mb-5">
@@ -388,7 +382,7 @@ async function fetchNewsContent() {
${ new Date(response.data.dateRelease).toLocaleDateString('en-GB').replace(/\//g, '.') + 'ý' }
</p>
${response.data.content[window.locale]}
${response.data.content || ''}
</div>
`;
}