标签: nslocale

使用特定语言环境(en_US 或无)运行单元测试

有没有办法在运行单元测试时强制使用特定的语言环境?

例如。始终使用 en_US 或强制无语言环境,以便不加载任何 .lproj 文件。

我的单元测试对设置应用程序(在 iOS 模拟器中)中当前选择的语言很敏感。最好我希望我的单元测试对语言不敏感。

下面是我的单元测试代码,显示了问题

@interface MYGalleryTests : SenTestCase
@end
@implementation MYGalleryTests
-(void)test0 {
    // This test doesn't work. 
    // I get 'No pictures' when locale is en_US
    // I get 'Ingen billeder' when locale is da_DK
    NSString* actual = [MYGallery emptyMessage];
    NSString* expected = @"EMPTY_MESSAGE"; 
    STAssertEqualObjects(actual, expected, nil);
}
@end


@interface MYGallery : NSObject
+(NSString*)emptyMessage;
@end
@implementation MYGallery
+(NSString*)emptyMessage {
    return NSLocalizedStringFromTable(@"EMPTY_MESSAGE", @"Gallery", @"Message shown when gallery is empty");
}
@end


en.lproj/Gallery.strings
/* Message …
Run Code Online (Sandbox Code Playgroud)

cocoa nslocalizedstring ios nslocale sentestingkit

6
推荐指数
2
解决办法
5874
查看次数

iPad上的NSLocaleUsesMetricSystem始终为YES

我试图确定用户Region/Locale设置是否验证Metric值的使用(在这种情况下,如果应该追加kglb).

我在iPad上和模拟器上运行3.2(Xcode 3.2.4).我已经尝试了一些不同的区域设置,但我根本无法使其恢复NONSLocaleUsesMetricSystem

NSLocale *locale = [NSLocale systemLocale]; 
BOOL usesMetric = [[locale objectForKey:NSLocaleUsesMetricSystem] boolValue];

NSLog(@"The system is Metric: %@\n", (usesMetric ? @"YES" : @"NO"));
Run Code Online (Sandbox Code Playgroud)

甚至在模拟器和实际设备上设置为英语的语言和设置为美国的区域也会发生这种情况.

我也试过NSLocaleMeasurementSystem,它总是返回"公制",从不"美国".

我将如何决定是否应该使用公制?

谢谢你给予的任何帮助:)

...我可以这么幸运,在我睡觉的时候整个世界变成了公制:)

metric-system ipad ios nslocale

5
推荐指数
1
解决办法
2396
查看次数

iPhone使用用户区域设置显示日期,但使用其他语言

是否有任何解决方案使用他的语言环境设置向用户显示日期,但是单词是否使用不同的语言?

我想要的是显示给用户设置en_US德语日期(例如月份名称,工作日).

iphone nsdateformatter nslocale

5
推荐指数
1
解决办法
3804
查看次数

如何从ISOCurrencyCodes获取NSNumberFormatter货币样式?

如果我要使用EUR或USD(两种ISO货币代码),我怎么能让我的NSNumberFormatter正确格式化我这样的字符串?

对于EUR代码:10.000,00€对于美元代码:$ 10,000.00

我可以通过设置localeIdentifier来做到这一点.但我真的需要从ISO货币代码中获取它.这可能吗?

[numberFormatter stringFromNumber: [_property price]];
Run Code Online (Sandbox Code Playgroud)

谢谢!

objective-c nsnumberformatter ios nslocale

5
推荐指数
1
解决办法
7844
查看次数

Swift - 获取国家/地区列表

如何在Swift中获取包含所有国家/地区名称的数组?我试图转换Objective-C中的代码,这是:

if (!pickerCountriesIsShown) {
    NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];

    for (NSString *countryCode in [NSLocale ISOCountryCodes])
    {
        NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
        NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
        [countries addObject: country];
    }
Run Code Online (Sandbox Code Playgroud)

在斯威夫特我无法从这里传递:

        if (!countriesPickerShown) {
        var countries: NSMutableArray = NSMutableArray()
        countries = NSMutableArray.arrayWithCapacity((NSLocale.ISOCountryCodes).count) // Here gives the Error. It marks NSLocale.ISOCountryCodes and .count
Run Code Online (Sandbox Code Playgroud)

你们有谁知道这个吗?

谢谢

arrays countries ios nslocale swift

5
推荐指数
2
解决办法
9656
查看次数

iOS:复数和自定义语言环境

我正在开发支持“en”和“ru”语言的应用程序,用户可以在应用程序内选择语言。

如果默认手机的语言环境设置为“en”,但在应用程序中选择了“ru”语言,则在尝试本地化复数句时会忽略“many”/“few”形式。所以它被英语复数规则本地化。

定义:

<key>%d files</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@files@</string>
        <key>files</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>one</key>
            <string>???? ????</string>
            <key>many</key>
            <string>%d ??????</string>
            <key>other</key>
            <string>%d ?????</string>
        </dict>
    </dict>
Run Code Online (Sandbox Code Playgroud)

本地化代码(手动创建“ru”语言环境):

let locale = NSLocale(localeIdentifier: "ru_RU")
String(format: NSLocalizedString("%d files", comment: ""),
                    locale: locale,
                    count)
Run Code Online (Sandbox Code Playgroud)

作为输出,我得到:
for count = 1: “???? ????” - 它
适合 count = 2:“2 ?????” -
count = 6也是正确的(来自“其他”类别):“6 ?????” - 错误,也来自“其他”类别,但应来自“许多”

如果我将手机的语言切换为俄语,则所有语言都已正确本地化。

iphone localization ios nslocale ios7

5
推荐指数
1
解决办法
335
查看次数

大写所有字符串根据Locale - Swift

我试图根据'Locale'大写所有字符串,但它不起作用.

var text = "istanbul, izmir"

println(text.uppercaseStringWithLocale(NSLocale.currentLocale())) 
Run Code Online (Sandbox Code Playgroud)

在土耳其语中,我的大写是不是我.我的结果是伊斯坦布尔,IZMIR但它应该返回İSTANBUL,İZMİR.

请问我的问题在哪里?

ios nslocale swift

5
推荐指数
2
解决办法
3367
查看次数

确定当前区域设置是否在iOS中使用非阿拉伯数字?

在iOS中,如果用户[NSLocale currentLocale]不使用阿拉伯数字(即不是0123456789),我想执行一些特定的功能.我该如何检查这种情况?

cocoa-touch localization nslocalizedstring ios nslocale

4
推荐指数
1
解决办法
521
查看次数

原生iOS语言翻译字符串及其语言代码识别(LCID)

iOS如何在翻译字符串或语言的区域设置中显示语言列表.

  • 是否有与之相关的LCID?
  • 如果是这样,我在哪里可以找到它的映射?
  • 它是如何工作的?
  • 是否有API或任何文档可以指向我?

我附上了我实际意思的截图: 在此输入图像描述

我能找到的就是这个链接

更新:我想实现这样的目标:

  • 我可以在哪里映射国家的语言,实际上是在其语言环境中.那么有一个LCID类型的东西,我可以映射它并使用iOS API使用LCID获取该Locale字符串?

  • 样机在下面
    在此输入图像描述

xcode localization ios nslocale

4
推荐指数
1
解决办法
1862
查看次数

NSLocale在iOS 11中返回错误的值

从iOS 11.0开始,以下代码返回"de_US"而不是"en_US":

// => Returns "de_US"
NSString *regionCode = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
NSLog(@"Region code: %@", regionCode);
Run Code Online (Sandbox Code Playgroud)

在iOS 11下面,它返回"en_US".

我的设备有语言和地区英语/美国.首选语言(尽管我不在我的代码中使用它们)是有序的:

  1. 英语
  2. 德语
  3. 法国

这是iOS 11的已知问题吗?API有变化吗?

localization ios nslocale ios11

4
推荐指数
1
解决办法
1509
查看次数