Giter Club home page Giter Club logo

Comments (4)

robisim74 avatar robisim74 commented on July 17, 2024

@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.

intellix avatar intellix commented on July 17, 2024

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.

robisim74 avatar robisim74 commented on July 17, 2024

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.

robisim74 avatar robisim74 commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.