我有运行XCode版本4.3.3和iOS 5.1.我可以做我想做的事.我无法让本地化运行.我通过项目文件添加了不同的语言.我在每种语言中调整了InfoPlist.strings,如下所示:
"key" = "value";
Run Code Online (Sandbox Code Playgroud)
我到处都有:
NSLocalizedString(@"key", nil);
Run Code Online (Sandbox Code Playgroud)
没有关键像它的价值一样.我检查了选择的语言:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
我尝试删除和清理所有内容,如下所述: 本地化无法正常工作
我试着复制这里描述的文件: NSLocalizedString问题
并且所有文件都在他们的语言文件夹中: iPhone:NSLocalizedString读取英文.strings文件,即使设置了另一个lang
有没有人知道我还能做什么?!?我不知道我做错了什么?!
在检查了一些主题之后,我仍然无法弄清楚:使用静态NSLocalizedString的最佳方法是什么,即静态分配NSLocalizedString并轻松访问它.
理想情况下,我想做类似的事情(不会编译,但有Initializer element is not a compile-time constant错误):
//Somewhere in my header
static NSString* mystring = NSLocalizedString(@"min", nil); //Error : "Initializer element is not a compile-time constant"
@implementation myClass
(NSString*)aMethod
{
return myString;
}
@end
Run Code Online (Sandbox Code Playgroud)
我知道NSLocalizedString是一个定义的宏#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil],
但是没有多大帮助:S.
长话短说,以防止文档的多个部分中的相同字符串的多重定义(这将阻止我的整个应用程序中的一笔编辑).
考虑一个示例,其中定义的冗余是明确的:
//MyDelegate.h
@property IBoutlet NSTextField* myTextField;
//MyDelegate.m
@implementation MyDelegate.m
@synthetize myTextField;
-(void)setTextFieldToDefaultValue
{
[myTextField setStringValue:NSLocalizedString(@"Name",@"This field is used to write one's name");
}
-(BOOL)isTextFieldStringDefault:(NSString*)myString
{
return [[myTextField stringValue] isEqual:NSLocalizedString(@"Name",@"This …Run Code Online (Sandbox Code Playgroud) 我试图在我的localization.strings文件中放入一个Unicode字符。当我手动将标签的文本设置为“ \ u {F071}”时,字符正确显示。当我将该字符作为本地化文件中字符串的一部分时,则unicode字符不会出现我想要的样子。我可能会缺少一些编码设置吗?还是在本地化文件中不应该包含Unicode字符?
我的项目基础语言是英语。我的模拟器/手机是英文的。这些项目支持英语、法语、荷兰语。
当应用程序第一次启动时,即使手机是英文的,应用程序也始终是法语。
当我调试并打印出这一行时:
[NSLocale preferredLanguages][0];
Run Code Online (Sandbox Code Playgroud)
它给了我正确的语言,即英语。
有人知道这是为什么吗?
我正在尝试添加带有参数的 SwiftUI 本地化文本。然而,在整个应用程序中,我们使用本地化键的包装器,所以我们有这样的东西:
static let helloWorldText = NSLocalizedString("helloWorldText", comment: "")
Run Code Online (Sandbox Code Playgroud)
随着使用
label.text = LocalizationWrapper.helloWorldText
Run Code Online (Sandbox Code Playgroud)
或者
Text(LocalizationWrapper.helloWorldText)
Run Code Online (Sandbox Code Playgroud)
而且效果很好。
然后,当涉及到添加带有参数的本地化文本时,它似乎不起作用。
所以我有一把钥匙"helloWorld %@"
我有一个static let helloWorldWithParameter = NSLocalizedString("helloWorld %@", comment: "")
现在我尝试这样做:
Text("\(LocalizationWrapper.helloWorldWithParameter) \(name)")
Text(LocalizedStringKey(LocalizationWrapper.helloWorldWithParameter + " " + name))
Text(LocalizationWrapper.helloWorldWithParameter + " " + name)
Run Code Online (Sandbox Code Playgroud)
它们都不起作用,但Text("helloWorld \(name)")效果很好。
然后我尝试删除 NSLocalizedString 只留下LocalizationWrapper.helloWorldWithParameter一个干净的字符串,但它也没有做任何事情
我怎样才能做到这一点?我看过这个,但有点脏。
我有一个字符串,假设“我的名字是 %@,我在 %@ 班学习”,现在我想将要插入的占位符文本加粗,这样结果将如下所示:“我的名字很严厉,并且我在10班学习”,我会将其显示在标签上
我已经尝试过使用 NSAttributedString 但由于字符串将被本地化,我无法使用属性字符串的范围参数将其设置为粗体。
我无法理解并找到任何向我展示NSLocalizedString实际用途的资源.即使是Apple文档..
NSLocalizedString(<#key: String#>, tableName: <#String?#>,
bundle: <#NSBundle#>, value: <#String#>, comment: <#String#>)
Run Code Online (Sandbox Code Playgroud)
好吧,假设我在一个文件中创建了这个,我该如何为不同的语言编写这个?
我是否创建了整个文件的本地化版本,如:en.thisfile.swift,es.thisfile.swift,fr.thisfile.swift?
或者我在不同的包或表中创建新语言?
NSLocalizedString("hello", tableName: "en", value: "hello", comment: "wtf is this anyways?")
NSLocalizedString("hello", tableName: "fr", value: "bonjour", comment: "wtf is this anyways?")
NSLocalizedString("hello", tableName: "es", value: "hola", comment: "wtf is this anyways?")
Run Code Online (Sandbox Code Playgroud)
然后如何在设置后在随机文件中使用它?
println("dat nslocalizedstring I created, which will automatically know what to write??")
Run Code Online (Sandbox Code Playgroud) 我如何翻译一个包含变量的字符串,如下所示:
let alert = UIAlertController(title: NSLocalizedString("NEUEARTIKEL",comment:"Bitte gib einen neuen Artikel für \(titelArr[sender.tag]) an:"), message: nil, preferredStyle: .Alert)
Run Code Online (Sandbox Code Playgroud)
当我在localizable.string中正常翻译String时:
NEUEARTIKEL="Add an item to \(titelArr[sender.tag]):";
Run Code Online (Sandbox Code Playgroud)
警报将显示(titelArr [sender.tag])而不是其值.
这可能很简单,但我是一个新手快速的一个是不能谷歌的东西有用!;-)
谢谢你的帮助// Seb
我正在使用Notification Serivce Extension根据我的需要更改来自服务器的文本.
但是,这对普通字符串很有用.但是当我使用NSLocalizedString时,它会显示本地化的字符串,而是显示字符串的字段名称.
例如:在UNNotificationServiceExtension内:
let CompanyViewLM = NSLocalizedString("Notifications.View", comment: "No Comment")
Run Code Online (Sandbox Code Playgroud)
在Localizable.strings中我输入以下行:
Notifications.View = "I Viewed your profile";
Run Code Online (Sandbox Code Playgroud)
注意到我在普通的视图控制器中放了相同的代码行,它工作得很好.
我知道有简体中文(zh-Hans)和繁体中文(zh-Hant)。
但是 Xcode 允许本地化中文 ( zh)。
从本地化回退的概念我理解它是简体中文和繁体中文的回退。因此,它在技术上可用于托管简体中文和繁体中文相同的常见中文本地化字符串。
什么是 Xcode 语言环境zh?应该zh是简体中文还是繁体中文?
localization localizable.strings nslocalizedstring ios chinese-locale