IntlLocale.path constructor

IntlLocale.path(
  1. String path
)

Instantiate parsing a path and finding the locale code in it.

Implementation

factory IntlLocale.path(String path) {
  var idx = path.lastIndexOf('_');

  if (idx >= 0) {
    if (idx < 2) throw ArgumentError("Can't find locale delimiter in path");

    var lang = path.substring(idx - 2, idx);
    var reg = path.substring(idx + 1);

    if (reg.length > 3) reg = reg.substring(0, 3);
    if (reg.length == 3 &&
        !RegExp(r'[a-zA-Z]').hasMatch(reg.substring(2, 3))) {
      reg = reg.substring(0, 2);
    }

    return IntlLocale.langReg(lang, reg);
  }

  idx = path.lastIndexOf('.');

  if (idx > 2) {
    var lang = path.substring(idx - 2, idx);

    if (!RegExp(r'[a-zA-Z]').hasMatch(lang)) {
      throw ArgumentError("Can't find locale part with only language on it.");
    }

    return IntlLocale(lang);
  }

  throw ArgumentError("Can't find locale part in path");
}