feat: add build script

This commit is contained in:
Matthieu Bessat 2022-07-14 18:16:36 +02:00
parent e202802a56
commit 6592b70aef
4 changed files with 60 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules
vendor
build

50
build.sh Executable file
View file

@ -0,0 +1,50 @@
#!/bin/bash
PORT=8452
echo "Trying to start a PHP server on port $PORT"
php -S 127.0.0.1:$PORT -t ./public &
phpPID=$!
echo "PHP server running with PID $phpPID"
sleep 0.3
mkdir ./build
cd build
locales=('fr' 'en')
for locale in "${locales[@]}"; do
echo "Downloading $locale..."
mkdir $locale
cd $locale
# wget with custom header to not render link to change language because wget will follow them
wget -k -K -E -r -l 10 -p -N -F -nH http://127.0.0.1:$PORT/$locale --header="Enable-Locale-Switch: no"
mv $locale.html index.html
# Multiple sed commands to clean up the mess made by wget replacing stuff
# sed -E -i 's/\"([a-z]{2})\.html/\"\/\1/g' *.html
# for the main page, remove broken link to the same page (because we renamed to index.html)
sed -E -i "s/\"$locale\.html/\"/g" index.html
# for each file other than index.html, put a proper link back to the main page
find ./ -not -name "index.html" -name "*.html" -exec sed -E -i "s/\"(\.\.\/)?$locale\.html/\"\1index.html/g" {} \+
# repair the links of the footer locale switcher
# for each line with data-lang-switch="{locale}" replace the href with the link to /{locale}/path...
# attempt to do with pipe failed, so I use a non optimal for loop
# find ./ -name "*.html" -exec sh -c 'sed -E -i "s/data-lang-switch=\"([a-z]{2})\".+href\=\".*\"/href\=\"\/\1\/{}\"/g" {}' \;
for path in $(find ./ -name "*.html"); do
pathM=`echo $path | cut -c 3-`
sed -E -i "s%data-lang-switch=\"([a-z]{2})\".+href\=\".*\"%href\=\"/\1/$pathM\"%g" $path
done
cd ..
done
kill $phpPID
echo "Done."

View file

@ -25,7 +25,10 @@ class TwigLocalizationMiddleware {
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->container->get(Twig::class)->getEnvironment()->addGlobal('locale', $request->getAttribute('locale'));
$env = $this->container->get(Twig::class)->getEnvironment();
$env->addGlobal('locale', $request->getAttribute('locale'));
$env->addGlobal('enableLocaleSwitch', $request->hasHeader('Enable-Locale-Switch') ? ($request->getHeader('Enable-Locale-Switch')[0] === 'yes') : true);
return $handler->handle($request);
}

View file

@ -38,14 +38,14 @@
</div>
<div class="footer-right">
<div class="locale-switch-short">
<a href="/fr">FR</a>
<a href="/en">EN</a>
<a data-lang-switch="fr" href="{% if enableLocaleSwitch %}/fr{% endif %}">FR</a>
<a data-lang-switch="en" href="{% if enableLocaleSwitch %}/en{% endif %}">EN</a>
</div>
<div class="locale-switch-large">
<div class="l">{{ getLocalizedStr('locales.choose') }}</div>
<div class="r">
<a href="/fr">Français</a>
<a href="/en">English</a>
<a data-lang-switch="fr" href="{% if enableLocaleSwitch %}/fr{% endif %}">Français</a>
<a data-lang-switch="en" href="{% if enableLocaleSwitch %}/en{% endif %}">English</a>
</div>
</div>
</div>