Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Taku Amano · committed by GitHub · parent 93fc250 · Browse files · 2 files+53 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+53 added−7 removed

  • tests:+38 added−1 removed
  • other:+15 added−6 removed

2 files+53 added−7 removed

modified:src/middleware/language/index.test.ts+38 added-1 removed
1import { Hono } from '../../hono'2import { detectors } from './language'2import { DEFAULT_OPTIONS, detectors, normalizeLanguage } from './language'3import { languageDetector } from '.'45describe('languageDetector', () => {6 const createTestApp = (options = {}) => {
99 unmodified lines
106 })107 expect(await res.text()).toBe('zh-Hant')108 })109110 it('should select the longest truncated match regardless of supported language order', async () => {111 const app = createTestApp({112 supportedLanguages: ['zh', 'zh-Hant'],113 fallbackLanguage: 'zh',114 order: ['header'],115 })116117 const res = await app.request('/', {118 headers: {119 'accept-language': 'zh-Hant-CN',120 },121 })122 expect(await res.text()).toBe('zh-Hant')123 })124125 it('should fallback when truncation does not match any supported language', async () => {126 const app = createTestApp({127 supportedLanguages: ['en', 'ja'],128 fallbackLanguage: 'en',
70 unmodified lines
199 expect(await res.text()).toBe('en')200 })201 })202203 describe('Language Normalization', () => {204 it('should preserve the configured value for a case-insensitive truncated match', () => {205 expect(206 normalizeLanguage('zh-HANT-CN', {207 ...DEFAULT_OPTIONS,208 supportedLanguages: ['en', 'ZH-Hant'],209 })210 ).toBe('ZH-Hant')211 })212213 it('should handle a large unsupported language tag', () => {214 const unsupportedLanguage = 'x-'.repeat(30_000).slice(0, -1)215216 expect(217 normalizeLanguage(unsupportedLanguage, {218 ...DEFAULT_OPTIONS,219 supportedLanguages: ['en', 'ja'],220 })221 ).toBeUndefined()222 })223 })224225 describe('Path Detection', () => {226 it('should detect language from path', async () => {227 const app = createTestApp({228 order: ['path'],
280 unmodified lines
modified:src/middleware/language/language.ts+15 added-6 removed
105 unmodified lines
106 return options.supportedLanguages[exactIndex]107 }108109 // Progressive truncation (RFC 4647 Lookup)110 const parts = compLang.split('-')111 for (let i = parts.length - 1; i > 0; i--) {112 const candidate = parts.slice(0, i).join('-')113 const prefixIndex = compSupported.indexOf(candidate)114 if (prefixIndex !== -1) {115 return options.supportedLanguages[prefixIndex]110 let longestMatchIndex = -1111 let longestMatchLength = -1112 for (let i = 0; i < compSupported.length; i++) {113 const candidate = compSupported[i]114 if (115 candidate.length < compLang.length &&116 candidate.length > longestMatchLength &&117 compLang.startsWith(candidate) &&118 compLang[candidate.length] === '-'119 ) {120 longestMatchIndex = i121 longestMatchLength = candidate.length122 }123 }124 if (longestMatchIndex !== -1) {125 return options.supportedLanguages[longestMatchIndex]126 }127128 return undefined129 } catch {130 return undefined
179 unmodified lines