Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: Support for other headers, not 'accept-language' #15

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ You can do `import { ... } from '@intlify/utils'` the above utilities

### HTTP

- `getAcceptLanguages`
- `getAcceptLanguage`
- `getAcceptLocales`
- `getAcceptLocale`
- `getHeaderLanguages`
- `getHeaderLanguage`
- `getHeaderLocales`
- `getHeaderLocale`
- `getCookieLocale`
- `setCookieLocale`
- `getPathLanguage`
Expand Down
4 changes: 4 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
console.log('main navigator.language', navigator.language)
console.log('main navigator.languages', navigator.languages)

new Worker(new URL('./worker.ts', import.meta.url).href, { type: 'module' })
6 changes: 3 additions & 3 deletions playground/deno/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getAcceptLanguages } from 'https://esm.sh/@intlify/utils/web'
import { getHeaderLanguages } from 'https://esm.sh/@intlify/utils/web'

const port = 8125
Deno.serve({
port,
}, (req: Request) => {
const acceptLanguages = getAcceptLanguages(req)
return new Response(`detect accpect-language: ${acceptLanguages}`)
const languages = getHeaderLanguages(req)
return new Response(`detect accpect-language: ${languages}`)
})
console.log(`server listening on ${port}`)
6 changes: 3 additions & 3 deletions playground/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createServer } from 'node:http'
import { getAcceptLanguages } from '@intlify/utils/node'
import { getHeaderLanguages } from '@intlify/utils/node'

const server = createServer((req, res) => {
const acceptLanguages = getAcceptLanguages(req)
const languages = getHeaderLanguages(req)

res.writeHead(200)
res.end(`detect accpect-language: ${acceptLanguages}`)
res.end(`detect accpect-language: ${languages}`)
})

server.listen(8123)
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const DEFAULT_LANG_TAG = 'en-US'
export const DEFAULT_COOKIE_NAME = 'i18n_locale'
export const ACCEPT_LANGUAGE_HEADER = 'accept-language'
122 changes: 102 additions & 20 deletions src/h3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { beforeEach, describe, expect, test } from 'vitest'
import { createApp, eventHandler, toNodeListener } from 'h3'
import supertest from 'supertest'
import {
getAcceptLanguage,
getAcceptLanguages,
getAcceptLocale,
getAcceptLocales,
getCookieLocale,
getHeaderLanguage,
getHeaderLanguages,
getHeaderLocale,
getHeaderLocales,
setCookieLocale,
} from './h3.ts'
import { DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'

import type { App, H3Event } from 'h3'
import type { SuperTest, Test } from 'supertest'

describe('getAcceptLanguages', () => {
describe('getHeaderLanguages', () => {
test('basic', () => {
const mockEvent = {
node: {
Expand All @@ -26,7 +26,7 @@ describe('getAcceptLanguages', () => {
},
},
} as H3Event
expect(getAcceptLanguages(mockEvent)).toEqual(['en-US', 'en', 'ja'])
expect(getHeaderLanguages(mockEvent)).toEqual(['en-US', 'en', 'ja'])
})

test('any language', () => {
Expand All @@ -40,7 +40,7 @@ describe('getAcceptLanguages', () => {
},
},
} as H3Event
expect(getAcceptLanguages(mockEvent)).toEqual([])
expect(getHeaderLanguages(mockEvent)).toEqual([])
})

test('empty', () => {
Expand All @@ -52,7 +52,27 @@ describe('getAcceptLanguages', () => {
},
},
} as H3Event
expect(getAcceptLanguages(mockEvent)).toEqual([])
expect(getHeaderLanguages(mockEvent)).toEqual([])
})

test('custom header', () => {
// @ts-ignore: for mocking
const mockEvent = {
node: {
req: {
method: 'GET',
headers: {
'x-inlitfy-language': 'en-US,en,ja',
},
},
},
} as H3Event
expect(
getHeaderLanguages(mockEvent, {
name: 'x-inlitfy-language',
parser: (header) => header.split(','),
}),
).toEqual(['en-US', 'en', 'ja'])
})
})

Expand All @@ -68,7 +88,7 @@ describe('getAcceptLanguage', () => {
},
},
} as H3Event
expect(getAcceptLanguage(mockEvent)).toEqual('en-US')
expect(getHeaderLanguage(mockEvent)).toEqual('en-US')
})

test('any language', () => {
Expand All @@ -82,7 +102,7 @@ describe('getAcceptLanguage', () => {
},
},
} as H3Event
expect(getAcceptLanguage(mockEvent)).toEqual('')
expect(getHeaderLanguage(mockEvent)).toEqual('')
})

test('empty', () => {
Expand All @@ -94,11 +114,31 @@ describe('getAcceptLanguage', () => {
},
},
} as H3Event
expect(getAcceptLanguage(mockEvent)).toEqual('')
expect(getHeaderLanguage(mockEvent)).toEqual('')
})

test('custom header', () => {
// @ts-ignore: for mocking
const mockEvent = {
node: {
req: {
method: 'GET',
headers: {
'x-inlitfy-language': 'en-US,en,ja',
},
},
},
} as H3Event
expect(
getHeaderLanguage(mockEvent, {
name: 'x-inlitfy-language',
parser: (header) => header.split(','),
}),
).toEqual('en-US')
})
})

describe('getAcceptLocales', () => {
describe('getHeaderLocales', () => {
test('basic', () => {
const mockEvent = {
node: {
Expand All @@ -110,7 +150,7 @@ describe('getAcceptLocales', () => {
},
},
} as H3Event
expect(getAcceptLocales(mockEvent).map((locale) => locale.baseName))
expect(getHeaderLocales(mockEvent).map((locale) => locale.baseName))
.toEqual(['en-US', 'en', 'ja'])
})

Expand All @@ -125,7 +165,7 @@ describe('getAcceptLocales', () => {
},
},
} as H3Event
expect(getAcceptLocales(mockEvent)).toEqual([])
expect(getHeaderLocales(mockEvent)).toEqual([])
})

test('empty', () => {
Expand All @@ -137,11 +177,31 @@ describe('getAcceptLocales', () => {
},
},
} as H3Event
expect(getAcceptLocales(mockEvent)).toEqual([])
expect(getHeaderLocales(mockEvent)).toEqual([])
})

test('custom header', () => {
// @ts-ignore: for mocking
const mockEvent = {
node: {
req: {
method: 'GET',
headers: {
'x-inlitfy-language': 'en-US,en,ja',
},
},
},
} as H3Event
expect(
getHeaderLocales(mockEvent, {
name: 'x-inlitfy-language',
parser: (header) => header.split(','),
}).map((locale) => locale.baseName),
).toEqual(['en-US', 'en', 'ja'])
})
})

describe('getAcceptLocale', () => {
describe('getHeaderLocale', () => {
test('basic', () => {
const mockEvent = {
node: {
Expand All @@ -153,7 +213,7 @@ describe('getAcceptLocale', () => {
},
},
} as H3Event
const locale = getAcceptLocale(mockEvent)
const locale = getHeaderLocale(mockEvent)

expect(locale.baseName).toEqual('en-US')
expect(locale.language).toEqual('en')
Expand All @@ -171,7 +231,7 @@ describe('getAcceptLocale', () => {
},
},
} as H3Event
const locale = getAcceptLocale(mockEvent)
const locale = getHeaderLocale(mockEvent)

expect(locale.baseName).toEqual(DEFAULT_LANG_TAG)
})
Expand All @@ -187,7 +247,7 @@ describe('getAcceptLocale', () => {
},
},
} as H3Event
const locale = getAcceptLocale(mockEvent, 'ja-JP')
const locale = getHeaderLocale(mockEvent, { lang: 'ja-JP' })

expect(locale.baseName).toEqual('ja-JP')
})
Expand All @@ -204,7 +264,29 @@ describe('getAcceptLocale', () => {
},
} as H3Event

expect(() => getAcceptLocale(mockEvent, 'ja-JP')).toThrowError(RangeError)
expect(() => getHeaderLocale(mockEvent, { lang: 'ja-JP' })).toThrowError(
RangeError,
)
})

test('custom header', () => {
// @ts-ignore: for mocking
const mockEvent = {
node: {
req: {
method: 'GET',
headers: {
'x-inlitfy-language': 'en-US,en,ja',
},
},
},
} as H3Event
expect(
getHeaderLocale(mockEvent, {
name: 'x-inlitfy-language',
parser: (header) => header.split(','),
}).toString(),
).toEqual('en-US')
})
})

Expand Down
Loading