我可以在iOS中翻译我的应用语言。我不明白为什么,但是在Android平台上可以正常使用。我做错了什么吗?还是应该添加某项来使这项工作有效?请给我一些指针。
本地化文件内容
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Locale userLanguage;
//Locale userLanguage = Locale('ko', 'KR');
class AppLocalizations {
final Locale locale;
//
AppLocalizations(this.locale);
static AppLocalizations of(BuildContext context) {
return Localizations.of(context, AppLocalizations);
}
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
Map<String, String> _localizedStrings;
Future<bool> load(Locale localeInput) async {
print('inside load ' + localeInput.languageCode);
String jsonString =
await rootBundle.loadString('i18n/${localeInput.languageCode}.json');
print('After load ' + localeInput.languageCode);
Map<String, dynamic> jsonMap = json.decode(jsonString);
_localizedStrings = jsonMap.map(
(key, value) {
return MapEntry(key, value.toString());
},
);
return true;
}
String translate(String key) {
return _localizedStrings[key];
}
// String language() {
// if (true) {
// return 'en';
// }
// return 'ko';
// }
}
class _AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizations> {
const _AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) {
return ['en', 'ko'].contains(locale.languageCode);
}
@override
Future<AppLocalizations> load(Locale locale) async {
if (userLanguage != null) {
locale = userLanguage;
}
AppLocalizations localizations = new AppLocalizations(locale);
await localizations.load(locale);
return localizations;
}
@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}
在主文件中
supportedLocales: [
Locale('en', 'US'),
Locale('ko', 'KR'),
],
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
localeResolutionCallback: (deviceLocale, supportedLocales) {
if (userLocale != null) {
print('User preferred language is ' + userLocale.languageCode);
return userLocale;
} else {
for (var locale in supportedLocales) {
print(locale.languageCode + deviceLocale.languageCode);
if (locale.languageCode == deviceLocale.languageCode &&
locale.countryCode == deviceLocale.countryCode) {
print(deviceLocale.languageCode + ' is supported');
return deviceLocale;
}
}
print('>>>>> ' +
deviceLocale.languageCode +
' <<<<<< is not supported');
return supportedLocales.first;
}
},
Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>RecycleX</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb30109**********5293</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>FacebookAppID</key>
<string>30108******93</string>
<key>FacebookDisplayName</key>
<string>PhotoEarn</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>f860ce37***********8b7d1</string>
<string>kakaokompassauth</string>
<string>storykompassauth</string>
<string>kakaolink</string>
<string>kakaotalk-5.9.7</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
<string>NSMicrophoneUsageDescription</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app doesn't need access to location when in the background.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>NSPhotoLibraryAddUsageDescription</string>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDarkContent</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
我期待着大家的来信。谢谢。
答案 0 :(得分:0)
您没有将CFBundleLocalizations
添加到您的ios/Runner/Info.plist
文件中
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>ko</string>
</array>
添加文件后,请考虑运行flutter clean
,然后再次运行项目。