fix: geo show
Node.js CI / build (push) Has been cancelled

This commit is contained in:
lly
2026-07-23 16:30:02 +08:00
parent c91156820c
commit c7691ae9a7
5 changed files with 74 additions and 47 deletions
+3 -2
View File
@@ -1,5 +1,5 @@
import { BROWSERS, OS_NAMES } from '@/lib/constants';
import regions from '../../../public/iso-3166-2.json';
import { getRegionNames } from '@/lib/geo';
import { useCountryNames } from './useCountryNames';
import { useLanguageNames } from './useLanguageNames';
import { useLocale } from './useLocale';
@@ -10,6 +10,7 @@ export function useFormat() {
const { locale } = useLocale();
const { countryNames } = useCountryNames(locale);
const { languageNames } = useLanguageNames(locale);
const regionNames = getRegionNames(locale);
const formatOS = (value: string): string => {
return OS_NAMES[value] || value;
@@ -29,7 +30,7 @@ export function useFormat() {
const formatRegion = (value?: string): string => {
const [country] = value?.split('-') || [];
return regions[value] ? `${regions[value]}, ${countryNames[country]}` : value;
return regionNames[value] ? `${regionNames[value]}, ${countryNames[country]}` : value;
};
const formatCity = (value: string, country?: string): string => {
+2 -1
View File
@@ -1,8 +1,9 @@
import regions from '../../../public/iso-3166-2.json';
import { getRegionNames } from '@/lib/geo';
import { useCountryNames } from './useCountryNames';
export function useRegionNames(locale: string) {
const { countryNames } = useCountryNames(locale);
const regions = getRegionNames(locale);
const getRegionName = (regionCode: string, countryCode?: string) => {
if (!countryCode) {
+1 -44
View File
@@ -4,6 +4,7 @@ import ipaddr from 'ipaddr.js';
import isLocalhost from 'is-localhost-ip';
import maxmind from 'maxmind';
import { UAParser } from 'ua-parser-js';
import { getChinaRegionCode } from '@/lib/geo';
import { getIpAddress, stripPort } from '@/lib/ip';
import { safeDecodeURIComponent } from '@/lib/url';
@@ -11,43 +12,6 @@ const MAXMIND = 'maxmind';
const IP_LOCATION_API_URL = 'https://ip9.com.cn/get';
const IP_LOCATION_API_TIMEOUT = 2000;
const CHINA_REGION_CODES = {
: 'BJ',
: 'TJ',
: 'HE',
西: 'SX',
: 'NM',
: 'LN',
: 'JL',
: 'HL',
: 'SH',
: 'JS',
: 'ZJ',
: 'AH',
: 'FJ',
西: 'JX',
: 'SD',
: 'HA',
: 'HB',
: 'HN',
广: 'GD',
广西: 'GX',
: 'HI',
: 'CQ',
: 'SC',
: 'GZ',
: 'YN',
西: 'XZ',
西: 'SN',
: 'GS',
: 'QH',
: 'NX',
: 'XJ',
: 'TW',
: 'HK',
: 'MO',
};
const PROVIDER_HEADERS = [
// Umami custom headers (cloud mode only)
...(process.env.CLOUD_MODE
@@ -123,13 +87,6 @@ async function isLocalIp(ip: string) {
}
}
function getChinaRegionCode(province: string) {
const name = province?.trim();
const region = Object.entries(CHINA_REGION_CODES).find(([key]) => name?.startsWith(key))?.[1];
return region ? getRegionCode('CN', region) : undefined;
}
async function getOnlineLocation(ip: string) {
try {
const url = new URL(process.env.IP_LOCATION_API_URL || IP_LOCATION_API_URL);
+11
View File
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest';
import { getChinaRegionCode, getRegionNames } from './geo';
test('getChinaRegionCode: converts Chinese province names to ISO codes', () => {
expect(getChinaRegionCode('福建省')).toBe('CN-FJ');
});
test('getRegionNames: localizes Chinese regions for zh-CN', () => {
expect(getRegionNames('zh-CN')['CN-FJ']).toBe('福建');
expect(getRegionNames('en-US')['CN-FJ']).toBe('Fujian');
});
+57
View File
@@ -0,0 +1,57 @@
import regions from '../../public/iso-3166-2.json';
export const CHINA_REGION_CODES = {
: 'BJ',
: 'TJ',
: 'HE',
西: 'SX',
: 'NM',
: 'LN',
: 'JL',
: 'HL',
: 'SH',
: 'JS',
: 'ZJ',
: 'AH',
: 'FJ',
西: 'JX',
: 'SD',
: 'HA',
: 'HB',
: 'HN',
广: 'GD',
广西: 'GX',
: 'HI',
: 'CQ',
: 'SC',
: 'GZ',
: 'YN',
西: 'XZ',
西: 'SN',
: 'GS',
: 'QH',
: 'NX',
: 'XJ',
: 'TW',
: 'HK',
: 'MO',
};
const CHINA_REGION_NAMES = Object.fromEntries(
Object.entries(CHINA_REGION_CODES).map(([name, code]) => [`CN-${code}`, name]),
);
export function getChinaRegionCode(province: string) {
const name = province?.trim();
const region = Object.entries(CHINA_REGION_CODES).find(([key]) => name?.startsWith(key))?.[1];
return region ? `CN-${region}` : undefined;
}
export function getRegionNames(locale?: string): Record<string, string> {
if (locale?.toLowerCase() === 'zh-cn') {
return { ...regions, ...CHINA_REGION_NAMES };
}
return regions;
}