
Research
/Security News
11 Malicious Go Packages Distribute Obfuscated Remote Payloads
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
browser-language-auto-detector
Advanced tools
🌍 Intelligent browser language detection library with 100+ languages support, RFC 5646 compliant, LRU cache optimized, TypeScript ready
🌍 智能浏览器语言检测库,自动识别用户语言偏好并转换为标准语言信息
English | 简体中文
# 使用 npm
npm install browser-language-auto-detector
# 使用 yarn
yarn add browser-language-auto-detector
# 使用 pnpm
pnpm add browser-language-auto-detector
import { getLanguageName } from 'browser-language-auto-detector'
// 自动检测当前浏览器语言
const languageInfo = getLanguageName()
console.log(languageInfo)
// 输出示例
{
chinese: '简体中文',
origin: '简体中文',
english: 'Chinese Simplified',
rtl: false,
key: 'chinese'
}
import {
getLanguageNameOptimized,
getBrowserLocalOrigin,
translateOriginLanguage,
} from 'browser-language-auto-detector'
// 使用优化版本(推荐)
const result = getLanguageNameOptimized({
useCache: true, // 启用缓存
standardize: true, // 标准化语言代码
maxFallbacks: 5, // 最大回退数量
})
// 获取浏览器原始语言代码
const languages = getBrowserLocalOrigin()
// ['zh-Hans-CN', 'zh-CN', 'zh', 'en-US', 'en']
// 获取标准化语言代码
const standardizedLanguages = getBrowserLocalOrigin(true)
// ['zh', 'en']
// 手动翻译语言代码
const languageKey = translateOriginLanguage(['zh-Hans-CN', 'en-US'])
// 'chinese'
我们对算法进行了全面优化,性能测试结果:
🚀 性能基准测试
translateOriginLanguage (优化版):
总时间: 2.40ms
平均时间: 0.0002ms
操作/秒: 4,172,099
getLanguageName (原版):
总时间: 2.40ms
平均时间: 0.0002ms
操作/秒: 4,167,247
getLanguageNameOptimized (缓存):
总时间: 88.94ms
平均时间: 0.0089ms
操作/秒: 112,438
库支持 100+ 种语言,包括但不限于:
每种语言包含:
chinese
: 中文名称origin
: 原生语言名称english
: 英文名称rtl
: 是否为 RTL(从右到左)语言key
: 语言标识符# 安装依赖
pnpm install
# 开发模式
pnpm dev
# 构建
pnpm build
# 测试
pnpm test
# 代码检查
pnpm lint
# 格式化
pnpm format
# 性能基准测试
node benchmark/performance.js
getLanguageName(): LanguageName | null
检测当前浏览器语言并返回语言信息对象。
getLanguageNameOptimized(options?: {
useCache?: boolean // 是否使用缓存,默认 true
standardize?: boolean // 是否标准化语言代码,默认 true
maxFallbacks?: number // 最大回退数量,默认 10
}): LanguageName | null
优化版本的语言检测函数,支持性能调优配置。
getBrowserLocalOrigin(standardize?: boolean): string[]
获取浏览器原始语言代码数组。
translateOriginLanguage(names: string[]): string | null
将语言代码数组转换为内部语言键。
import { getLanguageName } from 'browser-language-auto-detector'
const userLanguage = getLanguageName()
if (userLanguage) {
// 设置应用语言
i18n.changeLanguage(userLanguage.key)
// 设置 RTL 支持
document.dir = userLanguage.rtl ? 'rtl' : 'ltr'
}
import { getLanguageNameOptimized } from 'browser-language-auto-detector'
const language = getLanguageNameOptimized({
useCache: true,
standardize: true,
})
if (language) {
// 显示本地化内容
displayWelcomeMessage(language.chinese)
}
import { getBrowserLocalOrigin } from 'browser-language-auto-detector'
const userLanguages = getBrowserLocalOrigin()
// 发送用户语言偏好数据到分析服务
analytics.track('user_language_preference', {
primary: userLanguages[0],
fallbacks: userLanguages.slice(1),
})
// 高频调用场景使用缓存
const result = getLanguageNameOptimized({
useCache: true,
maxFallbacks: 3, // 限制回退数量提升性能
})
// 一次性调用场景关闭缓存
const freshResult = getLanguageNameOptimized({
useCache: false,
})
// 获取标准化语言代码(推荐)
const standardized = getBrowserLocalOrigin(true)
// ['zh', 'en']
// 获取原始语言代码
const original = getBrowserLocalOrigin(false)
// ['zh-Hans-CN', 'zh-CN', 'zh', 'en-US', 'en']
getLanguageNameOptimized
函数查看完整更新日志:CHANGELOG.md
欢迎贡献代码、报告问题或提出建议!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)本项目采用 MIT 许可证。详情请查看 LICENSE 文件。
如果您发现任何问题或有改进建议,请在 GitHub Issues 中提出。
Made with ❤️ by Itinysun
FAQs
🌍 Intelligent browser language detection library with 100+ languages support, RFC 5646 compliant, LRU cache optimized, TypeScript ready
We found that browser-language-auto-detector demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).