Get a locale fallback list to check while translating.

@param string $locale
@param string $fallbackLocale

@return array

@throws InvalidArgumentException
12474
public static function getLocaleFallbackList($locale, $fallbackLocale = '')
    {
        // Check arguments
        if (empty($locale)) {
            throw new InvalidArgumentException(__METHOD__ . ': The given locale is empty');
        }

        // Create a priority fallback list to check for different locale.
        // This list includes also generic language codes separated from locale in case
        // there is generic language fallback.
        // If the base language for current and fallback locale is the same, then the language code
        // goes in the end as final fallback.
        $fallbackList = [];

        // Get fallback for normal locale
        $fallbackList = array_merge($fallbackList, self::getFallbackList($locale));

        // Add fallback locale, if different
        if ($fallbackLocale != $locale) {
            $fallbackList = array_merge($fallbackList, self::getFallbackList($fallbackLocale));
        }

        return $fallbackList;
    }
958588
public function load(string $locale):array
    {
        $filename = "{$this->path}/{$locale}.php";
        try {
            $loaded = $this->files->getRequire($filename);
            if (!is_array($loaded)) {
                throw new InvalidResourceException();
            }

            return $loaded;
        } catch (FileNotFoundException $e) {
            throw new NotFoundResourceException("$filename not found.", 0, $e);
        } catch (\Throwable $e) {
            throw new InvalidResourceException("$filename has invalid resources.", 0, $e);
        }
    }
1108985
public function getLine($key, $replace = [], $locale = null, $fallback = true)
	{
		list($namespace, $group, $item) = $this->parseKey($key);

		// Here we will get the locale that should be used for the language line. If one
		// was not passed, we will use the default locales which was given to us when
		// the translator was instantiated. Then, we can load the lines and return.
		$locales = $fallback ? $this->localeArray($locale)
							 : [$locale ?: $this->locale];

		foreach ($locales as $locale) {
			if (! is_null($line = $this->read(
				$namespace, $group, $locale, $item, $replace
			))) {
				break;
			}
		}

		return isset($line) ? $line : null;
	}
988172
protected function getSingleCatalog($locale, $domain = null) {
		if (! isset($this->catalogs[$locale])) {
				 return [];
		}

		if ($domain === null) {
			return [$locale => $this->catalogs[$locale]];
		}

		if (is_string($domain)) {
			if (! isset($this->catalogs[$locale][$domain])) {
				return [];
			}

			return [
				$locale => [
					$domain => $this->catalogs[$locale][$domain]
				]
			];
		}

		if (is_array($domain)) {
			return [$locale => array_intersect_key(
				$this->catalogs[$locale],
				array_flip($domain))
			];
		}
		
		throw new InvalidParameter('Domain must be a string or an array');
	}
998724
public String validateFallbackLocale() {
        ULocale loc = ULocale.getDefault();
        if (loc != fallbackLocale) {
            synchronized (this) {
                if (loc != fallbackLocale) {
                    fallbackLocale = loc;
                    fallbackLocaleName = loc.getBaseName();
                    clearServiceCache();
                }
            }
        }
        return fallbackLocaleName;
    }
29455
public static function getDisplayLanguages($locale)
    {
        if (!isset(self::$languages[$locale])) {
            self::$languages[$locale] = Intl::getLanguageBundle()->getLanguageNames($locale);
        }

        return self::$languages[$locale];
    }
1232036
public function translate(string $locale = null, bool $fallback = true): ?Model
    {
        $locale = $locale ?: $this->getLocale();

        $translation = $this->getTranslation($locale);

        if (!$translation && $fallback) {
            $translation = $this->getTranslation($this->getFallback());
        }

        if (!$translation && !$fallback) {
            $translation = $this->getEmptyTranslation($locale);
        }

        return $translation;
    }
777456
protected function parseLocale($locale)
	{
		if ( ! is_null($locale))
		{
			return array_filter(array($locale, $this->fallback));
		}
		else
		{
			return array_filter(array($this->locale, $this->fallback));
		}
	}
1127827
public function getTranslation($locale)
    {
        /** @var TaxClassTranslation $translation */
        foreach ($this->getTranslations() as $translation) {
            if ($translation->getLocale() === $locale) {
                return $translation;
            }
        }

        return null;
    }
53234
public function findUsagesByLocales( $locale = null )
    {
        if ( empty( $locale ) )
        {
            $locale = $this->getLocale();
        }

        return $this->getMapper()
                    ->findUsagesByLocales( (array) $locale );
    }
401075
@Override
	public Locale getFallbackLocale(String baseName, Locale locale) {
		if (baseName == null) {
			throw new NullPointerException();
		}
		Locale defaultLocale = Locale.getDefault();

		if (!fallbackToSystemLocale) {
			defaultLocale = new Locale("", "");
		}

		return locale.equals(defaultLocale) ? null : defaultLocale;
	}
82000
public static function setFallbackLocale($locale)
    {
        $translator = static::getTranslator();

        if (method_exists($translator, 'setFallbackLocales')) {
            $translator->setFallbackLocales([$locale]);

            if ($translator instanceof Translator) {
                $preferredLocale = $translator->getLocale();
                $translator->setMessages($preferredLocale, array_replace_recursive(
                    $translator->getMessages()[$locale] ?? [],
                    Translator::get($locale)->getMessages()[$locale] ?? [],
                    $translator->getMessages($preferredLocale)
                ));
            }
        }
    }
33360
public function getFallbackLocale()
    {
        if (!$this->getLocalizedMeta()) {
            return;
        }

        $fallbackLocale = $this->getLocalizedMeta()->getLocale();

        return $fallbackLocale !== $this->locale ? $fallbackLocale : null;
    }
463269
public function get(string $key, string $locale = null)
    {
        list($country) = $this->parseKey($key);

        $locale = $locale ?? $this->fallbackLocale;

        $this->load($locale);

        if ($this->has($locale, $country)) {
            return $this->loaded[$locale][$country];
        }

        return $key;
    }
894828
public function translate($locale = null, $create = false)
    {
        $locale = $locale ?: $this->currentLocale;
        if (null === $locale) {
            throw new \RuntimeException('No locale has been set and current locale is undefined.');
        }

        if ($this->currentTranslation && $locale === $this->currentTranslation->getLocale()) {
            return $this->currentTranslation;
        }

        if (!$translation = $this->translations->get($locale)) {
            if ($create) {
                $className = $this->getTranslationClass();

                /** @var TranslationInterface $translation */
                $translation = new $className();
                $translation->setLocale($locale);

                $this->addTranslation($translation);
            } else {
                if (null === $this->fallbackLocale) {
                    throw new \RuntimeException('No fallback locale has been set.');
                }

                if (!$fallbackTranslation = $this->translations->get($this->getFallbackLocale())) {
                    $className = $this->getTranslationClass();

                    /** @var TranslationInterface $translation */
                    $translation = new $className();
                    $translation->setLocale($locale);

                    $this->addTranslation($translation);
                } else {
                    $translation = clone $fallbackTranslation;
                }
            }
        }

        $this->currentTranslation = $translation;
        $this->currentLocale      = $locale;

        return $translation;
    }
826476
protected function getLocale($locale)
    {
        $result = null;
        if ($locale instanceof LocaleEntity) {
            $result = $locale;
        } elseif (is_string($locale) && $locale !== '') {
            $result = $this->app->make(LocaleRepository::class)->findApproved($locale);
        }

        return $result;
    }
270834
function findFallbackLocale(locale) {
  const locales = getAvailableLocales();

  for (let i = locale.length - 1; i > 1; i -= 1) {
    const key = locale.substring(0, i);

    if (locales.includes(key)) {
      return key;
    }
  }

  return null;
}