import 'package:flutter/material.dart';

import 'package:pshared/config/constants.dart';


class LocaleProvider with ChangeNotifier {
  Locale _locale = Constants.defaultLocale;

  Locale stringToLocale(String localeString) {
    var parts = localeString.split(RegExp(r'[-_]'));
    return (parts.length > 1) ? Locale(parts[0], parts[1]) : Locale(parts[0]);
  }

  LocaleProvider(String? localeCode) {
    if (localeCode != null) {
      _locale = stringToLocale(localeCode);
    }
  }

  Locale get locale => _locale;

  void setLocale(Locale locale) {
    if (_locale == locale) return;

    _locale = locale;
    notifyListeners();
  }
}

