In my angular project I have set up i18n for "sv"(swedish) and "en".
What I want is to when accessing mypage.com, "/sv" or "/en" to be added to baseHref
so that correct index.html is loaded from firebase hosting based of "accept-language" in other words users for sweden will get "mypage.com/sv" and everybody else mypage.com/en would be the one to load.
I made some digging and it seems that this is nothing that comes automaticly with firebase hositng and some logic has to be added eaither in angular or firebase functions.
Adding logic in angular failed totaly(keep in mind that I want it to happen automaticly and not something that user picks in a dropdown)
…and logic for firebase functions although it’s working on the simplest level it messing up with my routing (adding alot of "/sv" to path when it should not + it feels like a fix). bellow code for this fix:
exports.redirectBasedOnLanguage = functions.https.onRequest((req:any, res:any) => {
// Default to English
let language = 'en';
// Check for the Accept-Language header and prefer Swedish if present
const acceptLanguage = req.headers['accept-language'];
if (acceptLanguage && acceptLanguage.startsWith('sv')) {
language = 'sv';
}
// Construct the redirect URL based on the detected language
// Note: You might want to adjust the path construction based on your URL structure
const targetUrl = `/${language}${req.path}`;
// Perform the redirection
res.redirect(302, targetUrl);
});
my firebase.json:
for above solution:
"rewrites": [
{
"source": "**",
"function": "redirectBasedOnLanguage"
}
];
and for set up without any firebaseFunctions fix (when following docs):
"rewrites": [
{
"source": "/sv{,/**}",
"destination": "/sv/index.html"
},
{
"source": "/sv-SE{,/**}",
"destination": "/sv/index.html"
},
{
"source": "**",
"destination": "/en/index.html"
}
]
What is the correct way to handle?
2
Answers
you may want to take a look at this firebase hosting i18n configuration.
https://firebase.google.com/docs/hosting/i18n-rewrites
To solve the problem that you are having, you need to implement on the Angular side and on the serving side.
So, on the Angular side, you need to build the application using a
base href
, which can be done in multiple ways, but the one that suits your problem the best, is using the CLI build options.You should specify the appropriate
base href
for each build. Since you are using multiple builds fori18n
, this static approach works perfectly for your use case.On the Firebase side (never used Firebase hosting, so adjust accordingly), you need to perform a rewrite (routing), which can be achieved using the Firebase cloud functions, but Firebase already offers you a built-in way to serve different
i18n
builds as mentioned on a different answer. On the documentation, they specify that they use theAccept-Language
header to detect the language, which is exactly what you want.In your
firebase.json
file, add the following configuration:Create a
localized-files
directory in the path specified in thefirebase.json
file, then create 1 directory for each language that you want to serve (en_ALL
andsv_ALL
) and inside, add your different builds.Ref: https://firebase.google.com/docs/hosting/i18n-rewrites#set-up-i18n-rewrites
Side note on why your function was misbehaving. You are always prepending the language code, even when it is already in the request URL. You have 2 possible solutions:
Solution 1 seems more appropriate, so you could just update your rewrite configuration to: