diff --git a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart index dd1e85d8f63..d04d27d5f01 100644 --- a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart +++ b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart @@ -59,6 +59,7 @@ class _AddBasicDetailsPageState extends State { _multilingualHelper.init( multilingualTexts: _product.productNameInLanguages, monolingualText: _product.productName, + productLanguage: _product.lang, ); } diff --git a/packages/smooth_app/lib/pages/product/edit_ocr_page.dart b/packages/smooth_app/lib/pages/product/edit_ocr_page.dart index b7d8d361de0..b6f9d304eb6 100644 --- a/packages/smooth_app/lib/pages/product/edit_ocr_page.dart +++ b/packages/smooth_app/lib/pages/product/edit_ocr_page.dart @@ -61,6 +61,9 @@ class _EditOcrPageState extends State { _multilingualHelper.init( multilingualTexts: _helper.getMultilingualTexts(widget.product), monolingualText: _helper.getMonolingualText(widget.product), + selectedImages: widget.product.selectedImages, + imageField: _helper.getImageField(), + productLanguage: widget.product.lang, ); } diff --git a/packages/smooth_app/lib/pages/product/multilingual_helper.dart b/packages/smooth_app/lib/pages/product/multilingual_helper.dart index 2e02fcecb91..09072466c01 100644 --- a/packages/smooth_app/lib/pages/product/multilingual_helper.dart +++ b/packages/smooth_app/lib/pages/product/multilingual_helper.dart @@ -30,45 +30,83 @@ class MultilingualHelper { final Map _currentMultilingualTexts = {}; + /// Init of the translations. + /// + /// Here we try our best to find translations and a default language. void init({ required final Map? multilingualTexts, required final String? monolingualText, + final List? 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 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 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) =>