Skip to content

Commit

Permalink
fix: 4165 - new cases of default language for OCR (#4227)
Browse files Browse the repository at this point in the history
Impacted files:
* `add_basic_details_page.dart`: added parameters for `init` method.
* `edit_ocr_page.dart`: added parameters for `init` method.
* `multilingual_helper.dart`: added parameters for `init` method, that helps us compute the default language
  • Loading branch information
monsieurtanuki authored Jun 24, 2023
1 parent 57eff45 commit dc5806a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> {
_multilingualHelper.init(
multilingualTexts: _product.productNameInLanguages,
monolingualText: _product.productName,
productLanguage: _product.lang,
);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/smooth_app/lib/pages/product/edit_ocr_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class _EditOcrPageState extends State<EditOcrPage> {
_multilingualHelper.init(
multilingualTexts: _helper.getMultilingualTexts(widget.product),
monolingualText: _helper.getMonolingualText(widget.product),
selectedImages: widget.product.selectedImages,
imageField: _helper.getImageField(),
productLanguage: widget.product.lang,
);
}

Expand Down
78 changes: 58 additions & 20 deletions packages/smooth_app/lib/pages/product/multilingual_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,83 @@ class MultilingualHelper {
final Map<OpenFoodFactsLanguage, String> _currentMultilingualTexts =
<OpenFoodFactsLanguage, String>{};

/// Init of the translations.
///
/// Here we try our best to find translations and a default language.
void init({
required final Map<OpenFoodFactsLanguage, String>? multilingualTexts,
required final String? monolingualText,
final List<ProductImage>? selectedImages,
final ImageField? imageField,
OpenFoodFactsLanguage? productLanguage,
}) {
void setMultilingualText(
final OpenFoodFactsLanguage language,
final String text,
) =>
_initialMultilingualTexts[language] =
_currentMultilingualTexts[language] = text;

_initialMonolingualText = monolingualText;

// potentially text for all localized images.
if (selectedImages != null && imageField != null) {
for (final ProductImage selectedImage in selectedImages) {
if (selectedImage.field == imageField &&
selectedImage.language != null) {
setMultilingualText(selectedImage.language!, '');
}
}
}

// checking if we use the multilingual version...
if (multilingualTexts != null) {
for (final OpenFoodFactsLanguage language in multilingualTexts.keys) {
final String name = getCleanText(multilingualTexts[language]);
if (name.isNotEmpty) {
_initialMultilingualTexts[language] = name;
_currentMultilingualTexts[language] = name;
setMultilingualText(language, name);
}
}
if (_initialMultilingualTexts.isNotEmpty) {
// fallback
_currentLanguage = _initialMultilingualTexts.keys.first;
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
if (_initialMultilingualTexts.containsKey(language)) {
// best choice: the current app language
_currentLanguage = language;
} else {
// second best choice: the same language as the "main" text
for (final MapEntry<OpenFoodFactsLanguage, String> entry
in _initialMultilingualTexts.entries) {
if (entry.value == _initialMonolingualText) {
_currentLanguage = entry.key;
}
}

// trying to set the language, from multilingual texts
if (_initialMultilingualTexts.isNotEmpty) {
// fallback
_currentLanguage = _initialMultilingualTexts.keys.first;
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
if (_initialMultilingualTexts.containsKey(language)) {
// best choice: the current app language
_currentLanguage = language;
} else {
// second best choice: the same language as the "main" text
for (final MapEntry<OpenFoodFactsLanguage, String> entry
in _initialMultilingualTexts.entries) {
if (entry.value == _initialMonolingualText) {
_currentLanguage = entry.key;
}
}
controller.text = _initialMultilingualTexts[_currentLanguage] ?? '';
}

controller.text = _initialMultilingualTexts[_currentLanguage] ?? '';
return;
}
// Fallback: we may have old data where there are no translations.
if (_initialMultilingualTexts.isEmpty) {
controller.text = _initialMonolingualText ?? '';

if (_initialMonolingualText == null) {
// no initial text but at least we will have a language selector.
if (productLanguage == OpenFoodFactsLanguage.UNDEFINED) {
productLanguage = null;
}
_currentLanguage = productLanguage ?? ProductQuery.getLanguage();
setMultilingualText(_currentLanguage, '');
controller.text = '';
return;
}

// Fallback: we may have old data where there are no translations.
controller.text = _initialMonolingualText!;
}

// TODO(monsieurtanuki): we would be better off always never monolingual
bool isMonolingual() => _initialMultilingualTexts.isEmpty;

Widget getLanguageSelector(void Function(void Function()) setState) =>
Expand Down

0 comments on commit dc5806a

Please sign in to comment.