cc-pets 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/CHANGELOG.zh-CN.md +17 -0
- package/CONTRIBUTING.md +54 -0
- package/CONTRIBUTING.zh-CN.md +45 -0
- package/README.md +31 -26
- package/README.zh-CN.md +22 -16
- package/Resources/Info.plist +3 -1
- package/Resources/en.lproj/InfoPlist.strings +1 -0
- package/Resources/phrases.default.en.txt +182 -0
- package/Resources/zh-Hans.lproj/InfoPlist.strings +1 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +332 -0
- package/Sources/CCPets/CCPetsAppDelegate+AgentStatus.m +28 -28
- package/Sources/CCPets/CCPetsAppDelegate+Bridge.m +16 -14
- package/Sources/CCPets/CCPetsAppDelegate+Pets.m +7 -9
- package/Sources/CCPets/CCPetsAppDelegate+Private.h +3 -0
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +25 -4
- package/Sources/CCPets/CCPetsAppDelegate+Speech.m +1 -1
- package/Sources/CCPets/CCPetsAppDelegate+Update.m +36 -35
- package/Sources/CCPets/CCPetsAppDelegate.m +20 -14
- package/Sources/CCPets/CCPetsCleanup.m +9 -6
- package/Sources/CCPets/CCPetsEvents.m +4 -3
- package/Sources/CCPets/CCPetsL10n.h +55 -0
- package/Sources/CCPets/CCPetsL10n.m +219 -0
- package/Sources/CCPets/CCPetsPhrases.h +7 -1
- package/Sources/CCPets/CCPetsPhrases.m +86 -31
- package/Sources/CCPets/CCPetsPhrasesEditor.m +62 -64
- package/Sources/CCPets/CCPetsPreviews.m +7 -6
- package/Sources/CCPets/CCPetsVersion.h +7 -4
- package/Sources/CCPets/CCPetsVersion.m +14 -6
- package/Sources/CCPets/MenuToggleSwitch.m +3 -2
- package/Sources/CCPets/PetView.m +103 -74
- package/Sources/CCPets/QuotaDashboardView.m +122 -85
- package/Sources/CCPets/main.m +3 -1
- package/bin/cc-pets +21 -22
- package/bin/claude-with-pet +2 -1
- package/bin/codex-with-pet +2 -1
- package/package.json +3 -2
- package/scripts/bridge/cli.mjs +39 -38
- package/scripts/bridge/core.mjs +27 -26
- package/scripts/bridge/hook.mjs +7 -6
- package/scripts/bridge/install.mjs +24 -23
- package/scripts/bridge/mcp-server.mjs +79 -71
- package/scripts/bridge/options.mjs +7 -6
- package/scripts/bridge/reservations.mjs +6 -5
- package/scripts/bridge/store.mjs +10 -9
- package/scripts/build.sh +24 -5
- package/scripts/check-l10n.mjs +168 -0
- package/scripts/configure-updater.mjs +4 -3
- package/scripts/doctor.mjs +93 -79
- package/scripts/i18n.mjs +122 -0
- package/scripts/i18n.zsh +66 -0
- package/scripts/install-app.sh +3 -2
- package/scripts/install-claude-hooks.mjs +10 -9
- package/scripts/install-codex-hooks.mjs +7 -6
- package/scripts/install-shell-integration.sh +6 -5
- package/scripts/locales/zh-Hans.json +366 -0
- package/scripts/pet-store.mjs +45 -44
- package/scripts/uninstall-app.sh +4 -3
- package/scripts/uninstall-integrations.mjs +8 -7
- package/scripts/uninstall-shell-integration.sh +8 -7
- /package/Resources/{phrases.default.txt → phrases.default.zh-Hans.txt} +0 -0
|
@@ -270,7 +270,7 @@ NSString *const PetSpeechFrequencyKey = @"CCPetsSpeechFrequency";
|
|
|
270
270
|
if (![value isKindOfClass:NSString.class] || [value length] == 0) return;
|
|
271
271
|
[defaults removeObjectForKey:@"CCPetsSpeechDebugTag"];
|
|
272
272
|
NSString *text = PetPhraseForTag(value, [self speechSlots]);
|
|
273
|
-
if (text.length == 0) text = @"
|
|
273
|
+
if (text.length == 0) text = L(@"(No lines available for this tag)");
|
|
274
274
|
[self presentSpeechText:text];
|
|
275
275
|
}
|
|
276
276
|
- (void)hideSpeechBubble {
|
|
@@ -76,9 +76,10 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
76
76
|
BOOL npmCliExists = npmCliPath.isAbsolutePath &&
|
|
77
77
|
[NSFileManager.defaultManager isReadableFileAtPath:npmCliPath];
|
|
78
78
|
if (!nodeExecutable || !npmCliExists) {
|
|
79
|
-
[self showAlertWithTitle
|
|
80
|
-
message
|
|
81
|
-
|
|
79
|
+
[self showAlertWithTitle:L(@"Can't Update Automatically")
|
|
80
|
+
message:L(@"The Node.js/npm used to install CC Pets wasn't found. Run this once manually:\n"
|
|
81
|
+
"\n"
|
|
82
|
+
"npm install -g cc-pets@latest --allow-scripts=cc-pets")];
|
|
82
83
|
return;
|
|
83
84
|
}
|
|
84
85
|
|
|
@@ -87,7 +88,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
87
88
|
[NSFileManager.defaultManager createDirectoryAtPath:supportDirectory
|
|
88
89
|
withIntermediateDirectories:YES attributes:nil error:&directoryError];
|
|
89
90
|
if (directoryError) {
|
|
90
|
-
[self showAlertWithTitle
|
|
91
|
+
[self showAlertWithTitle:L(@"Can't Update Automatically") message:directoryError.localizedDescription];
|
|
91
92
|
return;
|
|
92
93
|
}
|
|
93
94
|
NSString *logPath = [supportDirectory stringByAppendingPathComponent:@"update.log"];
|
|
@@ -95,7 +96,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
95
96
|
chmod(logPath.fileSystemRepresentation, S_IRUSR | S_IWUSR);
|
|
96
97
|
NSFileHandle *logHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
|
|
97
98
|
if (!logHandle) {
|
|
98
|
-
[self showAlertWithTitle
|
|
99
|
+
[self showAlertWithTitle:L(@"Can't Update Automatically") message:L(@"Couldn't create the update log.")];
|
|
99
100
|
return;
|
|
100
101
|
}
|
|
101
102
|
|
|
@@ -142,8 +143,8 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
142
143
|
strongSelf.updating = NO;
|
|
143
144
|
[strongSelf refreshUpdateBadge];
|
|
144
145
|
if ([strongSelf restartAfterUpdateToVersion:version configuration:configuration]) return;
|
|
145
|
-
[strongSelf showAlertWithTitle
|
|
146
|
-
message
|
|
146
|
+
[strongSelf showAlertWithTitle:L(@"Update Complete")
|
|
147
|
+
message:L(@"CC Pets was updated, but the new app couldn't be relaunched automatically. Please restart it manually.")];
|
|
147
148
|
return;
|
|
148
149
|
}
|
|
149
150
|
// 暂时性故障(典型是刚发布的版本报 ETARGET)自动重试一次再说,
|
|
@@ -160,11 +161,11 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
160
161
|
[strongSelf refreshUpdateBadge];
|
|
161
162
|
[NSApp activateIgnoringOtherApps:YES];
|
|
162
163
|
NSAlert *alert = [NSAlert new];
|
|
163
|
-
alert.messageText = @"
|
|
164
|
+
alert.messageText = L(@"Update Failed");
|
|
164
165
|
alert.informativeText = [NSString stringWithFormat:
|
|
165
|
-
@"
|
|
166
|
-
[alert addButtonWithTitle
|
|
167
|
-
[alert addButtonWithTitle
|
|
166
|
+
L(@"You can keep using the current version. Details were written to:\n%@"), logPath];
|
|
167
|
+
[alert addButtonWithTitle:L(@"Open Log")];
|
|
168
|
+
[alert addButtonWithTitle:L(@"Close")];
|
|
168
169
|
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
169
170
|
[NSWorkspace.sharedWorkspace openURL:[NSURL fileURLWithPath:logPath]];
|
|
170
171
|
}
|
|
@@ -176,13 +177,13 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
176
177
|
[self refreshUpdateBadge];
|
|
177
178
|
self.updateTask = nil;
|
|
178
179
|
[logHandle closeFile];
|
|
179
|
-
[self showAlertWithTitle
|
|
180
|
+
[self showAlertWithTitle:L(@"Can't Start Update") message:launchError.localizedDescription];
|
|
180
181
|
return;
|
|
181
182
|
}
|
|
182
183
|
// 重试是静默的:第一次已经弹过"正在更新",再弹一次只会让人以为出了两回事。
|
|
183
184
|
if (attempt == 0) {
|
|
184
|
-
[self showAlertWithTitle
|
|
185
|
-
message:[NSString stringWithFormat
|
|
185
|
+
[self showAlertWithTitle:L(@"Updating")
|
|
186
|
+
message:[NSString stringWithFormat:L(@"Downloading and installing CC Pets %@. The pet will restart when it's done."), version]];
|
|
186
187
|
}
|
|
187
188
|
}
|
|
188
189
|
// 关于弹窗与检查更新同用 NSAlert,保持样式一致;版本号用构建时注入的 CC_PETS_VERSION(与检查更新
|
|
@@ -192,9 +193,9 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
192
193
|
NSAlert *alert = [NSAlert new];
|
|
193
194
|
alert.messageText = @"CC Pets";
|
|
194
195
|
alert.informativeText = [NSString stringWithFormat:
|
|
195
|
-
@"
|
|
196
|
-
[alert addButtonWithTitle
|
|
197
|
-
[alert addButtonWithTitle
|
|
196
|
+
L(@"Version %@\n\nA desktop pet for Claude Code and Codex CLI."), @CC_PETS_VERSION];
|
|
197
|
+
[alert addButtonWithTitle:L(@"OK")];
|
|
198
|
+
[alert addButtonWithTitle:L(@"Visit Homepage")];
|
|
198
199
|
if ([alert runModal] == NSAlertSecondButtonReturn) {
|
|
199
200
|
[NSWorkspace.sharedWorkspace openURL:
|
|
200
201
|
[NSURL URLWithString:[@"https://github.com/" stringByAppendingString:CCPetsRepositorySlug]]];
|
|
@@ -219,7 +220,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
219
220
|
BOOL valid = NO;
|
|
220
221
|
NSComparisonResult comparison = CompareStableVersions(@CC_PETS_VERSION, latestVersion, &valid);
|
|
221
222
|
if (error || httpResponse.statusCode != 200 || !valid) {
|
|
222
|
-
NSString *message = error.localizedDescription ?: @"npm
|
|
223
|
+
NSString *message = error.localizedDescription ?: L(@"The npm registry returned invalid version info. Please try again later.");
|
|
223
224
|
dispatch_async(dispatch_get_main_queue(), ^{ completion(nil, nil, NO, message); });
|
|
224
225
|
return;
|
|
225
226
|
}
|
|
@@ -242,8 +243,8 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
242
243
|
NSString *body = [release isKindOfClass:NSDictionary.class] &&
|
|
243
244
|
[release[@"body"] isKindOfClass:NSString.class] ? release[@"body"] : nil;
|
|
244
245
|
BOOL truncated = NO;
|
|
245
|
-
NSArray<NSString *> *highlights =
|
|
246
|
-
UpdateHighlightLimit, UpdateHighlightMaxLength, &truncated);
|
|
246
|
+
NSArray<NSString *> *highlights = ReleaseNoteHighlightsForLanguage(body,
|
|
247
|
+
CCPetsCurrentLanguage(), UpdateHighlightLimit, UpdateHighlightMaxLength, &truncated);
|
|
247
248
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
248
249
|
completion(latestVersion, highlights, truncated, nil);
|
|
249
250
|
});
|
|
@@ -262,14 +263,14 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
262
263
|
if (!strongSelf) return;
|
|
263
264
|
strongSelf.checkingForUpdate = NO;
|
|
264
265
|
if (errorMessage) {
|
|
265
|
-
[strongSelf showAlertWithTitle
|
|
266
|
+
[strongSelf showAlertWithTitle:L(@"Update Check Failed") message:errorMessage];
|
|
266
267
|
return;
|
|
267
268
|
}
|
|
268
269
|
BOOL valid = NO;
|
|
269
270
|
if (CompareStableVersions(@CC_PETS_VERSION, version, &valid) != NSOrderedAscending) {
|
|
270
271
|
[strongSelf clearPendingUpdate];
|
|
271
|
-
[strongSelf showAlertWithTitle
|
|
272
|
-
message:[NSString stringWithFormat
|
|
272
|
+
[strongSelf showAlertWithTitle:L(@"You're Up to Date")
|
|
273
|
+
message:[NSString stringWithFormat:L(@"Version %@"), @CC_PETS_VERSION]];
|
|
273
274
|
return;
|
|
274
275
|
}
|
|
275
276
|
[strongSelf rememberPendingUpdate:version highlights:highlights truncated:truncated];
|
|
@@ -317,7 +318,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
317
318
|
BOOL show = self.pendingUpdateVersion.length > 0 && !self.updating;
|
|
318
319
|
self.updateBadgeView.hidden = !show;
|
|
319
320
|
self.updateBadgeButton.toolTip = show
|
|
320
|
-
? [NSString stringWithFormat
|
|
321
|
+
? [NSString stringWithFormat:L(@"Version %@ is available. Click to view."), self.pendingUpdateVersion] : nil;
|
|
321
322
|
}
|
|
322
323
|
- (void)clearPendingUpdate {
|
|
323
324
|
self.pendingUpdateVersion = nil;
|
|
@@ -343,7 +344,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
343
344
|
NSMutableParagraphStyle *headerStyle = [NSMutableParagraphStyle new];
|
|
344
345
|
headerStyle.paragraphSpacing = 6;
|
|
345
346
|
|
|
346
|
-
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString
|
|
347
|
+
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:L(@"What's New\n")
|
|
347
348
|
attributes:@{NSFontAttributeName: [NSFont systemFontOfSize:12 weight:NSFontWeightSemibold],
|
|
348
349
|
NSForegroundColorAttributeName: NSColor.labelColor,
|
|
349
350
|
NSParagraphStyleAttributeName: headerStyle}];
|
|
@@ -358,7 +359,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
358
359
|
attributes:itemAttributes]];
|
|
359
360
|
}];
|
|
360
361
|
if (self.pendingUpdateHighlightsTruncated) {
|
|
361
|
-
[text appendAttributedString:[[NSAttributedString alloc] initWithString
|
|
362
|
+
[text appendAttributedString:[[NSAttributedString alloc] initWithString:L(@"More changes in the full GitHub release notes")
|
|
362
363
|
attributes:@{NSFontAttributeName: [NSFont systemFontOfSize:11],
|
|
363
364
|
NSForegroundColorAttributeName: NSColor.tertiaryLabelColor,
|
|
364
365
|
NSParagraphStyleAttributeName: itemStyle}]];
|
|
@@ -376,12 +377,12 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
376
377
|
if (self.updateBubbleVisible) [self hideSpeechBubble];
|
|
377
378
|
[NSApp activateIgnoringOtherApps:YES];
|
|
378
379
|
NSAlert *alert = [NSAlert new];
|
|
379
|
-
alert.messageText = [NSString stringWithFormat
|
|
380
|
-
alert.informativeText = [NSString stringWithFormat
|
|
380
|
+
alert.messageText = [NSString stringWithFormat:L(@"CC Pets %@ Is Available"), version];
|
|
381
|
+
alert.informativeText = [NSString stringWithFormat:L(@"Current version %@"), @CC_PETS_VERSION];
|
|
381
382
|
alert.accessoryView = [self updateHighlightsAccessoryView];
|
|
382
|
-
[alert addButtonWithTitle
|
|
383
|
-
[alert addButtonWithTitle
|
|
384
|
-
if (self.pendingUpdateHighlights.count > 0) [alert addButtonWithTitle
|
|
383
|
+
[alert addButtonWithTitle:L(@"Update Now")];
|
|
384
|
+
[alert addButtonWithTitle:L(@"Later")];
|
|
385
|
+
if (self.pendingUpdateHighlights.count > 0) [alert addButtonWithTitle:L(@"Release Notes")];
|
|
385
386
|
NSModalResponse response = [alert runModal];
|
|
386
387
|
if (response == NSAlertFirstButtonReturn) {
|
|
387
388
|
[self startUpdateToVersion:version];
|
|
@@ -429,15 +430,15 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
429
430
|
return;
|
|
430
431
|
}
|
|
431
432
|
self.updateBubbleDeferred = NO;
|
|
432
|
-
[self showUpdateBubbleWithText:[NSString stringWithFormat
|
|
433
|
+
[self showUpdateBubbleWithText:[NSString stringWithFormat:L(@"CC Pets v%@ is out! Click to update"),
|
|
433
434
|
self.pendingUpdateVersion] dwell:UpdateBubbleDwell];
|
|
434
435
|
}
|
|
435
436
|
// 碎碎念时机里的更新提醒,几句轮换,免得每次都是同一句。
|
|
436
437
|
- (NSString *)updateReminderText {
|
|
437
438
|
NSArray<NSString *> *lines = @[
|
|
438
|
-
@"
|
|
439
|
-
@"{version}
|
|
440
|
-
@"
|
|
439
|
+
L(@"{version} is still waiting. Click me!"),
|
|
440
|
+
L(@"{version} is out. See what's new!"),
|
|
441
|
+
L(@"Upgrade to {version}? Click me"),
|
|
441
442
|
];
|
|
442
443
|
return [lines[arc4random_uniform((uint32_t)lines.count)]
|
|
443
444
|
stringByReplacingOccurrencesOfString:@"{version}" withString:self.pendingUpdateVersion];
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
NSAlert *alert = [NSAlert new];
|
|
59
59
|
alert.messageText = title;
|
|
60
60
|
alert.informativeText = message;
|
|
61
|
-
[alert addButtonWithTitle
|
|
61
|
+
[alert addButtonWithTitle:L(@"OK")];
|
|
62
62
|
[alert runModal];
|
|
63
63
|
}
|
|
64
64
|
// 建一个只有"编辑"的主菜单。
|
|
@@ -76,19 +76,19 @@
|
|
|
76
76
|
- (void)installEditMenu {
|
|
77
77
|
if (NSApp.mainMenu) return;
|
|
78
78
|
NSMenu *mainMenu = [NSMenu new];
|
|
79
|
-
NSMenuItem *editItem = [mainMenu addItemWithTitle
|
|
80
|
-
NSMenu *editMenu = [[NSMenu alloc] initWithTitle
|
|
79
|
+
NSMenuItem *editItem = [mainMenu addItemWithTitle:L(@"Edit") action:nil keyEquivalent:@""];
|
|
80
|
+
NSMenu *editMenu = [[NSMenu alloc] initWithTitle:L(@"Edit")];
|
|
81
81
|
|
|
82
82
|
NSArray<NSArray *> *entries = @[
|
|
83
|
-
@[@"
|
|
84
|
-
@[@"
|
|
83
|
+
@[L(@"Undo"), NSStringFromSelector(@selector(undo:)), @"z", @(NSEventModifierFlagCommand)],
|
|
84
|
+
@[L(@"Redo"), NSStringFromSelector(@selector(redo:)), @"z",
|
|
85
85
|
@(NSEventModifierFlagCommand | NSEventModifierFlagShift)],
|
|
86
86
|
@[@"-", @"", @"", @0],
|
|
87
|
-
@[@"
|
|
88
|
-
@[@"
|
|
89
|
-
@[@"
|
|
90
|
-
@[@"
|
|
91
|
-
@[@"
|
|
87
|
+
@[L(@"Cut"), NSStringFromSelector(@selector(cut:)), @"x", @(NSEventModifierFlagCommand)],
|
|
88
|
+
@[L(@"Copy"), NSStringFromSelector(@selector(copy:)), @"c", @(NSEventModifierFlagCommand)],
|
|
89
|
+
@[L(@"Paste"), NSStringFromSelector(@selector(paste:)), @"v", @(NSEventModifierFlagCommand)],
|
|
90
|
+
@[L(@"Delete"), NSStringFromSelector(@selector(delete:)), @"", @0],
|
|
91
|
+
@[L(@"Select All"), NSStringFromSelector(@selector(selectAll:)), @"a", @(NSEventModifierFlagCommand)],
|
|
92
92
|
];
|
|
93
93
|
for (NSArray *entry in entries) {
|
|
94
94
|
if ([entry[0] isEqualToString:@"-"]) {
|
|
@@ -108,6 +108,12 @@
|
|
|
108
108
|
// 台词全在这个文件里,代码里没有第二份。首次启动先从默认词库拷一份出来,
|
|
109
109
|
// 否则新装的桌宠一句话都不会说。
|
|
110
110
|
PetPhrasesEnsureFileExists();
|
|
111
|
+
// 没改过的默认台词跟着界面语言走(例如英文系统上从旧版升级上来的用户)。
|
|
112
|
+
PetPhrasesAdoptLanguageDefaults();
|
|
113
|
+
// Node 端(cc-pets、hook、CC Bridge)读这个文件决定输出语言。
|
|
114
|
+
CCPetsWriteLanguageFile();
|
|
115
|
+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(languageDidChange:)
|
|
116
|
+
name:CCPetsLanguageDidChangeNotification object:nil];
|
|
111
117
|
if (![NSUserDefaults.standardUserDefaults boolForKey:PetInteractionPhrasesV1MigratedKey] &&
|
|
112
118
|
PetPhrasesEnsureInteractionSections()) {
|
|
113
119
|
[NSUserDefaults.standardUserDefaults setBool:YES
|
|
@@ -156,7 +162,7 @@
|
|
|
156
162
|
NSImage *image = LoadPetSpriteImage(spritePath, NSMakeSize(140, 150),
|
|
157
163
|
[selectedOption[@"spriteRowCount"] integerValue] ?: 9);
|
|
158
164
|
if (!image) {
|
|
159
|
-
fprintf(stderr, "
|
|
165
|
+
fprintf(stderr, "%s\n", [NSString stringWithFormat:L(@"Can't read the pet sprite: %@"), spritePath].UTF8String);
|
|
160
166
|
[NSApp terminate:nil];
|
|
161
167
|
return;
|
|
162
168
|
}
|
|
@@ -234,7 +240,7 @@
|
|
|
234
240
|
self.updateBadgeArrow = [[NSImageView alloc] initWithFrame:updateBadge.bounds];
|
|
235
241
|
self.updateBadgeArrow.imageScaling = NSImageScaleNone;
|
|
236
242
|
self.updateBadgeArrow.image = [[NSImage imageWithSystemSymbolName:@"arrow.up"
|
|
237
|
-
accessibilityDescription
|
|
243
|
+
accessibilityDescription:L(@"Update available")] imageWithSymbolConfiguration:
|
|
238
244
|
[NSImageSymbolConfiguration configurationWithPointSize:11 weight:NSFontWeightBold]];
|
|
239
245
|
[updateBadge addSubview:self.updateBadgeArrow];
|
|
240
246
|
CCPetsStatusClickButton *updateClick = [[CCPetsStatusClickButton alloc]
|
|
@@ -316,7 +322,7 @@
|
|
|
316
322
|
self.statusIconButton.wantsLayer = YES;
|
|
317
323
|
self.statusIconButton.layer.cornerRadius = 17;
|
|
318
324
|
self.statusIconButton.layer.masksToBounds = YES;
|
|
319
|
-
self.statusIconButton.toolTip = @"
|
|
325
|
+
self.statusIconButton.toolTip = L(@"Recent Agent sessions and CC Bridge messages");
|
|
320
326
|
self.statusIconButton.target = self;
|
|
321
327
|
self.statusIconButton.action = @selector(showAgentSessionsMenu:);
|
|
322
328
|
[self.statusGlass.contentView addSubview:self.statusIconButton];
|
|
@@ -330,7 +336,7 @@
|
|
|
330
336
|
statusClick.transparent = YES;
|
|
331
337
|
statusClick.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
332
338
|
statusClick.title = @"";
|
|
333
|
-
statusClick.toolTip = @"
|
|
339
|
+
statusClick.toolTip = L(@"Go back to the Agent's terminal");
|
|
334
340
|
statusClick.target = self;
|
|
335
341
|
statusClick.action = @selector(focusLatestAgentTerminal:);
|
|
336
342
|
self.statusClickButton = statusClick;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#import "CCPetsL10n.h"
|
|
1
2
|
#import "CCPetsCleanup.h"
|
|
2
3
|
#import "CCPetsPaths.h"
|
|
3
4
|
#import <sys/file.h>
|
|
@@ -147,13 +148,13 @@ int CleanCCPetsData(BOOL purge) {
|
|
|
147
148
|
NSString *supportDirectory = ApplicationSupportDirectory();
|
|
148
149
|
NSString *buildCache = BuildCacheDirectory();
|
|
149
150
|
if (!IsSafeSupportDirectory(supportDirectory)) {
|
|
150
|
-
fprintf(stderr, "
|
|
151
|
-
supportDirectory.UTF8String);
|
|
151
|
+
fprintf(stderr, "%s\n", [NSString stringWithFormat:
|
|
152
|
+
L(@"Clean failed: the app data directory doesn't belong to CC Pets: %@"), supportDirectory].UTF8String);
|
|
152
153
|
return 3;
|
|
153
154
|
}
|
|
154
155
|
if (buildCache.length > 0 && !IsSafeBuildCacheDirectory(buildCache)) {
|
|
155
|
-
fprintf(stderr, "
|
|
156
|
-
buildCache.UTF8String);
|
|
156
|
+
fprintf(stderr, "%s\n", [NSString stringWithFormat:
|
|
157
|
+
L(@"Clean failed: the build cache directory doesn't belong to CC Pets: %@"), buildCache].UTF8String);
|
|
157
158
|
return 3;
|
|
158
159
|
}
|
|
159
160
|
NSUInteger removed = 0;
|
|
@@ -174,8 +175,10 @@ int CleanCCPetsData(BOOL purge) {
|
|
|
174
175
|
[NSUserDefaults.standardUserDefaults removePersistentDomainForName:domain];
|
|
175
176
|
[NSUserDefaults.standardUserDefaults synchronize];
|
|
176
177
|
}
|
|
177
|
-
|
|
178
|
-
(
|
|
178
|
+
NSString *format = purge
|
|
179
|
+
? (removed != 1 ? L(@"Full cleanup done. Removed %lu items.") : L(@"Full cleanup done. Removed %lu item."))
|
|
180
|
+
: (removed != 1 ? L(@"Cache cleanup done. Removed %lu items.") : L(@"Cache cleanup done. Removed %lu item."));
|
|
181
|
+
printf("%s\n", [NSString stringWithFormat:format, (unsigned long)removed].UTF8String);
|
|
179
182
|
return EXIT_SUCCESS;
|
|
180
183
|
}
|
|
181
184
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#import "CCPetsL10n.h"
|
|
1
2
|
#import "CCPetsEvents.h"
|
|
2
3
|
#import "CCPetsPaths.h"
|
|
3
4
|
#import "CCPetsTerminalFocus.h"
|
|
@@ -210,7 +211,7 @@ int RecordProviderEvent(void) {
|
|
|
210
211
|
if (![payload isKindOfClass:NSDictionary.class] ||
|
|
211
212
|
![payload[@"schemaVersion"] isKindOfClass:NSNumber.class] ||
|
|
212
213
|
[payload[@"schemaVersion"] integerValue] != 1) {
|
|
213
|
-
fprintf(stderr, "provider-event
|
|
214
|
+
fprintf(stderr, "%s\n", L(@"provider-event needs a JSON object with schemaVersion 1.").UTF8String);
|
|
214
215
|
return 2;
|
|
215
216
|
}
|
|
216
217
|
NSString *provider = SanitizedShortString(payload[@"provider"], 32);
|
|
@@ -221,7 +222,7 @@ int RecordProviderEvent(void) {
|
|
|
221
222
|
@"tool_completed", @"tool_failed", @"completed", @"failed", @"notification"
|
|
222
223
|
]];
|
|
223
224
|
if (provider.length == 0 || ![states containsObject:state]) {
|
|
224
|
-
fprintf(stderr, "provider-event
|
|
225
|
+
fprintf(stderr, "%s\n", L(@"provider-event has an invalid provider or state.").UTF8String);
|
|
225
226
|
return 2;
|
|
226
227
|
}
|
|
227
228
|
NSDictionary<NSString *, NSString *> *events = @{
|
|
@@ -247,7 +248,7 @@ int RecordProviderEvent(void) {
|
|
|
247
248
|
NSDictionary *terminal = TerminalFocusTargetFromEnvironment();
|
|
248
249
|
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
249
250
|
if (!AppendAgentEventRecord(record)) {
|
|
250
|
-
fprintf(stderr, "
|
|
251
|
+
fprintf(stderr, "%s\n", L(@"Couldn't write the provider event.").UTF8String);
|
|
251
252
|
return EXIT_FAILURE;
|
|
252
253
|
}
|
|
253
254
|
return EXIT_SUCCESS;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 界面多语言。
|
|
4
|
+
//
|
|
5
|
+
// 源码里直接写英文原文,它同时就是查表的 key(gettext 的做法):
|
|
6
|
+
// - 英文界面不查表,原样返回;
|
|
7
|
+
// - 其他语言查 <语言>.lproj/Localizable.strings,查不到就退回英文原文。
|
|
8
|
+
// 英文是源语言:漏翻的条目显示英文,比让别的语言用户看到某种第三语言好接受;
|
|
9
|
+
// 新语言也从英文译。代价是改了英文原文要同步改各语言表的 key,
|
|
10
|
+
// scripts/check-l10n.mjs 会把漏掉的报出来。
|
|
11
|
+
//
|
|
12
|
+
// 带参数的文案照常写 %@ / %ld;参数顺序和译文语序不同时,两边都用 %1$@ / %2$@ 标位置。
|
|
13
|
+
//
|
|
14
|
+
// 支持哪些语言不写在代码里:英文之外,有 <语言>.lproj/Localizable.strings 的就算支持。
|
|
15
|
+
// 新增一种语言只需加文件,见 CONTRIBUTING.md 的「多语言」一节。每张表除了界面文案,
|
|
16
|
+
// 还要带两条约定的元信息(键见下方常量):语言自己的名字、Release 说明里对应段落的标题。
|
|
17
|
+
|
|
18
|
+
// 语言偏好(NSUserDefaults):system 或某个支持的语言标识(en、zh-Hans…)。
|
|
19
|
+
extern NSString *const CCPetsLanguageKey;
|
|
20
|
+
extern NSString *const CCPetsLanguageSystem;
|
|
21
|
+
// 源语言,也是什么都匹配不上时的兜底。
|
|
22
|
+
extern NSString *const CCPetsSourceLanguage;
|
|
23
|
+
// 表里的元信息键。"Language Name" 的值是菜单里显示的语言名(用该语言自己的写法);
|
|
24
|
+
// "Release Notes Section" 是 GitHub Release 里该语言段落的标题,有多种写法时用 | 隔开。
|
|
25
|
+
extern NSString *const CCPetsLanguageNameKey;
|
|
26
|
+
extern NSString *const CCPetsReleaseNotesSectionKey;
|
|
27
|
+
// 切换语言后发出;常驻的界面(状态卡、额度面板、编辑器)收到后重绘。菜单每次现建,不用管。
|
|
28
|
+
extern NSNotificationName const CCPetsLanguageDidChangeNotification;
|
|
29
|
+
|
|
30
|
+
// 支持的语言:源语言在最前,其余按标识排序。
|
|
31
|
+
NSArray<NSString *> *CCPetsSupportedLanguages(void);
|
|
32
|
+
// 菜单里显示的语言名。
|
|
33
|
+
NSString *CCPetsLanguageDisplayName(NSString *language);
|
|
34
|
+
// Release 说明里该语言段落可能的标题。
|
|
35
|
+
NSArray<NSString *> *CCPetsReleaseNotesSectionTitles(NSString *language);
|
|
36
|
+
|
|
37
|
+
NSString *CCPetsLanguagePreference(void);
|
|
38
|
+
void CCPetsSetLanguagePreference(NSString *preference);
|
|
39
|
+
// 实际生效的语言,一定在 CCPetsSupportedLanguages() 里。
|
|
40
|
+
// 环境变量 CC_PETS_LANGUAGE 优先(测试用,也方便临时切换),其次是偏好,最后跟随系统:
|
|
41
|
+
// 系统首选语言里第一个能匹配上的说了算,都匹配不上就用源语言。
|
|
42
|
+
NSString *CCPetsCurrentLanguage(void);
|
|
43
|
+
BOOL CCPetsLanguageIsSource(void);
|
|
44
|
+
// 按给定的支持列表解析,不读任何全局状态,测试用。匹配规则:先找完全一致或前缀一致的
|
|
45
|
+
// (zh-Hans-CN → zh-Hans),再退到只比主语言(zh-Hant → zh-Hans、ja_JP → ja)。
|
|
46
|
+
NSString *CCPetsResolveLanguageAmong(NSString *preference, NSArray<NSString *> *systemLanguages,
|
|
47
|
+
NSArray<NSString *> *supported);
|
|
48
|
+
|
|
49
|
+
NSString *CCPetsLocalized(NSString *text) NS_FORMAT_ARGUMENT(1);
|
|
50
|
+
#define L(text) CCPetsLocalized(text)
|
|
51
|
+
|
|
52
|
+
// ~/.cc-pets/language(可用 CC_PETS_HOME 覆盖)。桌宠把实际生效的语言写在这里,
|
|
53
|
+
// Node 端的 cc-pets / hook / CC Bridge 据此输出同一种语言。
|
|
54
|
+
NSString *CCPetsLanguageFilePath(void);
|
|
55
|
+
void CCPetsWriteLanguageFile(void);
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
#import "CCPetsL10n.h"
|
|
2
|
+
|
|
3
|
+
NSString *const CCPetsLanguageKey = @"CCPetsLanguage";
|
|
4
|
+
NSString *const CCPetsLanguageSystem = @"system";
|
|
5
|
+
NSString *const CCPetsSourceLanguage = @"en";
|
|
6
|
+
NSString *const CCPetsLanguageNameKey = @"Language Name";
|
|
7
|
+
NSString *const CCPetsReleaseNotesSectionKey = @"Release Notes Section";
|
|
8
|
+
NSNotificationName const CCPetsLanguageDidChangeNotification = @"CCPetsLanguageDidChange";
|
|
9
|
+
|
|
10
|
+
// 源语言没有表,名字和 Release 段落标题只能写在这里。
|
|
11
|
+
static NSString *const CCPetsSourceLanguageName = @"English";
|
|
12
|
+
|
|
13
|
+
// 放 .lproj 的目录,依次是:CC_PETS_LOCALIZATION_DIR(测试用)、app bundle、
|
|
14
|
+
// 可执行文件旁边(直接跑 .build/release/cc-pets 时没有 bundle)。
|
|
15
|
+
static NSArray<NSString *> *CCPetsLocalizationDirectories(void) {
|
|
16
|
+
NSMutableArray<NSString *> *directories = [NSMutableArray array];
|
|
17
|
+
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_LOCALIZATION_DIR"];
|
|
18
|
+
if (override.length > 0) [directories addObject:override.stringByStandardizingPath];
|
|
19
|
+
NSString *resources = NSBundle.mainBundle.resourcePath;
|
|
20
|
+
if (resources.length > 0) [directories addObject:resources];
|
|
21
|
+
NSString *executableDirectory =
|
|
22
|
+
NSBundle.mainBundle.executablePath.stringByDeletingLastPathComponent;
|
|
23
|
+
if (executableDirectory.length > 0) [directories addObject:executableDirectory];
|
|
24
|
+
return directories;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static NSString *CCPetsTablePath(NSString *directory, NSString *language) {
|
|
28
|
+
return [[directory stringByAppendingPathComponent:
|
|
29
|
+
[language stringByAppendingPathExtension:@"lproj"]]
|
|
30
|
+
stringByAppendingPathComponent:@"Localizable.strings"];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 每种语言的表只读一次。表随 app 打包,运行期不会变。
|
|
34
|
+
static NSMutableDictionary<NSString *, NSDictionary<NSString *, NSString *> *> *CCPetsTables;
|
|
35
|
+
|
|
36
|
+
static NSDictionary<NSString *, NSString *> *CCPetsTable(NSString *language) {
|
|
37
|
+
if (language.length == 0 || [language isEqualToString:CCPetsSourceLanguage]) return nil;
|
|
38
|
+
@synchronized(CCPetsLanguageKey) {
|
|
39
|
+
if (!CCPetsTables) CCPetsTables = [NSMutableDictionary dictionary];
|
|
40
|
+
id cached = CCPetsTables[language];
|
|
41
|
+
if (cached) return cached == NSNull.null ? nil : cached;
|
|
42
|
+
NSDictionary *table = nil;
|
|
43
|
+
for (NSString *directory in CCPetsLocalizationDirectories()) {
|
|
44
|
+
NSDictionary *candidate = [NSDictionary dictionaryWithContentsOfFile:
|
|
45
|
+
CCPetsTablePath(directory, language)];
|
|
46
|
+
if ([candidate isKindOfClass:NSDictionary.class]) {
|
|
47
|
+
table = candidate;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
CCPetsTables[language] = table ?: (id)NSNull.null;
|
|
52
|
+
return table;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
NSArray<NSString *> *CCPetsSupportedLanguages(void) {
|
|
57
|
+
static NSArray<NSString *> *languages;
|
|
58
|
+
static dispatch_once_t once;
|
|
59
|
+
dispatch_once(&once, ^{
|
|
60
|
+
NSMutableSet<NSString *> *found = [NSMutableSet set];
|
|
61
|
+
NSFileManager *manager = NSFileManager.defaultManager;
|
|
62
|
+
for (NSString *directory in CCPetsLocalizationDirectories()) {
|
|
63
|
+
for (NSString *entry in [manager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
64
|
+
if (![entry.pathExtension isEqualToString:@"lproj"]) continue;
|
|
65
|
+
NSString *language = entry.stringByDeletingPathExtension;
|
|
66
|
+
if ([language isEqualToString:CCPetsSourceLanguage]) continue;
|
|
67
|
+
// 只有 InfoPlist.strings 的目录(如 en.lproj)不算一种界面语言。
|
|
68
|
+
if ([manager fileExistsAtPath:CCPetsTablePath(directory, language)]) {
|
|
69
|
+
[found addObject:language];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
NSArray *sorted = [found.allObjects sortedArrayUsingSelector:@selector(compare:)];
|
|
74
|
+
languages = [@[CCPetsSourceLanguage] arrayByAddingObjectsFromArray:sorted];
|
|
75
|
+
});
|
|
76
|
+
return languages;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
NSString *CCPetsLanguageDisplayName(NSString *language) {
|
|
80
|
+
if ([language isEqualToString:CCPetsSourceLanguage]) return CCPetsSourceLanguageName;
|
|
81
|
+
NSString *name = CCPetsTable(language)[CCPetsLanguageNameKey];
|
|
82
|
+
return name.length > 0 ? name : language;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
NSArray<NSString *> *CCPetsReleaseNotesSectionTitles(NSString *language) {
|
|
86
|
+
if ([language isEqualToString:CCPetsSourceLanguage]) return @[CCPetsSourceLanguageName];
|
|
87
|
+
NSString *titles = CCPetsTable(language)[CCPetsReleaseNotesSectionKey];
|
|
88
|
+
NSMutableArray<NSString *> *result = [NSMutableArray array];
|
|
89
|
+
for (NSString *title in [titles componentsSeparatedByString:@"|"]) {
|
|
90
|
+
NSString *trimmed = [title stringByTrimmingCharactersInSet:
|
|
91
|
+
NSCharacterSet.whitespaceCharacterSet];
|
|
92
|
+
if (trimmed.length > 0) [result addObject:trimmed];
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// zh_CN.UTF-8 → zh-cn。系统给的是 BCP 47(zh-Hans-CN),环境变量里常是 POSIX 写法。
|
|
98
|
+
static NSString *CCPetsNormalizedTag(NSString *tag) {
|
|
99
|
+
NSString *lower = [tag.lowercaseString stringByReplacingOccurrencesOfString:@"_" withString:@"-"];
|
|
100
|
+
NSRange dot = [lower rangeOfString:@"."];
|
|
101
|
+
return dot.location == NSNotFound ? lower : [lower substringToIndex:dot.location];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static NSString *CCPetsBaseLanguage(NSString *normalizedTag) {
|
|
105
|
+
return [normalizedTag componentsSeparatedByString:@"-"].firstObject;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static NSString *CCPetsMatchLanguage(NSString *candidate, NSArray<NSString *> *supported) {
|
|
109
|
+
NSString *tag = CCPetsNormalizedTag(candidate);
|
|
110
|
+
if (tag.length == 0) return nil;
|
|
111
|
+
for (NSString *language in supported) {
|
|
112
|
+
NSString *lower = language.lowercaseString;
|
|
113
|
+
if ([tag isEqualToString:lower] || [tag hasPrefix:[lower stringByAppendingString:@"-"]]) {
|
|
114
|
+
return language;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
NSString *base = CCPetsBaseLanguage(tag);
|
|
118
|
+
for (NSString *language in supported) {
|
|
119
|
+
if ([CCPetsBaseLanguage(language.lowercaseString) isEqualToString:base]) return language;
|
|
120
|
+
}
|
|
121
|
+
return nil;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
NSString *CCPetsResolveLanguageAmong(NSString *preference, NSArray<NSString *> *systemLanguages,
|
|
125
|
+
NSArray<NSString *> *supported) {
|
|
126
|
+
NSString *explicit = [preference isEqualToString:CCPetsLanguageSystem] ? nil
|
|
127
|
+
: CCPetsMatchLanguage(preference, supported);
|
|
128
|
+
if (explicit) return explicit;
|
|
129
|
+
for (NSString *language in systemLanguages) {
|
|
130
|
+
NSString *match = CCPetsMatchLanguage(language, supported);
|
|
131
|
+
if (match) return match;
|
|
132
|
+
}
|
|
133
|
+
return CCPetsSourceLanguage;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static NSString *CCPetsNormalizedPreference(NSString *preference) {
|
|
137
|
+
return [CCPetsSupportedLanguages() containsObject:preference] ? preference : CCPetsLanguageSystem;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
NSString *CCPetsLanguagePreference(void) {
|
|
141
|
+
return CCPetsNormalizedPreference(
|
|
142
|
+
[NSUserDefaults.standardUserDefaults stringForKey:CCPetsLanguageKey]);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 当前语言在偏好和环境变量都不变时不会变,但查表在绘制路径上每帧都可能调用,
|
|
146
|
+
// 缓存一份,切换偏好时清掉。
|
|
147
|
+
static NSString *CCPetsCachedLanguage;
|
|
148
|
+
|
|
149
|
+
// 系统首选语言。偏好里选了具体语言时,桌宠会把 AppleLanguages 写进自己的 defaults 域,
|
|
150
|
+
// 好让系统自带的按钮、面板在下次启动后也换成同一种语言;所以这里要读全局域,
|
|
151
|
+
// 不能用 NSLocale.preferredLanguages(它会读到我们自己写进去的那份)。
|
|
152
|
+
static NSArray<NSString *> *CCPetsSystemLanguages(void) {
|
|
153
|
+
NSArray *global = [[NSUserDefaults.standardUserDefaults
|
|
154
|
+
persistentDomainForName:NSGlobalDomain] objectForKey:@"AppleLanguages"];
|
|
155
|
+
if ([global isKindOfClass:NSArray.class] && global.count > 0) return global;
|
|
156
|
+
return NSLocale.preferredLanguages;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
NSString *CCPetsCurrentLanguage(void) {
|
|
160
|
+
@synchronized(CCPetsLanguageKey) {
|
|
161
|
+
if (CCPetsCachedLanguage) return CCPetsCachedLanguage;
|
|
162
|
+
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_LANGUAGE"];
|
|
163
|
+
NSString *preference = override.length > 0 ? override : CCPetsLanguagePreference();
|
|
164
|
+
CCPetsCachedLanguage = CCPetsResolveLanguageAmong(preference, CCPetsSystemLanguages(),
|
|
165
|
+
CCPetsSupportedLanguages());
|
|
166
|
+
return CCPetsCachedLanguage;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
BOOL CCPetsLanguageIsSource(void) {
|
|
171
|
+
return [CCPetsCurrentLanguage() isEqualToString:CCPetsSourceLanguage];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
NSString *CCPetsLocalized(NSString *text) {
|
|
175
|
+
if (text.length == 0) return text;
|
|
176
|
+
NSString *language = CCPetsCurrentLanguage();
|
|
177
|
+
if ([language isEqualToString:CCPetsSourceLanguage]) return text;
|
|
178
|
+
NSString *translated = CCPetsTable(language)[text];
|
|
179
|
+
return translated.length > 0 ? translated : text;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
NSString *CCPetsLanguageFilePath(void) {
|
|
183
|
+
NSString *home = NSProcessInfo.processInfo.environment[@"CC_PETS_HOME"];
|
|
184
|
+
if (home.length == 0) {
|
|
185
|
+
home = [NSHomeDirectory() stringByAppendingPathComponent:@".cc-pets"];
|
|
186
|
+
}
|
|
187
|
+
return [home.stringByStandardizingPath stringByAppendingPathComponent:@"language"];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
void CCPetsWriteLanguageFile(void) {
|
|
191
|
+
NSString *path = CCPetsLanguageFilePath();
|
|
192
|
+
NSString *language = CCPetsCurrentLanguage();
|
|
193
|
+
NSString *existing = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding
|
|
194
|
+
error:nil];
|
|
195
|
+
NSString *content = [language stringByAppendingString:@"\n"];
|
|
196
|
+
if ([existing isEqualToString:content]) return;
|
|
197
|
+
[NSFileManager.defaultManager createDirectoryAtPath:path.stringByDeletingLastPathComponent
|
|
198
|
+
withIntermediateDirectories:YES attributes:nil error:nil];
|
|
199
|
+
[content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void CCPetsSetLanguagePreference(NSString *preference) {
|
|
203
|
+
NSString *normalized = CCPetsNormalizedPreference(preference);
|
|
204
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
205
|
+
[defaults setObject:normalized forKey:CCPetsLanguageKey];
|
|
206
|
+
// 系统自带的部分(NSAlert 默认按钮、打开面板、编辑菜单的系统项)跟 AppleLanguages 走,
|
|
207
|
+
// 只在下次启动时生效。跟随系统时删掉,回到全局设置。
|
|
208
|
+
if ([normalized isEqualToString:CCPetsLanguageSystem]) {
|
|
209
|
+
[defaults removeObjectForKey:@"AppleLanguages"];
|
|
210
|
+
} else {
|
|
211
|
+
[defaults setObject:@[normalized] forKey:@"AppleLanguages"];
|
|
212
|
+
}
|
|
213
|
+
@synchronized(CCPetsLanguageKey) {
|
|
214
|
+
CCPetsCachedLanguage = nil;
|
|
215
|
+
}
|
|
216
|
+
CCPetsWriteLanguageFile();
|
|
217
|
+
[NSNotificationCenter.defaultCenter
|
|
218
|
+
postNotificationName:CCPetsLanguageDidChangeNotification object:nil];
|
|
219
|
+
}
|