Files
react-native/React/Fabric/RCTLocalizationProvider.mm
T
Rob Hogan 18196512db Fall back to non-localized string if no translation is available
Summary:
We've seen a couple of `EXC_BAD_ACCESS` crashes in [`RCTParagraphComponentAccessibilityProvider.mm:L125`](https://github.com/facebook/react-native/blob/52d8a797e7a6be3fa472f323ceca4814a28ef596/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentAccessibilityProvider.mm#L125), most likely explained by [RCTLocalizationProvider RCTLocalizedString] returning nil, which will happen in the case that a language pack has no entry for the input string.

This is a small defensive change to fall back to the input if there's no better alternative.

Changelog:
[iOS][Fixed] - `RCTLocalizationProvider` Fall back to input when no localization is available

Reviewed By: luluonet

Differential Revision: D35583786

fbshipit-source-id: e8ae6ff61518e105301e7e51f5d8f43290fb20bf
2022-04-19 04:10:31 -07:00

47 lines
1.0 KiB
Plaintext

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTLocalizationProvider.h"
#import <Foundation/Foundation.h>
static id<RCTLocalizationProtocol> _delegate = nil;
static NSDictionary<NSString *, NSString *> *_languagePack = nil;
void setLocalizationDelegate(id<RCTLocalizationProtocol> delegate)
{
_delegate = delegate;
}
void setLocalizationLanguagePack(NSDictionary<NSString *, NSString *> *pack)
{
_languagePack = pack;
}
@implementation RCTLocalizationProvider
+ (NSString *)RCTLocalizedString:(NSString *)oldString withDescription:(NSString *)description
{
NSString *candidate = nil;
if (_delegate != nil) {
candidate = [_delegate localizedString:oldString withDescription:description];
}
if (candidate == nil && _languagePack != nil) {
candidate = _languagePack[oldString];
}
if (candidate == nil) {
candidate = oldString;
}
return candidate;
}
@end