ããã«ã¡ã¯ãã¨ã³ã¸ãã¢ã® @localdisk ã§ãããã®ã¨ã³ããªã¯ ã¤ããã¼ã¿ã¼ã»ã¸ã£ãã³ Advent Calendar 2016 16æ¥ç®ã®è¨äºã§ãã
å æ¥ã¯å½ç¤¾ãã¶ã¤ãã® @tyan_hikaruã®è¨äºã§ããããããã§ããã§ãããã? ã¾ã èªãã§ãªãã¨ããæ¹ã¯ããã£ããç®ãéãã¦ã¿ã¦ãã ããã
ãã¦ãLaravel 5.3 ã®ãªãªã¼ã¹ãã4ã¶æã5.3 ã使ãæ £ããããªãã¨ããä»æ¥ãã®é ã§ãããGihtubãªãã¸ã㪠ã§ã¯ 5.4 ã®éçºãé²ãã§ãã¾ããä»å㯠Laravel 5.4 ã®æ°æ©è½ã®ä¸ã§ãå人çã«å¬ãã対å¿ã§ãã£ã JSON Based Language Files ã®è©±ããããã¨æãã¾ãã
ä½ãå¬ããã®ã?
Web ã¢ããªã±ã¼ã·ã§ã³ãå¤è¨èªå¯¾å¿ããå ´åãä¸è¬ã«ã¯*1ã¯ã©ã¤ã¢ã³ããµã¤ãã¨ãµã¼ãã¼ãµã¤ãã§ç¿»è¨³æ å ±ãæã¤å¿ è¦ãããã¾ããã¯ã©ã¤ã¢ã³ããµã¤ãã¯JSON以å¤ã®é¸æè¢ã¯ã»ã¼ãªãç¶æ³ã§ããããµã¼ãã¼ãµã¤ãã®å ´åããã®è¨èªã¨ç¸æ§ã®ããå½¢å¼ã§æã¤ãã¨ãå¤ããªãã¾ããLaravel ã®å ´åã¯é åã§å®ç¾©ãããPHPãã¡ã¤ã«ã«ãã£ã¦å¤è¨èªãµãã¼ããå®ç¾ãã¦ãã¾ãã
Laravel 5.4 ã§ã¯å¤è¨èªãµãã¼ãã¨ãã¦ä¸è¿°ã®PHPãã¡ã¤ã«ä»¥å¤ã«ãJSONããµãã¼ããã¾ãããã®å¯¾å¿ã«ãã£ã¦ã¯ã©ã¤ã¢ã³ããµã¤ãã¨ãµã¼ãã¼ãµã¤ãã§åããã¡ã¤ã«ãå ±æã§ãããã¨ã«ãªãã¾ããã¯ã©ã¤ã¢ã³ããµã¤ãã¨ãµã¼ãã¼ãµã¤ãã§åãæå³ã®ã¡ãã»ã¼ã¸ãªã®ã«å¾®å¦ã«å 容ãéãâ¦ã¨ããæ²åãç¹°ãè¿ããã¨ããªããªãã¾ããå¬ããï¼
ç¹ã«å½ç¤¾ã§Webãµã¼ãã¹ãä½æããéã«ã¯å¿ ãå¤è¨èªåãããã¨ã義åä»ãããã¦ããã®ã§é常ã«ãããããã
ã§ã¯æ¬é¡ã«ãç¾å¨æç¹(2016/12/14)ã§ã®ä½¿ãæ¹ã¯ä»¥ä¸ã®ãããªæãã§ãã
è¨èªãã¡ã¤ã«ãä½ã
resources/lang/{locale}.json
ãä½æãã¾ãã {locale}
ã«ã¯ç¿»è¨³å¯¾è±¡ã®ãã±ã¼ã«ãæå®ãã¾ããæ¥æ¬èªã®å ´å㯠resources/lang/ja.json
ã§ããã
è¨èªãã¡ã¤ã«ã®ä¾
{ "sample": "ãµã³ãã«", "required": ":attribute ã¯å¿ ãå ¥åãã¦ãã ãã" }
__ é¢æ°
JSON ãç¨ãããã¡ã¤ã«ã翻訳ãã¦åºåããã®ã¯ __ é¢æ°ãç¨ãã¾ã(ã¢ã³ãã¼ã¹ã³ã¢ã2å)ã WordPress ã¨ä¸ç·ã§ããã
string __($key = null, $replace = [], $locale = null)
第1å¼æ°ã¯ãã¼ã第2å¼æ°ã¯ç½®æãããã©ã¡ã¼ã¿ã第3å¼æ°ã¯ãã±ã¼ã«ãæå®ãã¾ãã第3å¼æ°ãæå®ããªãå ´åããã©ã«ããã±ã¼ã«(config/app.phpã§æå®ããã¦ãããã±ã¼ã«)ãè¨å®ããã¾ãã
使ç¨ä¾
<?php // ä¸è¿°ã® resources/lang/ja.json ã使ç¨ããå ´å echo __('sample'); // ãµã³ãã« echo __('required', ['attribute' => 'ãåå']); // ãååã¯å¿ ãå ¥åãã¦ãã ãã
ããããã§ããã trans
é¢æ°ã¨å¤§ä½ä¸ç·ã§ããJSONãã¡ã¤ã«å
ã§å
¥ãåã«ãªã£ãæã« .(ããã)
ã§ç¹ãããã¨ãã§ããªãã®ãé£ç¹ã§ããã ãã¡ããè¿ã解æ¶ãããã¨æãã¾ãã
ä¸ç·ã«ç¦å²¡ã§ Laravel ã使ãåãã¾ããã?
ãå¿åãå¾ ã¡ãã¦ããã¾ãï¼
ææ¥ã¯ã¨ã³ã¸ãã¢ã® @hacktk ã§ããã楽ãã¿ã«ï¼
*1:Node.jsã¯JSONã§çµ±ä¸ã§ãã