/** * Execute the command. * * @param Filesystem $fileSystem * @param Application $app * @param TranslationRepositoryInterface $repository * @param Dispatcher $event * @return Collection of Group */ public function handle(Filesystem $fileSystem, Application $app, TranslationRepositoryInterface $repository, Dispatcher $event) { $files = $fileSystem->allFiles($app->langPath()); /** * Retrieves all local languages */ $languages = collect($files)->transform(function ($file) { return $file->getRelativePath(); })->unique(); /** * Save Database instance with all languages */ $database = $repository->languages(); /** * List Only names */ $names = $database->pluck('name'); /** * Create New Language for those which has been set locally * but was not present yet on the database */ $newLanguages = $languages->merge($names)->diff($names)->map(function ($name) { return $this->dispatch(new CreateLanguageCommand($name)); }); /** * Announce LanguagesWasCreated */ if (!$newLanguages->isEmpty()) { $event->fire(new LanguagesWasCreated($newLanguages)); } /** * Returns All languages */ return $database->merge($newLanguages); }
/** * Display a listing of the resource. * * @param string $language * @param string $group * @return \Illuminate\Http\Response */ public function index($language = '1', $group = '1') { $translations = $this->repository->fetch($language, $group)->load('groups', 'language'); $groups = $this->repository->groups(); $languages = $this->repository->languages(); return view('translation.index', compact('translations', 'groups', 'languages')); }
/** * Execute the command. * * @param TranslationRepositoryInterface $repository * @param Filesystem $fileSystem * @param Application $app */ public function handle(TranslationRepositoryInterface $repository, Filesystem $fileSystem, Application $app) { /** @var Collection $translations */ $translations = $repository->all()->load('groups', 'language'); /** * Execute operation for each language within the database */ $translations->groupBy('language.name')->map(function ($translations, $language) use($app, $fileSystem) { /** * Divide into groups */ $groups = $translations->map(function ($translation) { return $translation->groups->pluck('name'); })->collapse(); /** * For each group create a separated file */ $groups->each(function ($group) use($translations, $language, $app, $fileSystem) { /** * Filter Trough to get only translations that belongs to this group */ $content = $translations->filter(function ($translation) use($group) { return !$translation->groups->where('name', $group)->isEmpty(); })->lists('value', 'key')->toArray(); /** * Generate popper output */ $output = "<?php\n\nreturn " . var_export($content, true) . ";\n"; $directory = $app->langPath() . '/' . $language; $file = $directory . '/' . $group . '.php'; /** * if Directory doesnt exist then create it */ if (!$fileSystem->exists($directory)) { $fileSystem->makeDirectory($app->langPath() . '/' . $language); } /** * if File already exist delete it. */ if ($fileSystem->exists($file)) { $fileSystem->delete($file); } /** * Write file */ $fileSystem->put($file, $output); }); }); }
/** * Execute the command. * * @param TranslationRepositoryInterface $repository */ public function handle(TranslationRepositoryInterface $repository) { /** * Retrieve all languages for further usage */ $allLanguages = $repository->languages(); /** * Get All Groups */ $groups = $repository->groups()->load('translations', 'translations.language'); /** * For Each Group Execute Operation */ $groups->map(function ($group) use($allLanguages) { /** * Get All Languages Contained within this Group */ $languages = $group->translations->groupBy('language.id'); /** * Check if all languages are contained within $languages, otherwise append it * with an empty collection so it will generate all keys, values for this language * as it means the language does not have any translation at the moment */ $missingLanguages = collect($allLanguages->keyBy('id')->keys())->diff($languages->keys()); $missingLanguages->each(function ($language) use($languages) { $languages->put($language, collect()); }); /** * Merge All Values in order to get uniques */ $merged = $languages->map(function ($translations) { return $translations->lists('key'); })->collapse(); /** * Now for each language extract the merged groups in order to get new keys */ $languages->each(function ($translations, $language) use($merged, $group) { $newValues = collect($merged)->diff($translations->lists('key')); /** * For each new Value, Create a new Translation */ $newValues->each(function ($key) use($language, $group) { $this->dispatch(new CreateTranslationCommand($language, $group->id, compact('key'))); }); }); }); }
/** * Execute the command. * * @param Filesystem $fileSystem * @param Application $app * @param Translator $translator * @param TranslationRepositoryInterface $repository * @param Dispatcher $event */ public function handle(Filesystem $fileSystem, Application $app, Translator $translator, TranslationRepositoryInterface $repository, Dispatcher $event) { $files = $fileSystem->allFiles($app->langPath()); $loader = $translator->getLoader(); /** @var Collection $groups */ $groups = $this->dispatch(new ImportGroupsCommand()); /** @var Collection $languages */ $languages = $this->dispatch(new ImportLanguagesCommand()); foreach ($files as $file) { /** @var Language $language */ $language = $languages->where('name', $file->getRelativePath())->first(); /** @var Group $group */ $group = $groups->where('name', pathinfo($file)['filename'])->first(); /** @var Collection $translations */ $translations = collect($loader->load($language->name, $group->name)); /** * Convert all arrays to to json */ $translations->transform(function ($value) { return is_array($value) ? json_encode($value) : $value; }); /** * Merge Translations on the database with local translations */ $model = $repository->fetch($language->id, $group->id); $database = $model->lists('value', 'key'); /** * If translations is an array, process the JSON data and update it */ $translations->each(function ($data, $key) use($database, $model) { /** * If it's not a json then there is nothing to update */ if ($model->isEmpty() or !$this->isJSON($data)) { return; } /** * Retrieve Json */ $decoded = $database->map(function ($value) { return json_decode($value, true); }); $value = collect(json_decode($data, true))->merge($decoded->get($key))->toJson(); /** * Update Translation */ $updateCommand = new UpdateTranslationCommand($model->where('key', $key)->first()->id, compact('value')); $this->dispatch($updateCommand); }); /** * First get only new values that are not already on the DB * Then for each save new Translation on DB * Save Translations on database */ $newItems = $translations->merge($database)->diff($database)->map(function ($value, $key) use($language, $group) { return $this->dispatch(new CreateTranslationCommand($language->id, $group->id, compact('key', 'value'))); }); /** * Announce Translations was created */ if (!$newItems->isEmpty()) { $event->fire(new TranslationsWasCreated($newItems)); } } }
/** * Execute the command. * * @param TranslationRepositoryInterface $repository * @return Language */ public function handle(TranslationRepositoryInterface $repository) { return $repository->createTranslation($this->language_id, $this->group_id, $this->translation); }
/** * Execute the command. * * @param TranslationRepositoryInterface $repository */ public function handle(TranslationRepositoryInterface $repository) { return $repository->update($this->translation_id, $this->fields); }
/** * Execute the command. * * @param TranslationRepositoryInterface $repository * @return \DreamsArk\Models\Translation\Language */ public function handle(TranslationRepositoryInterface $repository) { return $repository->createGroup($this->name); }