/** * 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 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); }
public function exportTranslations($group) { if ($group == '*') { return $this->exportAllTranslations(); } $g = Group::find($group); if (!in_array($g->label, $this->config['exclude_groups'])) { $tree = $this->makeTree(Translation::where('group_id', $g->id)->whereNotNull('value')->get()); foreach ($tree as $locale => $groups) { $filename = $g->label; if (isset($groups[$g->label])) { if (str_contains($g->label, '/')) { $groupPath = $g->label; $filename = basename($groupPath); $folder = dirname($groupPath); $directoryPath = $this->app->langPath() . '/' . $locale . '/' . $folder; if (!$this->files->exists($directoryPath)) { $this->files->makeDirectory($directoryPath, 493, true, true); } $path = $this->app->langPath() . "/{$locale}/{$folder}/{$filename}.php"; } else { $path = $this->app->langPath() . "/{$locale}/{$filename}.php"; } $translations = $groups[$g->label]; $path = $this->app->langPath() . '/' . $locale . '/' . $g->label . '.php'; $output = "<?php\n\nreturn " . var_export($translations, true) . ";\n"; $this->files->put($path, $output); } } Translation::where('group_id', $g->id)->whereNotNull('value')->update(array('status' => Translation::STATUS_SAVED)); } }
public function addLocale($locale) { $localeDir = $this->app->langPath() . '/' . $locale; $this->ignoreLocales = array_diff($this->ignoreLocales, [$locale]); $this->saveIgnoredLocales(); $this->ignoreLocales = $this->getIgnoredLocales(); if (!$this->files->exists($localeDir) || !$this->files->isDirectory($localeDir)) { return $this->files->makeDirectory($localeDir); } return true; }
protected function getPath($group, $locale) { // Is module? if (strpos($group, '::')) { $parts = explode('::', $group); $path = $this->app->path() . '/Modules/' . ucfirst($parts[0]) . '/lang/' . $locale . '/' . $parts[1] . '.php'; } else { $path = $this->app->langPath() . '/' . $locale . '/' . $group . '.php'; } return $path; }
public function getLocales() { if (empty($this->locales)) { $locales = array_merge([config('app.locale')], Translation::groupBy('locale')->lists('locale')->all()); foreach ($this->files->directories($this->app->langPath()) as $localeDir) { $locales[] = $this->files->name($localeDir); } $this->locales = array_unique($locales); sort($this->locales); } return $this->locales; }
public function exportTranslations($group) { if (!in_array($group, $this->config['exclude_groups'])) { if ($group == '*') { return $this->exportAllTranslations(); } $tree = $this->makeTree(Translation::where('group', $group)->whereNotNull('value')->get()); foreach ($tree as $locale => $groups) { if (isset($groups[$group])) { $translations = $groups[$group]; $path = $this->app->langPath() . '/' . $locale . '/' . $group . '.php'; $output = "<?php\n\nreturn " . var_export($translations, true) . ";\n"; $this->files->put($path, $output); } } Translation::where('group', $group)->whereNotNull('value')->update(array('status' => Translation::STATUS_SAVED)); } }
/** * Get the path to the language files. * * @return string * @static */ public static function langPath() { return \Illuminate\Foundation\Application::langPath(); }
public function exportTranslations($group, $recursing = 0) { // TODO: clean up this recursion crap // this can come from the command line $ltm_translations = $this->getTranslationsTableName(); if (!$recursing) { $this->clearErrors(); $group = self::fixGroup($group); } if ($group && $group !== '*') { $this->getConnection()->affectingStatement("DELETE FROM {$ltm_translations} WHERE is_deleted = 1"); } elseif (!$recursing) { $this->getConnection()->affectingStatement("DELETE FROM {$ltm_translations} WHERE is_deleted = 1 AND `group` = ?", [$group]); } $inDatabasePublishing = $this->inDatabasePublishing(); if ($inDatabasePublishing < 3 && $inDatabasePublishing && ($inDatabasePublishing < 2 || !$recursing)) { if ($group && $group !== '*') { $this->getConnection()->affectingStatement(<<<SQL UPDATE {$ltm_translations} SET saved_value = value, status = ? WHERE (saved_value <> value || status <> ?) AND `group` = ? SQL , [Translation::STATUS_SAVED_CACHED, Translation::STATUS_SAVED, $group]); $translations = $this->translation->query()->where('status', '<>', Translation::STATUS_SAVED)->where('group', '=', $group)->get(['group', 'key', 'locale', 'saved_value']); } else { $this->getConnection()->affectingStatement(<<<SQL UPDATE {$ltm_translations} SET saved_value = value, status = ? WHERE (saved_value <> value || status <> ?) SQL , [Translation::STATUS_SAVED_CACHED, Translation::STATUS_SAVED]); $translations = $this->translation->query()->where('status', '<>', Translation::STATUS_SAVED)->get(['group', 'key', 'locale', 'saved_value']); } /* @var $translations Collection */ $this->clearCache($group); $this->clearUsageCache(false, $group); $translations->each(function ($tr) { $this->cacheTranslation($tr->group . '.' . $tr->key, $tr->saved_value, $tr->locale); }); } if (!$inDatabasePublishing || $inDatabasePublishing === 2 || $inDatabasePublishing === 3) { if (!in_array($group, $this->config(self::EXCLUDE_GROUPS_KEY))) { if ($group == '*') { $this->exportAllTranslations(1); } if ($inDatabasePublishing !== 3) { $this->clearCache($group); $this->clearUsageCache(false, $group); } $tree = $this->makeTree($this->translation->where('group', $group)->whereNotNull('value')->orderby('key')->get()); $configRewriter = new TranslationFileRewriter(); $exportOptions = array_key_exists('export_format', $this->config()) ? TranslationFileRewriter::optionFlags($this->config('export_format')) : null; // Laravel 5.1 $base_path = $this->app->basePath(); $pathTemplateResolver = new PathTemplateResolver($this->files, $base_path, $this->config('language_dirs'), '5'); $zipRoot = $base_path . $this->config('zip_root', mb_substr($this->app->langPath(), 0, -4)); // Laravel 4.2 //$base_path = base_path(); //$pathTemplateResolver = new PathTemplateResolver($this->files, $base_path, $this->config('language_dirs'), '4'); //$zipRoot = $base_path . $this->config('zip_root', mb_substr($this->app->make('path').'/lang', 0, -4)); if (mb_substr($zipRoot, -1) === '/') { $zipRoot = substr($zipRoot, 0, -1); } foreach ($tree as $locale => $groups) { if (isset($groups[$group])) { $translations = $groups[$group]; // use the new path mapping $computedPath = $base_path . $pathTemplateResolver->groupFilePath($group, $locale); $path = $computedPath; if ($path) { $configRewriter->parseSource($this->files->exists($path) ? $this->files->get($path) : ''); $output = $configRewriter->formatForExport($translations, $exportOptions); if ($this->zipExporting) { $pathPrefix = mb_substr($path, 0, mb_strlen($zipRoot)); $filePathName = $pathPrefix === $zipRoot ? mb_substr($path, mb_strlen($zipRoot)) : $path; //$this->makeDirPath($filePathName); $this->zipExporting->addFromString($filePathName, $output); } else { try { $this->makeDirPath($path); if (($result = $this->files->put($path, $output)) === false) { $this->errors[] = "Failed to write to {$path}"; } } catch (Exception $e) { $this->errors[] = $e->getMessage(); } } } } } if (!$inDatabasePublishing) { $this->translation->where('group', $group)->update(array('status' => Translation::STATUS_SAVED, 'saved_value' => new Expression('value'))); } } } }
/** * 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)); } } }
/** * Register the aliases from this module. */ protected function registerTranslation() { $langPath = $this->app->langPath() . '/' . $this->getPathName(); $this->loadTranslationsFrom(is_dir($langPath) ? $langPath : $this->manifest->getPath() . '/Resources/lang', $this->getSlugName()); }