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: rename getLocale #6

Merged
merged 2 commits into from
Sep 22, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ You can do `import { ... } from '@intlify/utils'` the above utilities

- `getAcceptLanguages`
- `getAcceptLanguage`
- `getLocale`
- `getAcceptLocale`
- `getCookieLocale`
- `setCookieLocale`

Expand Down
1 change: 1 addition & 0 deletions playground/browser/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './style.css'
import intlifyLogo from '/intlify.svg'
import { setupCounter } from './counter.ts'
// @ts-expect-error: The URL format of import identifier is not allowd in TS as default.
import { isLocale } from 'https://esm.sh/@intlify/utils'

document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
Expand Down
12 changes: 6 additions & 6 deletions src/h3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import supertest from 'supertest'
import {
getAcceptLanguage,
getAcceptLanguages,
getAcceptLocale,
getCookieLocale,
getLocale,
setCookieLocale,
} from './h3.ts'
import { DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('getAcceptLanguage', () => {
})
})

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

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

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

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

expect(() => getLocale(mockEvent, 'ja-JP')).toThrowError(RangeError)
expect(() => getAcceptLocale(mockEvent, 'ja-JP')).toThrowError(RangeError)
})
})

Expand Down
6 changes: 4 additions & 2 deletions src/h3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export function getAcceptLanguage(event: H3Event): string {
}

/**
* get locale
* get {@link Intl.Locale | locale} from `accept-language` header
*
* @description wrap with {@link Intl.Locale | locale}
*
* @example
* example for h3:
Expand All @@ -93,7 +95,7 @@ export function getAcceptLanguage(event: H3Event): string {
*
* @returns {Intl.Locale} The first locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
*/
export function getLocale(
export function getAcceptLocale(
event: H3Event,
lang = DEFAULT_LANG_TAG,
): Intl.Locale {
Expand Down
12 changes: 6 additions & 6 deletions src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import supertest from 'supertest'
import {
getAcceptLanguage,
getAcceptLanguages,
getAcceptLocale,
getCookieLocale,
getLocale,
setCookieLocale,
} from './node.ts'
import { createServer, IncomingMessage, OutgoingMessage } from 'node:http'
Expand Down Expand Up @@ -64,14 +64,14 @@ describe('getAcceptLanguage', () => {
})
})

describe('getLocale', () => {
describe('getAcceptLocale', () => {
test('basic', () => {
const mockRequest = {
headers: {
'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
},
} as IncomingMessage
const locale = getLocale(mockRequest)
const locale = getAcceptLocale(mockRequest)

expect(locale.baseName).toEqual('en-US')
expect(locale.language).toEqual('en')
Expand All @@ -84,7 +84,7 @@ describe('getLocale', () => {
'accept-language': '*',
},
} as IncomingMessage
const locale = getLocale(mockRequest)
const locale = getAcceptLocale(mockRequest)

expect(locale.baseName).toEqual(DEFAULT_LANG_TAG)
})
Expand All @@ -95,7 +95,7 @@ describe('getLocale', () => {
'accept-language': '*',
},
} as IncomingMessage
const locale = getLocale(mockRequest, 'ja-JP')
const locale = getAcceptLocale(mockRequest, 'ja-JP')

expect(locale.baseName).toEqual('ja-JP')
})
Expand All @@ -106,7 +106,7 @@ describe('getLocale', () => {
'accept-language': 's',
},
} as IncomingMessage
expect(() => getLocale(mockRequest, 'ja-JP')).toThrowError(RangeError)
expect(() => getAcceptLocale(mockRequest, 'ja-JP')).toThrowError(RangeError)
})
})

Expand Down
6 changes: 4 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export function getAcceptLanguage(request: IncomingMessage): string {
}

/**
* get locale
* get {@link Intl.Locale | locale} from `accept-language` header
*
* @description wrap with {@link Intl.Locale | locale}
*
* @example
* example for Node.js request:
Expand All @@ -91,7 +93,7 @@ export function getAcceptLanguage(request: IncomingMessage): string {
*
* @returns {Intl.Locale} The first locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
*/
export function getLocale(
export function getAcceptLocale(
request: IncomingMessage,
lang = DEFAULT_LANG_TAG,
): Intl.Locale {
Expand Down
12 changes: 6 additions & 6 deletions src/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { describe, expect, test } from 'vitest'
import {
getAcceptLanguage,
getAcceptLanguages,
getAcceptLocale,
getCookieLocale,
getLocale,
setCookieLocale,
} from './web.ts'
import { DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'
Expand Down Expand Up @@ -46,11 +46,11 @@ describe('getAcceptLanguage', () => {
})
})

describe('getLocale', () => {
describe('getAcceptLocale', () => {
test('basic', () => {
const mockRequest = new Request('https://example.com')
mockRequest.headers.set('accept-language', 'en-US,en;q=0.9,ja;q=0.8')
const locale = getLocale(mockRequest)
const locale = getAcceptLocale(mockRequest)

expect(locale.baseName).toEqual('en-US')
expect(locale.language).toEqual('en')
Expand All @@ -60,23 +60,23 @@ describe('getLocale', () => {
test('accept-language is any language', () => {
const mockRequest = new Request('https://example.com')
mockRequest.headers.set('accept-language', '*')
const locale = getLocale(mockRequest)
const locale = getAcceptLocale(mockRequest)

expect(locale.baseName).toEqual(DEFAULT_LANG_TAG)
})

test('specify default language', () => {
const mockRequest = new Request('https://example.com')
mockRequest.headers.set('accept-language', '*')
const locale = getLocale(mockRequest, 'ja-JP')
const locale = getAcceptLocale(mockRequest, 'ja-JP')

expect(locale.baseName).toEqual('ja-JP')
})

test('RangeError', () => {
const mockRequest = new Request('https://example.com')
mockRequest.headers.set('accept-language', 's')
expect(() => getLocale(mockRequest, 'ja-JP')).toThrowError(RangeError)
expect(() => getAcceptLocale(mockRequest, 'ja-JP')).toThrowError(RangeError)
})
})

Expand Down
6 changes: 4 additions & 2 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export function getAcceptLanguage(request: Request): string {
}

/**
* get locale
* get {@link Intl.Locale | locale} from `accept-language` header
*
* @description wrap with {@link Intl.Locale | locale}
*
* @example
* example for Web API request on Bun:
Expand All @@ -90,7 +92,7 @@ export function getAcceptLanguage(request: Request): string {
*
* @returns {Intl.Locale} The first locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
*/
export function getLocale(
export function getAcceptLocale(
request: Request,
lang = DEFAULT_LANG_TAG,
): Intl.Locale {
Expand Down