一个常见的国际问题是字符串中表示的双值的转换.这个东西在很多地方都有发现.
从调用的csv文件开始
comma separated
Run Code Online (Sandbox Code Playgroud)
要么
character separated
Run Code Online (Sandbox Code Playgroud)
因为有时它们会被存储起来
1.2,3.4
5.6,6.4
Run Code Online (Sandbox Code Playgroud)
在英语地区或
1,2;3,4
5,6;6,4
Run Code Online (Sandbox Code Playgroud)
例如德国地区.
从这个背景来看,有必要知道大多数std ::方法都依赖于语言环境.因此在德国,他们会将"1,2"读作1.2并将其写回"1,2",但使用英语操作系统时,它将"1,2"读为1并将其写回为"1".
由于语言环境是应用程序的全局状态,因此将其切换到其他设置并不是一个好主意.当我必须在英语机器上读取德语CSV文件或反之亦然时,我们遇到了一些问题.
在所有机器上编写行为相同的代码也很困难.C++流允许每个流的区域设置.
class Punctation : public numpunct<wchar_t>
{
public:
typedef wchar_t char_type;
typedef std::wstring string_type;
explicit Punctation(const wchar_t& decimalPoint, std::size_t r = 0) :
decimalPoint_(decimalPoint), numpunct<wchar_t>(r)
{
}
Punctation(const Punctation& rhs) :
decimalPoint_(rhs.decimalPoint_)
{
}
protected:
virtual ~Punctation()
{
};
virtual wchar_t do_decimal_point() const
{
return decimalPoint_;
}
private:
Punctation& operator=(const Punctation& rhs);
const wchar_t decimalPoint_;
};
...
std::locale newloc(std::locale::classic(), new Punctation(L','));
stream.imbue(newloc);
Run Code Online (Sandbox Code Playgroud)
将允许您使用std …
显然PostgreSQL 8.4和Ubuntu 10.04无法处理为瑞典字母排序W和V的更新方式.也就是说,它仍然将它们命名为相同的字母(瑞典订购的旧定义):
它应该是(瑞典订购的新定义):
我需要为我正在构建的Python/Django网站正确订购.我已经尝试了各种方法来使用*values_list*来命令从Django QuerySet创建的元组列表.但由于它是瑞典语,ä,ä和ö字母需要正确订购.现在我有一种或另一种方式都不是两种方式..
list_of_tuples = [(u'Wa', 1), (u'Vb',2), (u'Wc',3), (u'Vd',4), (u'Öa',5), (u'äa',6), (u'Åa',7)]
print '########## Ordering One ##############'
ordered_list_one = sorted(list_of_tuples, key=lambda t: tuple(t[0].lower()))
for item in ordered_list_one:
print item[0]
print '########## Ordering Two ##############'
locale.setlocale(locale.LC_ALL, "sv_SE.utf8")
list_of_names = [u'Wa', u'Vb', u'Wc', u'Vd', u'Öa', u'äa', u'Åa']
ordered_list_two = sorted(list_of_names, cmp=locale.strcoll)
for item in ordered_list_two:
print item
Run Code Online (Sandbox Code Playgroud)
这些例子给出了:
########## Ordering One ##############
Vb
Vd
Wa
Wc
äa
Åa
Öa
########## …
Run Code Online (Sandbox Code Playgroud) 我正在将a转换nullable integer
为a string
,并且Resharper警告我使用InvariantCulture.
shipment.ShipmentID.ToString()
Run Code Online (Sandbox Code Playgroud)
快速Alt-Enter,稍后输入,给我这个:
shipment.ShipmentID.ToString(CultureInfo.InvariantCulture)
Run Code Online (Sandbox Code Playgroud)
不幸的是,Resharper并不满意,并提出同样的建议,这对我来说没有意义.
shipment.ShipmentID.ToString(CultureInfo.InvariantCulture,
CultureInfo.InvariantCulture)
Run Code Online (Sandbox Code Playgroud)
nullable int上的ToString()将不会构建,给出一个错误,指出No overload方法'ToString'需要1个参数.
不可为空的整数表现不同.
int requiredInt = 3;
// no Resharper or compiler warning
var stringFromRequiredInt = requiredInt.ToString(CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
如何将a转换为nullable int
对string
不同语言环境中的计算机安全的应该怎么做?
我正在尝试为我的项目激活不同的语言.现在英语和西班牙语.
我将描述我遵循的所有步骤:
首先,我把自己放在我要翻译的目录中.或者更好地说,所有{%trans%}标签都是:
$ cd media/templates/landing/
$ mkdir locale
$ django-admin.py makemessages --locale=en
Run Code Online (Sandbox Code Playgroud)
上一个命令创建目录/ file /locale/en/LC_MESSAGES/django.po
我打开django.po,然后继续用英语填写所有msgstr字段.msgstr标签是西班牙语.我尊重有关长消息案例的提示.填写此文件后,我做:
$ django-admin.py compilemessages
Run Code Online (Sandbox Code Playgroud)
这个过程django.po并创建django.mo.
所以我修改了settings.py.重要内容:
TEMPLATE_CONTEXT_PROCESSORS = (
'ism.context_processor.user_vars',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'pipeline.middleware.MinifyHTMLMiddleware',
}
TIME_ZONE = 'Atlantic/Canary'
LANGUAGE_CODE = 'es'
USE_I18N = True
_ = lambda s: s
LANGUAGES = (
('es', _('Espanol')),
('en', _('English')),
)
USE_L10N = True
USE_TZ = True
Run Code Online (Sandbox Code Playgroud)
最后,我将此行添加到URLS.py:
(r'^i18n/', include('django.conf.urls.i18n')),
Run Code Online (Sandbox Code Playgroud)
我重新启动我的开发服务器,我配置我的Firefox浏览器首先选择英语作为主要语言,它不起作用.所有文本仍以西班牙文显示,而不是英文.
我确保Firefox配置为英文,因为在Django视图函数(呈现.html)中我使用request.LANGUAGE_CODE进行打印,打印"en".
我弄错了什么?
所以在我的应用程序中,我试图让设备目前设置语言及其首字母缩略词.所以我这样做:
NSString *fullLanguage = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[[NSLocale preferredLanguages] objectAtIndex:0]];
NSString *abrlanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
但是有些用户报告说语言正在返回类似:en_UK或类似的内容,这反过来又搞乱了我的应用程序的功能.
无论如何,有没有办法获得设备的当前设置语言,无论设备区域设置如何?
谢谢!
我需要从Java日期中仅提取月份和年份信息以显示在GUI中.必须使用用户区域设置翻译此日期.我知道两种格式化本地化日期的方法:
DateFormat.getInstance(DateFormat.LONG, locale).format(date)
new SimpleDateFormat("yyyy M", locale).format(date)
对于区域设置SIMPLIFIED_CHINESE
和日期2013-02-18
,这给出:
我们需要使用LONG
没有日期的格式(2013年2月).[变体2显然是不可接受的,因为年月的排序不受区域设置的影响].问题可能是:如何从长格式中删除日期部分(18日)?我尝试使用该方法format(Date date, StringBuffer toAppendTo, FieldPosition pos)
,但这只允许提取数字部分,而不是额外的字符.
模式DateFormat.getInstance(DateFormat.LONG, Locale.SIMPLIFIED_CHINESE)
是yyyy'?'M'?'d'?'
.提取日期部分会产生yyyy'?'M'?'
.在我看来,不可能使用Java的DateFormat标准来实现这一点,因为额外的字符(?
,?
和?
)似乎没有映射到相应的字段,而只是格式化器没有其他语义的字符.
我一看DateFormatSymbols
和LocaleServiceProvider
,但认为这并不能帮助.对我来说,扩展点将是为getInstance(int style)
方法添加另一种日期样式.但是如果没有为所有Locale实现它,它似乎是不可能的......
这个分析是否正确?如何以干净的方式实现这种格式化?
我想打印所有可用语言的分类波兰语名称.
import java.util.*;
public class Tmp
{
public static void main(String... args)
{
Locale.setDefault(new Locale("pl","PL"));
Locale[] locales = Locale.getAvailableLocales();
ArrayList<String> langs = new ArrayList<String>();
for(Locale loc: locales) {
String lng = loc.getDisplayLanguage();
if(!lng.trim().equals("") && ! langs.contains(lng)){
langs.add(lng);
}
}
Collections.sort(langs);
for(String str: langs){
System.out.println(str);
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我对排序部分有疑问.输出是:
:
:
katalo?ski
korea?ski
litewski
macedo?ski
:
:
w?gierski
w?oski
?otewski
Run Code Online (Sandbox Code Playgroud)
不幸的是在波兰?
之后来到l
之前m
所以输出应该是:
:
:
katalo?ski
korea?ski
litewski
?otewski
macedo?ski
:
:
w?gierski
w?oski
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?是否存在一种通用的非语言相关方法(比如说我现在想要显示它并使用另一种排序规则在另一种语言中排序).
我正在使用Microsoft Visual Studio 2012,并且正在查看使用std::put_time
,因此我创建了以下示例:
int main()
{
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::locale::global( std::locale("en-GB") );
std::cout << std::put_time( std::localtime( &t ), "%x" ) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
06/25/2013
Run Code Online (Sandbox Code Playgroud)
这不是我期望从en-GB
语言环境中获得的日期格式.我也尝试过:
std::cout.imbue( std::locale("en-GB") );
Run Code Online (Sandbox Code Playgroud)
但同样,输出相同.这是我应该为这个语言环境获取的输出,还是我在某个地方犯了错误?
这是我的代码:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
SimpleDateFormat date = new SimpleDateFormat("dd-MMM-yyyy", new Locale("es","ar"));
System.out.println(date.format(new Date(2014-1900,0,1)));
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码返回,
01-ene-2014
Run Code Online (Sandbox Code Playgroud)
但是,月份应该是句子,即Ene
任何人都可以帮助我如何01-Ene-2014
不使用子串?
所以我一直在尝试设置Python语言环境.我想,因为我想以当地语言(使用strftime('%A')
)使用工作日名称.但是目前工作日是用英语打印的,尽管我尝试过多种方式设置语言环境.我在带有Ubuntu Mate的RPi2上运行它.
我已经尝试过本主题中的解决方案在Windows上设置Python的语言环境的正确方法是什么?
我尝试将locale明确设置为nl_NL.utf8
:
$> locale -a
$> python3
>>> import locale
>>> locale.setlocale(locale.LC_ALL,'nl_NL.utf-8')
Run Code Online (Sandbox Code Playgroud)
我也尝试设置一个空字符串,在最后一个答案中建议:
>>> locale.setlocale(locale.LC_ALL, '')
Run Code Online (Sandbox Code Playgroud)
在我尝试的两种情况下
>>> locale.getlocale()
Run Code Online (Sandbox Code Playgroud)
我知道了 nl_NL.utf8
但我仍然用英语获得工作日!除了上面的主题外,还没有在internetz上找到太多这方面的内容,我无法解决这个问题.
编辑:
我尝试了所有3个选项.第一个返回一个奇怪的结果:
~/Documenten$ python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50)
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale(locale.LC_TIME)
(None, None)
>>> locale.setlocale(locale.LC_TIME, 'nl_NL.utf-8')
'nl_NL.utf-8'
>>> locale.getlocale(locale.LC_TIME)
('nl_NL', 'UTF-8')
>>> exit()
jeffrey@jeffrey-desktop:~/Documenten$ python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50) …
Run Code Online (Sandbox Code Playgroud)