@@ -163,15 +163,17 @@ export interface GeocodingProvider {
163163 */
164164export class MapboxGeocodingProvider implements GeocodingProvider {
165165 private readonly accessToken : string
166+ private readonly language : string | null
166167 private readonly baseUrl = 'https://api.mapbox.com'
167168 private readonly rateLimitMs = 100 // Mapbox 速率限制:1000次/分钟
168169 private readonly rateLimiter : SequentialRateLimiter
169170 private readonly interprocessKey : string
170171 private readonly maxRetries = 3
171172 private readonly retryBaseDelayMs = 500
172173
173- constructor ( accessToken : string ) {
174+ constructor ( accessToken : string , language ?: string | null ) {
174175 this . accessToken = accessToken
176+ this . language = language ?? null
175177 this . rateLimiter = getGlobalRateLimiter ( `mapbox:${ accessToken } ` , this . rateLimitMs )
176178 this . interprocessKey = `mapbox:${ accessToken } `
177179 }
@@ -187,8 +189,9 @@ export class MapboxGeocodingProvider implements GeocodingProvider {
187189 url . searchParams . set ( 'access_token' , this . accessToken )
188190 url . searchParams . set ( 'longitude' , lon . toString ( ) )
189191 url . searchParams . set ( 'latitude' , lat . toString ( ) )
190- url . searchParams . set ( 'types' , 'address,place,district,region,country' )
191- url . searchParams . set ( 'language' , 'zh-Hants' )
192+ if ( this . language ) {
193+ url . searchParams . set ( 'language' , this . language )
194+ }
192195
193196 log . info ( `调用 Mapbox API: ${ lat } , ${ lon } ` )
194197
@@ -266,15 +269,17 @@ export class MapboxGeocodingProvider implements GeocodingProvider {
266269 */
267270export class NominatimGeocodingProvider implements GeocodingProvider {
268271 private readonly baseUrl : string
272+ private readonly language : string | null
269273 private readonly userAgent = 'afilmory/1.0'
270274 private readonly rateLimitMs = 1000 // Nominatim 要求至少1秒间隔
271275 private readonly rateLimiter : SequentialRateLimiter
272276 private readonly interprocessKey : string
273277 private readonly maxRetries = 3
274278 private readonly retryBaseDelayMs = 1000
275279
276- constructor ( baseUrl ?: string ) {
280+ constructor ( baseUrl ?: string , language ?: string | null ) {
277281 this . baseUrl = baseUrl || 'https://nominatim.openstreetmap.org'
282+ this . language = language ?? null
278283 this . rateLimiter = getGlobalRateLimiter ( `nominatim:${ this . baseUrl } ` , this . rateLimitMs )
279284 this . interprocessKey = `nominatim:${ this . baseUrl } `
280285 }
@@ -291,13 +296,16 @@ export class NominatimGeocodingProvider implements GeocodingProvider {
291296 url . searchParams . set ( 'lon' , lon . toString ( ) )
292297 url . searchParams . set ( 'format' , 'json' )
293298 url . searchParams . set ( 'addressdetails' , '1' )
294- url . searchParams . set ( 'accept-language' , 'zh-CN,zh,en' )
299+ if ( this . language ) {
300+ url . searchParams . set ( 'accept-language' , this . language )
301+ }
295302
296303 log . info ( `调用 Nominatim API: ${ lat } , ${ lon } ` )
297304
298305 const response = await fetch ( url . toString ( ) , {
299306 headers : {
300307 'User-Agent' : this . userAgent ,
308+ ...( this . language ? { 'Accept-Language' : this . language } : { } ) ,
301309 } ,
302310 } )
303311
@@ -372,20 +380,22 @@ export class NominatimGeocodingProvider implements GeocodingProvider {
372380 * @param provider 提供者类型
373381 * @param mapboxToken Mapbox access token(可选)
374382 * @param nominatimBaseUrl Nominatim 基础 URL(可选)
383+ * @param language 首选语言(可选,逗号分隔的 BCP47 列表)
375384 */
376385export function createGeocodingProvider (
377386 provider : 'mapbox' | 'nominatim' | 'auto' ,
378387 mapboxToken ?: string ,
379388 nominatimBaseUrl ?: string ,
389+ language ?: string | null ,
380390) : GeocodingProvider | null {
381391 // 如果指定了 Mapbox 或自动模式且有 token,使用 Mapbox
382392 if ( ( provider === 'mapbox' || provider === 'auto' ) && mapboxToken ) {
383- return new MapboxGeocodingProvider ( mapboxToken )
393+ return new MapboxGeocodingProvider ( mapboxToken , language )
384394 }
385395
386396 // 使用 Nominatim
387397 if ( provider === 'nominatim' || provider === 'auto' ) {
388- return new NominatimGeocodingProvider ( nominatimBaseUrl )
398+ return new NominatimGeocodingProvider ( nominatimBaseUrl , language )
389399 }
390400
391401 return null
0 commit comments