Skip to content

Commit

Permalink
Rename url based on language just for english
Browse files Browse the repository at this point in the history
  • Loading branch information
11joselu committed Nov 28, 2024
1 parent 0bf5a68 commit 9c09dc3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
27 changes: 25 additions & 2 deletions build/build.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { execSync } from 'node:child_process';
import { i18n } from './i18n-utils.mjs';
import {DEFAULT_LANG, i18n} from './i18n-utils.mjs';
import {keyTranslations} from "./url-resolver.mjs";
import path from "node:path";
import fs from "node:fs";

import {fileURLToPath} from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

function runBuild(locale) {
console.log('Building for location: ' + locale)
Expand All @@ -8,8 +15,24 @@ function runBuild(locale) {
execSync('vite build --mode=production', { stdio: 'inherit' });
}

const locales = i18n.getLocales();

function renameFilesToMatchUrls(directory) {
for (const [originalName, translatedName] of Object.entries(keyTranslations)) {
const originalPath = path.join(directory, originalName.replace('/', ''));
const translatedPath = path.join(directory, translatedName.replace('/', ''));

if (fs.existsSync(originalPath)) {
fs.renameSync(originalPath, translatedPath);
} else {
throw Error(`File not found: ${originalName}`)
}
}
}

const locales = i18n.getLocales();
for (const locale of locales) {
runBuild(locale);
if (locale !== DEFAULT_LANG) {
renameFilesToMatchUrls(path.resolve(__dirname, '..', 'dist', locale));
}
}
19 changes: 17 additions & 2 deletions build/url-resolver.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { DEFAULT_LANG } from './i18n-utils.mjs';
export const keyTranslations = {
'/curso-docker.html': '/docker-training.html',
'/curso-legacy-code.html': '/legacy-code-training.html',
'/curso-quality-assurance.html': '/quality-assurance-training.html',
'/curso-refactoring-a-patrones.html': '/refactoring-patterns-training.html',
'/curso-tdd.html': '/tdd-training.html',
'/desarrollo.html': '/development.html',
'/programa-de-aceleracion.html': '/acceleration-program.html',
};

export function urlResolver(path, locale = process.env.locale) {
const host = process.env.BASE_DOMAIN || 'https://www.codium.team';
Expand All @@ -8,12 +17,18 @@ export function urlResolver(path, locale = process.env.locale) {
return path + '/' + locale;
}

return path;
return translateUrlInProduction(path);
}

if (locale && locale !== DEFAULT_LANG) {
return `${host}/${locale}${path}`;
return `${host}/${locale}${translateUrlInProduction(path)}`;
}

return host + path;
}

function translateUrlInProduction(path) {
if (process.env.NODE_ENV !== 'production') return path;

return keyTranslations[path] || path; // Return the translated path or the original if not found
}
28 changes: 28 additions & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,32 @@
<url>
<loc>https://blog.codium.team/2022-05_que-es-tdd</loc>
</url>

<url>
<loc>https://www.codium.team/en</loc>
</url>
<url>
<loc>https://www.codium.team/en/tdd-training.html</loc>
</url>
<url>
<loc>https://www.codium.team/en/legacy-code-training.html</loc>
</url>
<url>
<loc>https://www.codium.team/en/docker-training.html</loc>
</url>
<url>
<loc>https://www.codium.team/en/refactoring-patterns-training.html</loc>
</url>
<url>
<loc>https://www.codium.team/en/quality-assurance-training.html</loc>
</url>
<url>
<loc>https://blog.codium.team</loc>
</url>
<url>
<loc>https://www.codium.team/en/development.html</loc>
</url>
<url>
<loc>https://www.codium.team/en/acceleration-program.html</loc>
</url>
</urlset>
2 changes: 1 addition & 1 deletion src/partials/header/header.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<header class="header" id="js-header">
<div class="container">
<div class="header__wrapper">
<a href="/">
<a href="<%= url('/')%>">
<img
src="./img/resources/codium-brand-rectangle-black.svg"
alt="Logo de Codium"
Expand Down

0 comments on commit 9c09dc3

Please sign in to comment.