getString method
String
getString(
- String stringId, {
- OfferingPackages? offeringPackage,
- OfferingPackagesProduct? product,
- String? currentLocale,
Retrieves a localized string based on its ID and optionally injects dynamic product/package data.
Implementation
String getString(
String stringId, {
OfferingPackages? offeringPackage,
OfferingPackagesProduct? product,
String? currentLocale,
}) {
final effectiveLocale = currentLocale ?? _currentLocale;
final paywallString = _localizedStrings[effectiveLocale]?[stringId];
final isRTL = _rtlLocales[effectiveLocale] ?? false;
_debugLocalizationLog(
'Getting string for ID: $stringId, Locale: $effectiveLocale, Found: ${paywallString != null}',
);
_debugLocalizationLog(
'Available locales: ${_localizedStrings.keys.toList()}',
);
_debugLocalizationLog(
'Available strings in $effectiveLocale: ${_localizedStrings[effectiveLocale]?.keys.toList()}',
);
if (paywallString == null || paywallString.value == null) {
_debugLocalizationLog(
'Localization string not found for ID: $stringId in locale: $effectiveLocale',
);
return stringId; // Fallback to ID if string or its value not found
}
_debugLocalizationLog(
'String value type: ${paywallString.value.runtimeType}, Value: ${paywallString.value}',
);
// Handle the 'value' field which can be String or List<dynamic>
if (paywallString.value is String) {
return paywallString.value.toString();
} else if (paywallString.value is List) {
final List<dynamic> values = paywallString.value;
final StringBuffer buffer = StringBuffer();
_debugLocalizationLog('Processing list with ${values.length} items');
for (var item in values) {
_debugLocalizationLog(
'Processing item: $item (type: ${item.runtimeType})',
);
if (item is String) {
buffer.write(item);
} else if (item is Map<String, dynamic>) {
// Handle text objects with attributes
if (item.containsKey('text')) {
buffer.write(item['text'] ?? '');
_debugLocalizationLog('Added text: ${item['text']}');
} else {
// Handle tag objects
final String? tag = item['tag'];
_debugLocalizationLog('Processing tag: $tag');
switch (tag) {
case "PROD_TITLE":
final productName = product?.name ?? 'N/A';
buffer.write(productName);
_debugLocalizationLog('Added PROD_TITLE: $productName');
break;
case "PROD_PRICE":
// Find the price object, prioritizing IRR, then USD, then the first available
final priceObject = product?.prices?.firstWhereOrNull(
(p) => p.currency == 'IRR',
) ??
product?.prices?.firstWhereOrNull(
(p) => p.currency == 'USD',
) ??
product?.prices?.firstOrNull;
final price = priceObject?.price?.toString() ?? 'N/A';
final formattedPrice = _formatPrice(price, isRTL);
buffer.write(formattedPrice);
_debugLocalizationLog(
'Added PROD_PRICE: $formattedPrice (RTL: $isRTL)',
);
break;
case "PROD_CURRENCY":
// Find the price object, prioritizing IRR, then USD, then the first available
final priceObject = product?.prices?.firstWhereOrNull(
(p) => p.currency == 'IRR',
) ??
product?.prices?.firstWhereOrNull(
(p) => p.currency == 'USD',
) ??
product?.prices?.firstOrNull;
// Replace "IRR" with "تومان" for RTL
final String currencyText = priceObject?.currency ?? 'N/A';
final displayCurrency =
isRTL && currencyText == 'IRR' ? 'تومان' : currencyText;
buffer.write(displayCurrency);
_debugLocalizationLog(
'Added PROD_CURRENCY: $displayCurrency (RTL: $isRTL)',
);
break;
case "PACK_DESCRIPTION":
final description = offeringPackage?.description ?? 'N/A';
buffer.write(description);
_debugLocalizationLog('Added PACK_DESCRIPTION: $description');
break;
default:
_debugLocalizationLog(
'Unknown localization tag: $tag for string ID: $stringId',
);
buffer.write('[$tag]'); // Show unknown tags for debugging
break;
}
}
} else if (item == null) {
// Handle null values in the list
buffer.write('');
_debugLocalizationLog('Added null item');
} else {
_debugLocalizationLog(
'Unexpected type in localization string value list: ${item.runtimeType} for string ID: $stringId',
);
buffer.write(item.toString()); // Fallback for unexpected types
}
}
final result = buffer.toString();
_debugLocalizationLog('Final string result: "$result"');
return result;
}
_debugLocalizationLog(
'Unexpected localization string value type: ${paywallString.value.runtimeType} for string ID: $stringId',
);
return paywallString.value
.toString(); // Fallback for unexpected root value types
}