从网络检测区域设置(列表分隔符) [英] Detecting regional settings (List Separator) from web

查看:59
本文介绍了从网络检测区域设置(列表分隔符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对逗号分隔值 (CSV) 文件不一定以逗号分隔这一令人不快的惊讶之后,我试图找出是否有任何方法可以检测客户端计算机上的区域设置列表分隔符值是什么http 请求.

After having the unpleasant surprise that Comma Seperated Value (CSV) files are not necessarily comma-separated, I'm trying to find out if there is any way to detect what the regional settings list separator value is on the client machine from http request.

场景如下:用户可以从网站下载一些 CSV 格式的数据(RoR,如果重要的话).该 CSV 文件是即时生成的,发送给用户,大部分时间双击并在目标 Windows 机器上的 MS Excel 中打开.现在,如果用户将 ',' 设置为列表分隔符,则数据按列正确排列,但如果设置了任何其他分隔符(此处广泛使用的 ';'),则所有内容都只会被扔到单个列中.那么,有没有办法检测客户端机器上使用的分隔符,并相应地生成文件?

Scenario is as follows: A user can download some data in CSV format from web site (RoR, if it matters). That CSV file is generated on the fly, sent to the user, and most of the time double-clicked and opened in MS Excel on Windows machine at the destination. Now, if the user has ',' set as the list separator, the data is properly arranged in columns, but if any other separator (';' is widely used here) is set, it all just gets thrown into a single column. So, is there any way to detect what separator is used on the client machine, and generate the file accordingly?

我有一种不真实的感觉,但我想在向客户传达无法完成,抱歉"之前确定一下:)

I have a sinking feeling that it is not, but I'd like to be sure before I pass the 'can't be done, sorry' line to the customer :)

推荐答案

这是我刚刚根据所示方法编写的 JavaScript 解决方案 此处:

Here's a JavaScript solution that I just wrote based on the method shown here:

function getListSeparator() {
    var list = ['a', 'b'], str;
    if (list.toLocaleString) {
        str = list.toLocaleString();
        if (str.indexOf(';') > 0 && str.indexOf(',') == -1) {
            return ';';
        }
    }
    return ',';
}

关键在于使用系统列表分隔符的 toLocaleString() 方法.

The key is in the toLocaleString() method that uses the system list separator.

您可以使用 JavaScript 获取列表分隔符并将其设置在一个 cookie 中,然后您可以从您的服务器检测到它.

You could use JavaScript to get the list separator and set it in a cookie which you could then detect from your server.

我检查了所有 Windows 语言环境,似乎默认列表分隔符几乎总是,"或;".对于某些语言环境,控制面板中的下拉列表提供了两个选项;对于其他人,它只提供,".一种语言环境 Divehi 有一个我以前从未见过的奇怪字符作为列表分隔符,而且对于任何语言环境,用户都可以输入他们想要的任何字符串作为列表分隔符.

I checked all the Windows Locales, and it seems that the default list separator is virtually always either ',' or ';'. For some locales the drop-down list in the Control Panel offers both options; for others it offers just ','. One locale, Dive has a strange character that I've not seen before as the list separator, and, for any locale, it is possible for the user to enter any string they want as the list separator.

将随机字符串作为 CSV 文件中的分隔符对我来说听起来很麻烦,所以我上面的函数只会返回一个 ';'或 '.',它只会返回一个 ';'如果在 Array.toLocaleString 字符串中找不到,".我不完全确定 array.toLocaleString 是否具有跨浏览器保证的格式,因此 indexOf 会检查而不是在特定索引处挑选字符.

Putting random strings as the separator in a CSV file sounds like trouble to me, so my function above will only return either a ';' or a '.', and it will only return a ';' if it can't find a ',' in the Array.toLocaleString string. I'm not entirely sure about whether array.toLocaleString has a format that's guaranteed across browsers, hence the indexOf checks rather than picking out a character at a specific index.

使用 Array.toLocaleString 获取列表分隔符适用于 IE6、IE7 和 IE8,但不幸的是它似乎不适用于 Firefox、Safari、Opera 和 Chrome(或至少我的浏览器上的这些浏览器的版本)计算机):它们似乎都用逗号分隔数组项,而不管 Windows 的列表分隔符"设置如何.

Using Array.toLocaleString to get the list separator works on IE6, IE7, and IE8, but unfortunately it doesn't seem to work on Firefox, Safari, Opera, and Chrome (or at least the versions of those browsers on my computer): they all seem to separate array items with a comma, irrespective of the Windows "list separator" setting.

另外值得注意的是,默认情况下,Excel 在解析 CSV 文件中的数字时似乎使用系统小数分隔符".哎呀.因此,如果您要本地化列表分隔符,您可能还想本地化小数点分隔符.

Also worth noting that by default Excel seems to use the system "decimal separator" when it's parsing numbers out of CSV files. Yuk. So, if you're localizing the list separator you might want to localize the decimal separator too.

这篇关于从网络检测区域设置(列表分隔符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆