PHPackages                             simpliko\_cz/wavevision\_namespace-translator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. simpliko\_cz/wavevision\_namespace-translator

ActiveLibrary

simpliko\_cz/wavevision\_namespace-translator
=============================================

Manage Nette app translations on per namespace basis.

1.0.10(8mo ago)01MITPHPPHP &gt;=7.4

Since May 7Pushed 8mo agoCompare

[ Source](https://github.com/SimplikoCZ/wavevision_namespace-translator)[ Packagist](https://packagist.org/packages/simpliko_cz/wavevision_namespace-translator)[ RSS](/packages/simpliko-cz-wavevision-namespace-translator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (13)Versions (4)Used By (0)

[![Wavevision s.r.o.](https://camo.githubusercontent.com/9479da35305d94b0244ac1c81fe283d0abb86fe217b2a815056165c67c0574e6/68747470733a2f2f77617665766973696f6e2e636f6d2f696d616765732f77617665766973696f6e2d6c6f676f2e706e67)](https://github.com/wavevision)

Namespace Translator
====================

[](#namespace-translator)

[![Build Status](https://camo.githubusercontent.com/3796fb931479a00432a534ff984a8726b3e6b46482c14ad826b34df434580b6c/68747470733a2f2f7472617669732d63692e6f72672f77617665766973696f6e2f6e616d6573706163652d7472616e736c61746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/wavevision/namespace-translator)[![Coverage Status](https://camo.githubusercontent.com/3a4b3cec6a8684a2ed7ddb27329207c37f4dcf740c0374e914da952fb5015192/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f77617665766973696f6e2f6e616d6573706163652d7472616e736c61746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/wavevision/namespace-translator?branch=master)[![PHPStan](https://camo.githubusercontent.com/027fd636e970cf392fd4478baf3261048b41955d2905a95fb47da6bf19b8640a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c652d6c6576656c2532306d61782d627269676874677265656e2e7376673f6c6162656c3d7068707374616e)](https://github.com/phpstan/phpstan)

Translations manager for Nette framework using [contributte/translation](https://github.com/contributte/translation). It allows you to have your translation files located where they are really used (e.g. next to a component or a model).

**No more global translations mess!** 💪

Installation
------------

[](#installation)

Via [Composer](https://getcomposer.org)

```
composer require wavevision/namespace-translator
```

> **Note**: This will automatically install `contributte/translation` too.

Usage
-----

[](#usage)

Register required extensions in your project config:

```
extensions:
	translation: Contributte\Translation\DI\TranslationExtension
	namespaceTranslator: Wavevision\NamespaceTranslator\DI\Extension
```

You can configure `namespaceTranslator` as follows *(default values)*:

```
namespaceTranslator:
    dirNames: # names of dirs in which namespace translations will reside
        - translations
        - Translations
    loaders: # namespace translations loaders
        neon: Wavevision\NamespaceTranslator\Loaders\Neon
        php: Wavevision\NamespaceTranslator\Loaders\TranslationClass
        flatJson: Wavevision\NamespaceTranslator\Loaders\FlatJson
```

> **Note:** Refer to [Contributte docs](https://contributte.org/packages/contributte/translation.html#configuration)for further info about configuring `translation`.

With this setup, you can start managing your translations like a boss 🤵.

The best thing is the translator keeps full backwards compatibility with `contributte/translation` setup, so you can still use your translations as you are used to and migrate to namespaces step-by-step. Any translation not found by namespace translator will fallback to `translation` resources.

### Translated components

[](#translated-components)

Your components (or presenters) can use `Wavevision\NamespaceTranslator\TranslatedComponent` trait.

**Make sure your component has `inject` allowed.**

The trait will provide your component class / template with `$translator` property / variable. The translator will look for resources in configured dir names inside component's namespace.

> **Note**: The `translate` macro in component templates will, of course, work too.

### Translated models

[](#translated-models)

Even your services can use the translator. Simply use `Wavevision\NamespaceTranslator\NamespaceTranslator`.

**Make sure your service is registered with `inject: true` in your config.**

After that, it works the same as with your components.

Loaders
-------

[](#loaders)

There are three resource loaders included by default:

- [Neon](./src/NamespaceTranslator/Loaders/Neon.php) – loads translations from `neon` files
- [TranslationClass](./src/NamespaceTranslator/Loaders/TranslationClass.php) – loads translations from PHP classes
- [FlatJson](./src/NamespaceTranslator/Loaders/FlatJson.php) - loads translations from flat (no nesting) `json` files

Using PHP classes is useful when you want to refer to your translations using constants so changes in your resources get propagated throughout the whole project.

**Classes containing translations must implement `Wavevision\NamespaceTranslator\Resources\Translation`.**

You can also create and register your own loader, just make sure it implements `Wavevision\NamespaceTranslator\Loaders\Loader`.

Export \\ Import
----------------

[](#export--import)

For exporting \\ importing translations to \\ from CSV or GoogleSheet (or both) update configuration file

```
namespaceTranslator:
	transfer:
		google:
			credentials: credentials.json
			sheetId: googleSheetId
			parts:
				- directory: %vendorDir%/../App/AdminModule
				  tabName: admin-module
		csv:
			parts:
				- directory: %vendorDir%/../App/FrontModule
				  filename: %vendorDir%/../temp/front-module.csv
```

Set locales whitelist from [contributte/translation](https://contributte.org/packages/contributte/translation.html#configuration). Whitelist is used for creating translation columns in export.

Run command to export translations

```
php {bin/console} namespace-translator:export
```

Update translations, then run command to import them

```
php {bin/console} namespace-translator:import
```

### Google Sheet

[](#google-sheet)

For accessing Google sheet you will need [server-to-server API key](https://developers.google.com/sheets/api/guides/authorizing?authuser=1#APIKey) and [sheet ID](https://support.asinzen.com/article/516-how-do-i-get-my-google-spreadsheet-id).

### Export example

[](#export-example)

From files

```
# file translations/en.neon
hello: Hello %name%
```

```
# file translations/de.neon
hello: Hallo %name%
```

```
