cache = $container->get(Cache::class); } public function get($key, array $replace = [], $locale = null, $fallback = true): array|string { $locale = $locale ?: $this->getLocale(); $cacheKey = "lang:{$locale}:{$key}"; // 先查缓存 $cached = $this->cache->get($cacheKey); if ($cached !== null) { return $this->makeReplacements($cached, $replace); } // 查数据库 $value = tModel::getLangValue($locale, $key); if ($value) { $this->cache->set($cacheKey, $value, (int)config("translation.ttl")); return $this->makeReplacements($value, $replace); } // 文件翻译 $fallbackValue = parent::get($key, $replace, $locale, $fallback); // fallback 结果也缓存 $this->cache->set($cacheKey, $fallbackValue, (int)config("translation.ttl")); return $fallbackValue; } }