{"id":15282378,"url":"https://github.com/yidas/yii2-language","last_synced_at":"2025-05-12T19:41:47.173Z","repository":{"id":57086817,"uuid":"130453868","full_name":"yidas/yii2-language","owner":"yidas","description":"Yii 2 Framework Language extension with Status Keep and Mapping","archived":false,"fork":false,"pushed_at":"2018-11-01T07:02:29.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T02:48:25.370Z","etag":null,"topics":["language","language-maps","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-21T08:13:50.000Z","updated_at":"2020-12-04T16:32:37.000Z","dependencies_parsed_at":"2022-08-24T22:50:31.809Z","dependency_job_id":null,"html_url":"https://github.com/yidas/yii2-language","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fyii2-language","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fyii2-language/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fyii2-language/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fyii2-language/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidas","download_url":"https://codeload.github.com/yidas/yii2-language/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253809398,"owners_count":21967719,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["language","language-maps","yii2","yii2-extension"],"created_at":"2024-09-30T14:25:26.441Z","updated_at":"2025-05-12T19:41:47.127Z","avatar_url":"https://github.com/yidas.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars0.githubusercontent.com/u/993323\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eYii 2 Language Extension\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nYii 2 Framework Language extension with Status Keep and Mapping\n\n[![Latest Stable Version](https://poser.pugx.org/yidas/yii2-language/v/stable?format=flat-square)](https://packagist.org/packages/yidas/yii2-language)\n[![Latest Unstable Version](https://poser.pugx.org/yidas/yii2-language/v/unstable?format=flat-square)](https://packagist.org/packages/yidas/yii2-language)\n[![License](https://poser.pugx.org/yidas/yii2-language/license?format=flat-square)](https://packagist.org/packages/yidas/yii2-language)\n\nFEATURES\n--------\n\n- ***language Mapping** integrated with Yii2 Language*\n\n- ***Session \u0026 Cookie** storage support*\n\n- ***Yii2 i18n** support*\n\n---\n\nOUTLINE\n-------\n\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [Configuration](#configuration)\n* [Usage](#usage)\n    - [get()](#get)\n    - [set()](#set)\n    - [getbymap()](#getbymap)\n    - [setbymap()](#setbymap)\n    - [isFirstCome()](#isfirstcome)\n* [Implementation](implementation)\n    - [Controller for Changing Language](#controller-for-changing-language)\n    - [BeforeAction for globally changing language](#beforeaction-for-globally-changing-language)\n    \n---\n\nREQUIREMENTS\n------------\n\nThis library requires the following:\n\n- PHP 5.4.0+\n- Yii 2.0.0+\n\n---\n\n\nINSTALLATION\n------------\n\nInstall via Composer in your Yii2 project:\n\n```\ncomposer require yidas/yii2-language\n```\n\n---\n\nCONFIGURATION\n-------------\n\nAdd a component using `yidas\\components\\Language` with configurations:\n\n```php\nreturn [\n    'bootstrap' =\u003e ['log', 'lang'],\n    'language' =\u003e 'en-US',\n    'components' =\u003e [\n        'lang' =\u003e [\n            'class' =\u003e 'yidas\\components\\Language',\n            'languages' =\u003e [\n                0 =\u003e 'en-US',\n                1 =\u003e 'zh-TW',\n                2 =\u003e 'zh-CN',\n            ],\n            'maps' =\u003e [\n                'html' =\u003e [\n                    0 =\u003e 'en',\n                    1 =\u003e 'zh-Hant',\n                    2 =\u003e 'zh-Hans',\n                ],\n            ],\n            // 'storage' =\u003e 'session',\n            // 'storageKey' =\u003e 'language',\n        ],\n        ...\n```\n\n|property|Type|Default|Description|\n|:-|:-|:-|:-|\n|languages|array|As example|Supported language list|\n|maps|array|As example|Customized language map|\n|storage|string|'session'|Storage carrier: 'session' or 'cookie'|\n|storageKey|string|'language'|Storage carrier Key|\n\n### Bootstrap\n\nYou could add the language component into `bootstrap` for keeping the language storage work such as Seesion and Cookie.\n\n```php\n// `lang` component for example\nreturn [\n    'bootstrap' =\u003e ['lang'], \n    ...\n```\n\n---\n\nUSAGE\n-----\n\n### get()\n\nGet Current Language\n\n```php\npublic string get($map=null)\n```\n\n*Example:*\n\n```php\necho \\Yii::$app-\u003elang-\u003eget();  // en-US\n```\n\nYou could get from map by giving map key as first argument:\n\n```php\necho \\Yii::$app-\u003elang-\u003eget('html');  // en\n```\n\n### set()\n\nSet Current Language synchronised to `\\Yii::$app-\u003elanguage`\n\n```php\npublic boolean set($language)\n```\n\n*Example:*\n\n```php\n\\Yii::$app-\u003elang-\u003eset('zh-TW');\n```\n\n### getByMap()\n\nGet customized language value from $map\n\n```php\npublic string getByMap($mapKey)\n```\n\n*Example:*\n\nIf you have to echo HTML language value by current language:\n\n```php\necho \\Yii::$app-\u003elang-\u003egetByMap('html');  // en\n```\n\n### setByMap()\n\nSet by using customized language value from $map\n\n```php\npublic boolean setByMap($mapKey, $mapValue)\n```\n\n*Example:*\n\nIf you have to set current language by inputting a HTML language value:\n\n```php\n$this-\u003esetByMap('html', 'zh-Hant');\n```\n\n### isFirstCome()\n\nFirst time coming check, which has no StorageRecord\n\nInverse alias with `hasStorageRecord()`\n\n```php\npublic boolean isFirstCome()\n```\n\n*Example:*\n\n```php\nif (Yii::$app-\u003elang-\u003eisFirstCome()) {\n    // Detetmine user ip to set current language\n} \nelse if (Yii::$app-\u003elang-\u003ehasStorageRecord()) {\n    // Means !(Yii::$app-\u003elang-\u003eisFirstCome())\n}\n```\n\n---\n\nIMPLEMENTATION\n--------------\n\n### Controller for Changing Language\n\nYou could add a controller or action for changing language like `/language?language=zh-TW`:\n\n```php\n\u003c?php\n\nnamespace app\\controllers;\n\nuse Yii;\nuse yii\\web\\Controller;\n\n/**\n * The Controller for Language converting\n */\nclass LanguageController extends Controller\n{\n    public function actionIndex($language='')\n    {\n        $result = Yii::$app-\u003elang-\u003eset($language);\n        \n        return $this-\u003eredirect(Yii::$app-\u003erequest-\u003ereferrer ?: Yii::$app-\u003ehomeUrl);\n    }\n}\n```\n\n### BeforeAction for globally changing language\n\nYou could globally set language by handling language setting in the bootstrap of application.\n\nFor example, get `GET` parameter to set language in `on beforeAction` function:\n\n```php\nreturn [\n    'on beforeAction' =\u003e function ($event) {\n        // Always fetch language from get-parameter\n        $lang = \\Yii::$app-\u003erequest-\u003eget('lang');\n        // Set to given language with get-parameter\n        if ($lang) {\n            $result = \\Yii::$app-\u003elang-\u003eset($lang);\n        }\n    },\n    ...\n]\n```\n\nAfter that, by giving `lang` param from any url like `/post/my-article?lang=zh-TW` would change language.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fyii2-language","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidas%2Fyii2-language","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fyii2-language/lists"}