Git Product home page Git Product logo

avansai / resolve-accept-language Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 3.0 4.19 MB

Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.

Home Page: https://www.npmjs.com/package/resolve-accept-language

License: MIT License

TypeScript 78.41% JavaScript 21.59%
i18n internationalization accept-language accept-language-parser rfc-4646 rfc-4647 bcp-47 bcp47 rfc-2616 locale

resolve-accept-language's People

Contributors

dasdinggehtnicht avatar nbouvrette avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

resolve-accept-language's Issues

Improve TypeScript type inference for string values

Hello,

thank you for this very useful package! It would be awesome if the type definitions supported a bit nicer inferring of return values. This way one could use literal string values.

Based on my testing something like this in resolveAcceptLanguage would already help:

const resolveAcceptLanguage = (
acceptLanguageHeader: string,
locales: string[],
defaultLocale: string
): string => {

const resolveAcceptLanguage = <TLocale extends string>(
  acceptLanguageHeader: string,
  locales: TLocale[],
  defaultLocale: TLocale
): TLocale => {

It would allow type inference like:

Screenshot 2023-05-08 at 13 27 07

frequent releases

Is there a specific reason why a new version is released so often? If only the dev dependencies are updated, I see no reason for a new release, but maybe I'm missing something.

Feature request - Allow callers to discover when no languages match

Thank you for you great package.

In the event that no languages match, it can be interesting to serve language agnostic content (such as a language selector) rather than content in an arbitrary default language. Unfortunately the current API does not allow the caller to identify when no languages match. A suggested API might be to allow undefined to be passed as the default language, which would result in undefined being returned when no matches are found. This would admittedly make the return typing dirtier since it would force callers to check for undefined even when a default language was provided.

Underscores aren't properly parsed

Hi @nbouvrette, thank you for the fantastic library! ๐Ÿ™Œ

One question though, should underscores also be considered in the resolving algorithm?

For example:

var resolveAcceptLanguage = require("resolve-accept-language")

console.log(
  resolveAcceptLanguage.default('de_DE;q=0.01,en-CA;q=0.1,en-US;q=0.001', ['en-US', 'fr-CA', 'de-DE'], 'en-US')
);

I would expect to be parsed to de-DE but results in en-US instead.

Wondering if this is intended behaviour and if so how come (for my own knowledge), or if this is a bug (edit: an improvement that makes sense for the library to have).

languageBased when exact local is in header

Hi,

When i have the the AcceptLangauage header: de,de-DE;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 and then pass the locales: ['de-AT', 'de-DE']. The local de-AT gets matched based on the language.

I think the package should return the exact matcht. In this case de-DE

What do you think about this?

If you need more information please hit me up.

Suggestion: resolve same country in other language if nothing else matches

Hello, thank you for this library. I just have a suggestion for an additional (maybe optional) MatchType:

When all other MatchTypes have found nothing and the current implementation would return the defaultLocale, attempt the following match:

  • Only look at the country codes. What is the highest quality country code that is also a country code in one of the supported locales?
  • Return the first supported locale with that country code

This MatchType would basically be the same as the already implemented "relatedLocaleBased", just for countries instead of languages. The idea behind it is that if no language matches, it would be better to at least stay in the country you're accepting instead of using the default locale which is not in your language and probably also not your country.

Example:

  • the user only accepts "af-ZA"
  • the default locale is "en-US"
  • none of the supported locales support the language "af", but one of those locales is "en-ZA"

With this new MatchType, the user would receive "en-ZA", which is a better match for them than the default "en-US" because at least the country would still match their preference.

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.