imsujan276 / shopify_flutter

A flutter package that works as a bridge between your Shopify Store and Flutter Application
https://pub.dev/packages/shopify_flutter
MIT License
26 stars 27 forks source link

Added localization to PriceV2 #37

Closed vptcnt closed 10 months ago

vptcnt commented 10 months ago

Hello @imsujan276

I have updated PriceV2 to use the localization of NumberFormat. It allows displaying correctly the currency symbol before or after the price according to the locale.

/lib/models/src/product/price_v_2/price_v_2.dart

@freezed
class PriceV2 with _$PriceV2 {

  [...]

  String get formattedPrice => JsonHelper.chooseRightOrderOnCurrencySymbol(
        amount,
        currencyCode,
        priceFormat: priceFormat,
      );

  String formattedPriceWithLocale(String? locale) =>
      JsonHelper.chooseRightOrderOnCurrencySymbol(
        amount,
        currencyCode,
        priceFormat: priceFormat,
        locale: locale,
      );
}

I have to add the optional parameter to a json helpe in /lib/models/json_helper.dart

static String chooseRightOrderOnCurrencySymbol(
    dynamic amount,
    String currencyCode, {
    NumberFormat? priceFormat,
    String? locale,  // NEW
  }) {
    return NumberFormat.currency(
      locale: locale,
      name: currencyCode, // NEW
      symbol: '${_simpleCurrencySymbols[currencyCode]}',
    ).format(amountFromJson(amount));
  }

and then added two functions at the model Product th /lib/models/src/product/product.dart


@freezed
@JsonSerializable()
class Product with _$Product {

  [...]

  String formattedPriceWithLocale(String? locale) => productVariants.isEmpty
      ? ''
      : productVariants.first.price.formattedPriceWithLocale(locale);

  String compareAtPriceFormattedWithLocale(String? locale) => productVariants.isEmpty
      ? ''
      : (productVariants.first.compareAtPrice == null
          ? ''
          : productVariants.first.compareAtPrice!.formattedPriceWithLocale(locale));

  [...]

}

Vince

OliverMoller commented 10 months ago

@vptcnt How do i install this on my app?

vptcnt commented 10 months ago

@imsujan276 need to add the code to the plugin

imsujan276 commented 10 months ago

Thank you @vptcnt for this. It helps stores to display the price format according to their locale.

It will be available in v1.5.6