{"action":"closed","issue":{"url":"https://api.github.com/repos/dimsav/laravel-translatable/issues/116","labels_url":"https://api.github.com/repos/dimsav/laravel-translatable/issues/116/labels{/name}","comments_url":"https://api.github.com/repos/dimsav/laravel-translatable/issues/116/comments","events_url":"https://api.github.com/repos/dimsav/laravel-translatable/issues/116/events","html_url":"https://github.com/dimsav/laravel-translatable/issues/116","id":71616245,"number":116,"title":"Use fake attribute names to act like actual attributes?","user":{"login":"barryvdh","id":973269,"avatar_url":"https://avatars.githubusercontent.com/u/973269?v=3","gravatar_id":"","url":"https://api.github.com/users/barryvdh","html_url":"https://github.com/barryvdh","followers_url":"https://api.github.com/users/barryvdh/followers","following_url":"https://api.github.com/users/barryvdh/following{/other_user}","gists_url":"https://api.github.com/users/barryvdh/gists{/gist_id}","starred_url":"https://api.github.com/users/barryvdh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/barryvdh/subscriptions","organizations_url":"https://api.github.com/users/barryvdh/orgs","repos_url":"https://api.github.com/users/barryvdh/repos","events_url":"https://api.github.com/users/barryvdh/events{/privacy}","received_events_url":"https://api.github.com/users/barryvdh/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-04-28T15:09:04Z","updated_at":"2015-04-30T09:45:17Z","closed_at":"2015-04-30T09:45:17Z","body":"I'd like to automatically get/set some of my translations in an admin interface, which just use the field names to create the form and fill the attributes after posting.\r\n\r\nI'm not sure what the best way is to handle those translations. I was thinking about use a accessor/mutator to get the translations automatically. \r\n\r\nFor example, a `name` attribute has 2 translations, `en` and `nl`. Instead of `$country->translate('en')->name`, could we use `$country->{'name:en'}`? (I chose the colon as example, as it's probably an unlikely token in a database)\r\n\r\nRough implementation would be to simply split the name on the token to detect the language:\r\n\r\n```php\r\npublic function getAttribute($key)\r\n{\r\n    if (str_contains($key, ':')) {\r\n        list($key, $locale) = explode(':', $key);\r\n    } else {\r\n        $locale = App::getLocale();\r\n    }\r\n\r\n    if ($this->isTranslationAttribute($key)) {\r\n        if ($this->getTranslation($locale) === null) {\r\n            return;\r\n        }\r\n\r\n        return $this->getTranslation($locale)->$key;\r\n    }\r\n\r\n    return parent::getAttribute($key);\r\n}\r\n\r\npublic function setAttribute($key, $value)\r\n{\r\n    if (str_contains($key, ':')) {\r\n        list($key, $locale) = explode(':', $key);\r\n    } else {\r\n        $locale = App::getLocale();\r\n    }\r\n\r\n    if (in_array($key, $this->translatedAttributes)) {\r\n        $this->getTranslationOrNew($locale)->$key = $value;\r\n    } else {\r\n        parent::setAttribute($key, $value);\r\n    }\r\n}\r\n```\r\n"}}