Comments (4)
@intellix I don't understand what the problem is:
- you don't like the
extractFromUrl
function? don't use it, it's just a helper method, you can easily create the method you prefer, and if you think it might be useful to others, contribute to this library - or is your redirect not working because it doesn't contain the localized URL? In this case, as in any case of navigation, you must localize the URL, for example:
export const onRequest: RequestHandler = ({ redirect, url }) => {
let lang: string | undefined = undefined;
const prefix = extractFromUrl(url);
if (prefix && validateLocale(prefix)) {
lang = config.supportedLocales.find(value => value.lang === prefix)?.lang;
}
const getPath = translatePath();
throw redirect(302, getPath('/somewhere', lang || config.defaultLocale.lang));
};
from qwik-speak.
I think because it's implied that you're extracting the locale from the URL, but in fact it just extracts the first segment which always requires the same additional work afterwards (validateLocale). Can't think of any reason not to always call validateLocale first.
Maybe a more useful export:
export function extractLocaleFromUrl(url: string) {
const localeMaybe = extractFromUrl(url);
return validateLocale(localeMaybe) ? localeMaybe : null;
}
Then it would look like:
Get locale using URL:
const locale = extractLocaleFromUrl(url) ?? config.defaultLocale.lang;
Get prefix for redirecting:
const locale = extractLocaleFromUrl(url);
const prefix = locale ? `/${locale}` : '';
throw redirect(302, `${prefix}/somewhere);
from qwik-speak.
I initially created validateLocale
because there were Qwik bugs that assigned the lang
parameter the wrong value. It is used in the solution without url rewriting: https://github.com/robisim74/qwik-speak/blob/main/docs/tutorial-routing.md#routing
So a method was simply added to help extract the locale from the URL in the case of url rewriting.
If you want to make your own method that uses the two methods (which are used in two different solutions), just do it.
Then if you are using url rewriting, throw redirect(302, `${prefix}/somewhere);
will not work, because the URL is not rewritten (the path is not translated), so you need to use:
throw redirect(302, getPath('/somewhere', lang || config.defaultLocale.lang));
from qwik-speak.
The two functions will remain separate, because each has a different responsibility.
See also https://github.com/robisim74/qwik-speak/blob/main/docs/tutorial-routing-rewrite.md#routing
from qwik-speak.
Related Issues (20)
- Error since latest version HOT 5
- Issue with runtime translation in some scenarios HOT 7
- Does qwik-speak-extract takes into account arrays? HOT 3
- Upgrade to qwik 1.3 HOT 2
- Showing 404 page in localized routing with lang param HOT 3
- Fix failing tests on pipeline HOT 1
- Conditional rendering children shows keys instead values HOT 3
- Feat: autoassign-keys HOT 36
- FYI: DeepL translator for qwik-speak HOT 3
- Qwik-speak-extract: Maximum call stack exceeded HOT 4
- If/else - condition HOT 3
- URL rewrite translations arent working
- Text reverts back to default value on components containing modular-form HOT 19
- Support for Zero case in plural HOT 2
- Qwik Speak resolving locale multiple times on first render HOT 1
- Sitemap is not generating / finding routes when using localized routing HOT 2
- NX monorepo, only default translations are served on client in js files when run locally HOT 2
- Use of new t = inlineTranslate() in useVisibleTask$ HOT 4
- [Feature request] Domain-based routing HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qwik-speak.