๊ฐ์ธ์ ์ผ๋ก ์ฌ์ ์ํ ๋ก์ผ์ผ๋ง ์ง์ ํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์๋์ด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
์๋ฅผ ๋ค์ด ๊ฒฝ๋ก๊ฐ ์๋ ๊ฒฝ์ฐ:
/์ฐ๋ฝํ๋ค
/์ ๋ํ
/fr/์ฐ๋ฝ์ฒ
/fr/์ฝ
/fr/about์ ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค.
{{ ๊ฒฝ๋ก('์ฐ๋ฝ์ฒ') }}
/fr/contact ๋์ ๊ธฐ๋ณธ์ ์ผ๋ก /contact์ ๋ํ ๊ฒฝ๋ก๋ฅผ ๋ง๋๋ ์ด์ ๋ ๋ฌด์์ ๋๊น?
๋งํฌ๋ณ๋ก ๊ธฐ๋ณธ ๋์์ ์์ฑํ๋ ค๋ฉด ๋ค์์ ์ํํด์ผ ํฉ๋๋ค.
{{ ๊ฒฝ๋ก('์ฃผ์', {'_locale': app.request.attributes.get('_locale')}) }}
์ด๋ฅผ ์์ ํ ๊ณํ์ด ์์ต๋๊น?
๋ก์ผ์ผ์ ์๋์ผ๋ก ์ค์ ๋์ง๋ง, Routing Listener๊ฐ Routercontext ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ ์ ์ค์ ํ ๊ฒฝ์ฐ ํน์ ์กฐ๊ฑด์์๋ง ์ค์ ๋ฉ๋๋ค.
@Fabien :
2011๋
7์ 14์ผ ๋ชฉ์์ผ 23:57:58 -0700
ivrock์ [email protected] ์ ๋ค์
๊ฐ์ธ์ ์ผ๋ก ์ฌ์ ์ํ ๋ก์ผ์ผ๋ง ์ง์ ํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์๋์ด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
์๋ฅผ ๋ค์ด ๊ฒฝ๋ก๊ฐ ์๋ ๊ฒฝ์ฐ:
/์ฐ๋ฝํ๋ค
/์ ๋ํ/fr/์ฐ๋ฝ์ฒ
/fr/์ฝ/fr/about์ ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค.
{{ ๊ฒฝ๋ก('์ฐ๋ฝ์ฒ') }}
/fr/contact ๋์ ๊ธฐ๋ณธ์ ์ผ๋ก /contact์ ๋ํ ๊ฒฝ๋ก๋ฅผ ๋ง๋๋ ์ด์ ๋ ๋ฌด์์ ๋๊น?
๋งํฌ๋ณ๋ก ๊ธฐ๋ณธ ๋์์ ์์ฑํ๋ ค๋ฉด ๋ค์์ ์ํํด์ผ ํฉ๋๋ค.
{{ ๊ฒฝ๋ก('์ฃผ์', {'_locale': app.request.attributes.get('_locale')}) }}
์ด๋ฅผ ์์ ํ ๊ณํ์ด ์์ต๋๊น?
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
https://github.com/symfony/symfony/issues/1700
ํ ..
์ด ์ํฉ์ ์ด๋ป์ต๋๊น? ์์ด๋ก ๋ ๊ธฐ๋ณธ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์์ผ๋ฏ๋ก /en/ ์ ๋์ด๊ฐ ์์ต๋๋ค. ๋ชจ๋ ๊ฒฝ๋ก์๋ ์๋์ผ๋ก /en/ ์ ๋์ฌ๊ฐ ์ฝ์ ๋์ด URL ๊ตฌ์กฐ์ ์ผ์นํ์ง ์์ต๋๋ค. ๊ธฐ๋ณธ ๋ก์ผ์ผ์ด ํ์ฑํ๋์ด ์์ผ๋ฉด ๊ฒฝ๋ก์ ์ ๋์ฌ๊ฐ ์ฌ์ฉ๋์ง ์๋๋ก ์ด๋ฅผ ์ฐํํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
@ivrock : ์ด๊ฒ์ ์ ํํ ๋ด๊ฐ ์ฐพ๊ณ ์๋ ๋์์ ๋๋ค. ๊ธฐ๋ณธ ๋ก์ผ์ผ์ด ์๋ ๊ฒฝ๋ก์ URL์๋ ๋ก์ผ์ผ ๋งค๊ฐ๋ณ์๊ฐ ์ ํ ํฌํจ๋์ด ์์ง ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ด๋ฉ์ผ ์๋ฆผ, ์ฑ ๊ฐํผ ๋ฑ์ ๋ชจ๋ ์ด์ URL์ ์์์ํค์ง ์๊ณ ๋ก์ผ์ผ ์ง์์ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒ์์ ํ์ฅํ ์ ์์ต๋๋ค. IRC์์ ์ด์ ๋ํด ์ง๋ฌธํ ๋ ์ด๊ฒ์ด ๋ถ๊ฐ๋ฅํ ๊ฒ์ด๋ผ๊ณ ๋ค์์ต๋๋ค. ์ด์ ๊ฐ ๊ถ๊ธ).
@beberlei : @fabpot ๋ง์์ด์์ฃ ? ;)
@craue :
๊ทธ๋ค์ ๊ทธ๊ฒ์ด ๊ฐ๋ฅํ์ง ์์ ์ด์ ์ ๋ํด ์ค๋ช ํ์ต๋๊น?
@ivrock : ์, ๋๊ตฐ๊ฐ๊ฐ ์๋ฅผ ๋ค์ด์ฃผ์์ต๋๋ค. ๋ช ์์ ์ผ๋ก ๋ก์ผ์ผ์ ๊ดํ ๊ฒ์ด ์๋๋ผ URL์์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋งค๊ฐ๋ณ์๋ฅผ ์จ๊ธฐ๋ ๊ฒ์ ๋๋ค. ์ด ์๊ฒฌ์ ๋๋จธ์ง ๋ถ๋ถ์ http://pastebin.com/gavYkF0C๋ฅผ ์ธ์ฉํ๊ณ ์์ต๋๋ค.
foo:
pattern: /foo/{bar}/{car}
defaults:
bar: barValue
car: carValue
์ปจํธ๋กค๋ฌ์์ ๊ฒฝ๋ก ์์ฑ:
$this->get('router')->generate('foo', array('bar' => 'barValue', 'car' => 'somethingElse');
URL์์ ๊ธฐ๋ณธ๊ฐ์ด ์จ๊ฒจ์ ธ ์๋ ์๋๋ฆฌ์ค์์ 'barValue'๋ ๊ธฐ๋ณธ๊ฐ๊ณผ ์ผ์นํ๊ธฐ ๋๋ฌธ์ URL์ ์ถ๊ฐ๋์ง ์์ผ๋ฏ๋ก ์์ฑ๋ URL์ /foo/somethingElse๊ฐ ๋ฉ๋๋ค.
์ด์ ๋๊ตฐ๊ฐ๊ฐ ํด๋น ๋งํฌ๋ฅผ ํด๋ฆญํ๊ณ /foo/somethingElse๋ฅผ ์์ฒญํ๊ณ foo ๊ฒฝ๋ก์ ์ผ์นํ๋ฉด /foo/somethingElse/carValue์ ๋์ผํฉ๋๋ค.
๋ฐ์ ์๋์ฐจ ๋งค๊ฐ๋ณ์๊ฐ ํผํฉ๋์์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค. ๋์์ด ๋ ๊ฒ์ ๋๋ค!
ํ์ง๋ง, ๊ทธ๊ฒ์ ์ ๋ฐ์ ๋ถ๊ณผํฉ๋๋ค. ๋๋ญ๊ฐ์ง ํ ํ๋ฆฟ์์ ๋์ผํ ์์ ์ ์ํํ๋ ๊ฒ์ ์ด๋ป์ต๋๊น? (์๋ณธ ๋ฉ์์ง ์ฐธ์กฐ). ํ์ฌ ๋ก์ผ์ผ์ธ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๋ก์ผ์ผ์ ์๋์ผ๋ก ํฌํจํ๋๋ก path() ํจ์๋ฅผ ํ์ฅํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค.
@ivrock : ๋ฌผ๋ก ๊ฐ๋ฅํฉ๋๋ค ( https://github.com/craue/TwigExtensionsBundle/blob/92ef57f757a24493314c0029d3e21c6ee5563d05/Twig/Extension/ChangeLanguageExtension.php#L223 ์ฐธ์กฐ) ์๋ฃจ์ ์ ;
@ivrock : ์ด๊ธฐ ์ค๋ช ์ ๋ฐ๋ผ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ๋ ๋ฒ ์ ์ํฉ๋๊น? ํํธ์ผ๋ก๋ ๋ก์ผ์ผ ์ ๋์ฌ(๊ธฐ๋ณธ ๋ก์ผ์ผ์ฉ)๊ฐ ์๊ณ ๋ค๋ฅธ ํํธ์ผ๋ก๋ ๋ก์ผ์ผ ์ ๋์ฌ๊ฐ ๋ถ์ต๋๊น? ๋ผ์ฐํ ๊ตฌ์ฑ ํ์ผ์ ๋ ๋ฒ ๊ฐ์ ธ์์ ๋ฌผ๋ก ์ ์๋ ๊ฒฝ๋ก ์ด๋ฆ์ด ์ค๋ณต๋๋๋ก ์๋ํ์ต๋๋ค. _์ข์_ ์๋ฃจ์ ์ ๋ฌด์์ ๋๊น?
์๋ ํ์ธ์. ์๋, ๊ทธ๊ฒ์ด ๋ด๊ฐ ํผํ๋ ค๋ ๊ฒ์ ๋๋ค. ๊ฐ์๊ธฐ 10๊ฐ ์ธ์ด๊ฐ ์์ผ๋ฉด ์๋ฐฑ ๊ฐ์ ๊ฒฝ๋ก๊ฐ ์๋๋ฐ 10๊ฐ๋ฉด ์ถฉ๋ถํฉ๋๋ค.
๋ด๊ฐ ๋ช ๊ฐ์ง ์ธ์ด ๋ง ๊ฐ์ง๊ณ ์๋ค๋ฉด ๊ด์ฐฎ์ ์๊ฐ์ฒ๋ผ ๋ณด์ด์ง๋ง.
๋๋ ํ์ฌ ๊ฐ์ง๊ณ ์์ต๋๋ค :
app.com/kr/contact
app.com/ko/contact
์ฌ๊ธฐ์ /en/์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ธฐ๋ณธ ๋ก์ผ์ผ์ ๋๋ค. ๊ทธ๋์ ๋๋ ๊ฒฝ๋ก๋ฅผ ์ํ๋ค
app.com/contact
์๋์ผ๋ก /en/์ผ๋ก ์๋ํฉ๋๋ค. ํ์ฌ ๋ก์ผ์ผ์ ํฌํจํ์ง ์๋ ๋ค๋ฅธ ๊ฒฝ๋ก๋ฅผ ๋ง๋ค์ด์ผ ํ๋ฉฐ ์ด ๊ธฐ๋ณธ ๋ก์ผ์ผ์ ์์ ๋ ๋๋ญ๊ฐ์ง ํ ํ๋ฆฟ์ ๋ํด ์์ฑ๋ ๋ชจ๋ path() ๊ฒฝ๋ก์๋ ํด๋น ๋ก์ผ์ผ์ด ํฌํจ๋ฉ๋๋ค.
๊ทธ๊ฒ์ ๋ํ ํด๊ฒฐ์ฑ ์ ํ๋ฅญํ ๊ฒ์ ๋๋ค.
๋ด๊ฐ ์ฐพ๊ณ ์๋ ์ถ๋ ฅ์ ์ ํํ ๋ช ํํ ํด์ผ ํฉ๋๋ค.
๋๋ ๊ฐ์ง๊ณ ์๋ค:
ํจํด: /{๋ก์ผ์ผ}/์ฐ๋ฝ์ฒ
๋๋ ๊ฒฝ๋ก๋ฅผ ์ํ๋ค:
app.com/contact(๊ตฌ์ฑ ํ์ผ์ ์ค์ ๋ ๊ธฐ๋ณธ ๋ก์ผ์ผ)
app.com/kr/contact
์ด์ app.com/contact๋ฅผ ๋ณผ ๋ ๋ชจ๋ ๊ฒฝ๋ก๊ฐ /pagename์ผ๋ก ๊ธฐ๋ณธ ์ค์ ๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์ twig์ path()์ /{locale}/pagename์ด ํฌํจ๋์ง ์๋๋ก ํ๊ณ ์ถ์ต๋๋ค.
์ด์ ๋ํ 2๊ฐ์ง ์ฅ์ ๋ฌผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
/contact๋ฅผ ๋ง๋ค๊ธฐ ์ํด ๋ ์ด์์ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ ํฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. path()์๋ ๊ฒฝ๋ก ๋งค๊ฐ๋ณ์๊ฐ ํ์ํ์ง๋ง ์ด์ ๋ก์ผ์ผ์ ๋ฐ๋ผ 2๊ฐ๊ฐ ์์ต๋๋ค.
๊ฒฝ๋ก('์ฐ๋ฝ์ฒ')
๊ฒฝ๋ก('contactIN')
๋ชจ๋ ๊ฒ์ด ๋งค์ฐ ์ง์ ๋ถํด ๋ณด์ ๋๋ค. path()์์ ๊ธฐ๋ณธ ๋ก์ผ์ผ์ ์ ํํ ๊ฒฝ์ฐ ๋๋ญ๊ฐ์ง๋ฅผ ํ์ฅํ์ฌ ๋ก์ผ์ผ์ ์ ๊ฑฐํ๋๋ผ๋ ์ฌ์ ํ ๊ฒฝ๋ก๊ฐ ์๋ํ์ง ์๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ง๊ธ ๋น์ฅ์ ๋ถ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํ์ง๋ง ์์ด๋์ด์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
@ivrock : ๋น์ ์ ์ฌ์ฉํด์ผํฉ๋๋ค "๋ง๋ฒ"(๊ฐ์ง) ๋งค๊ฐ ๋ณ์ {_locale}
๋์ {locale}
์ ํํ ๋ ๋ฒ ๋๋ ๊ฐ ๋ก์ผ์ผ์ด ์๋ ํ ๊ฐ์ง๋ฅผ ์ธ๊ธ ํ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ์ ์ํด์ผํ๋ ์๋จ . ๊ทธ๋ฌ๋ ๊ทํ๊ฐ ์ค๋ช
ํ ๋ฌธ์ ๊ฐ ์ง์๋๋ฉฐ ํ์ฌ ์ด์ ๋ํ ๊นจ๋ํ ํด๊ฒฐ์ฑ
์ด ์๋์ง ์์ฌ์ค๋ฝ์ต๋๋ค.
์ฃ์กํฉ๋๋ค. ์ฐจ์ด๊ฐ ์๋์ง ๋ชฐ๋์ต๋๋ค. ์ ๋ {_locale}์ ์ฌ์ฉํ๊ณ ์๋ฅผ ๋ค์ด {locale}์(๋ฅผ) ์ฌ์ฉํ์ต๋๋ค. ์, ์ฌํ๊ฒ๋ ๋ฌธ์ ๊ฐ ์ฌ์ ํ ์ง์๋ฉ๋๋ค.
์๋ ํ์ธ์,
์ํ๋ ๊ฒ์ ๋ฌ์ฑํ๊ธฐ ์ํด ์ด ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ๊ณผ ํจ๊ป {_locale}
๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ์ฃผ์์ ์ฌ์ฉํ๊ณ ์์ง๋ง YAML ๋ผ์ฐํ
ํ์ผ์ ์ฌ์ฉํ์ฌ ์๋ํ์ง ์๋ ์ด์ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ์ฌ๊ธฐ์ ๋ช ๊ฐ์ง ์ ๋ณด๊ฐ ์์ต๋๋ค.
์ง์ํ๊ณ ์ถ์ ๋ค์ ๊ฒฝ๋ก๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
app.com/
=> (์์ด์ฌ์ผ ํจ)
app.com/en
=> (์์ด์ฌ์ผ ํจ)
app.com/fr
=> (๋ถ์ด์ฌ์ผ ํจ)
๋ด ์ปจํธ๋กค๋ฌ์์ ๋ค์ ์ฃผ์์ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ๋ฌ์ฑํฉ๋๋ค.
/**
* @Route("/{_locale}", name = "index", defaults = {"_locale" = "en"})
*/
์ง๊ธ์ผ๋ก์๋ ๋ผ์ฐํ
์ ์์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ ์๊ตฌ์ฌํญ์ ์ค์ ํ์ง ์์์ต๋๋ค. ๋ฐ๋ผ์ ์ ๋ก์ผ์ผ์ ์ถ๊ฐํ๋ ๊ฒ์ ๋ง์ ์๊ตฌ์ฌํญ ์ ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ด๊ฒ์ defaults
์ ์๋ ํด๋น๋์ง๋ง ๊ธฐ๋ณธ ๋ก์ผ์ผ์ ๋ณ๊ฒฝํ๋ฉด ์ ๋ก์ผ์ผ์ ์ถ๊ฐํ ๊ฐ๋ฅ์ฑ์ด ์ ์ต๋๋ค. ์ด์จ๋ ์ง๊ธ์ app.com/es
๋ฅผ ๋ก๋ํ๋ ค๊ณ ํ๋ฉด ๋ด ๊ธฐ๋ณธ ๋ก์ผ์ผ์ธ ์์ด๋ก ํ์ด์ง๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
Twig์์ {{ path('index') }}
ํ๋ฉด ํ์ฌ {_locale}
๊ฐ en
์ด๊ณ app.com/fr
๊ฐ ์ถ๋ ฅ๋๋ฉด app.com/
url์ด ์์ฑ๋ฉ๋๋ค. ํ์ฌ {_locale}
๊ฐ fr
. ๊ธฐ๋ณธ ์ค์ ์ด ์๋ ๊ฒฝ๋ก์ ๋ํ ์ด ์์
์ด๋ฒคํธ์
๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ๋ฉด
/**
* @Route("/{_locale}/event/create", name = "event_create")
*/
Twig, {{ path('event_create') }}
์์ ์ด ์์
์ ์ํํ๋ฉด {_locale}
๋งค๊ฐ๋ณ์์ ํ์ฌ ๊ฐ๊ณผ ๊ด๋ จํ์ฌ ์ถ๋ ฅ์ด ๋ณ๊ฒฝ๋ฉ๋๋ค.
en
=> app.com/en/event/create
fr
=> app.com/fr/event/create
์ด๋์์๋ ๋ก์ผ์ผ์ ์ง์ ํ ํ์๊ฐ ์์ต๋๋ค.
๋์์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
๋ฌธ์ ์ธ์ฌ,
๋งคํธ
๋๋ ๋ํ ์ด๊ฒ๊ณผ ๊ด๋ จํ์ฌ ๋ช ๊ฐ์ง ๋ฌธ์ ์ ์ง๋ฉดํ์ผ๋ฉฐ ์ฌ๊ธฐ์์ ๋ด ์๋ฃจ์
์ ์ฐพ์ ์ ์์ต๋๋ค.
http://github.com/schmittjoh/JMSI18nRoutingBundle
๊ธฐ๋ณธ์ ์ผ๋ก ํ์งํ๋ ๊ฒฝ๋ก ์์ด ์์ํ ๋ค์ ํ์์ ๋ฐ๋ผ ๊ฒฝ๋ก ๋๋ ์ผ๋ถ ๊ฒฝ๋ก์ ๋ํ ๋ฒ์ญ์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
์ด๋ป๊ฒ ์๊ฐํ๋์ง ์๋ ค์ฃผ์๋ฉด Symfony 2.1์ ์ผ๋ถ ๊ธฐ๋ฅ์ ํฌํจํ ์ ์์ต๋๋ค.
@schmittjoh : Thx, ์ด๊ฒ์ ์ ๋ง ํฅ๋ฏธ๋ก์ด ๊ฒ ๊ฐ์ต๋๋ค. ๋๋ ์์ผ๋ก ๋ฉฐ์น ๋์ ๊ทธ๊ฒ์๋ณด๊ณ ๊ทธ๊ฒ์ ๋ํ ๋์ ํผ๋๋ฐฑ์ ์ค ๊ฒ์ ๋๋ค.
์ด ๋ฒ๋ค๊ณผ Symfony์ ๋ํ ์์ ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๋ฌธ์ ์ธ์ฌ,
๋งคํธ
์๋
ํ์ธ์, ์๋ง๋ ์ด ๋งํฌ๊ฐ ๋์์ด ๋ ๊ฒ์
๋๋ค:
http://stackoverflow.com/questions/5800675/symfony2-locale-in-route/5803144#5803144
{{ path('address', {'_locale': app.request.attributes.get('_locale')}) }}
๋ง์คํฐ์์ @beberlei ๊ฐ ์ค๋ช
ํ ๋ฌธ์ ๊ฐ ์์ ๋์์ต๋๋ค(๊ทธ๋ฌ๋ ์ด ์์ ์ฌํญ์ 2.0์ผ๋ก ๋ฐฑํฌํธํ ์ ์์). ๋ฐ๋ผ์ _locale
์ธ์๋ ์ด์ ํญ์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ ํ์ฌ ๋ก์ผ์ผ์ ๋ํ ๊ฒฝ๋ก๋ฅผ ์์ฑํ ๋ ๋ช
์์ ์ผ๋ก ์ ๋ฌํ ํ์๊ฐ ์์ต๋๋ค.
ํ์ฌ๋ก์๋ ์ด๊ฒ์ด ๊ฐ๋ฅํ๊ณ ํจ์จ์ ์ ๋๋ค.
PTCIdentialsBundle:
resource: "@PTCIdentialsBundle/Controller/"
type: annotation
prefix: /{_locale}
๊ทธ๋ฌ๋ ์ฌ๊ธฐ์ defaults: { _locale: en }
๋งค๊ฐ๋ณ์๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ํ๋์ ์ปจํธ๋กค๋ฌ์ ๋ํ ๊ฒฝ๋ก๋ฅผ ์ ์ํ ๋๋ง ๊ฐ๋ฅํฉ๋๋ค.
contact:
pattern: /{_locale}/contact
defaults: { _controller: AcmeDemoBundle:Contact:index, _locale: en }
requirements:
_locale: en|fr|de
๋ฐ๋ผ์ ์ฌ์ดํธ์ ๋ฃจํธ์ ์ก์ธ์คํ ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ๋ค์๊ณผ ๊ฐ์ด ๋ฆฌ๋๋ ์ ์ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
root:
pattern: /
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: homepage
permanent: true # this is for 301
๊ทธ๋ฆฌ๊ณ ๋ด ๋ชจ๋ ๊ฒฝ๋ก์ ๋ํด ๊ธฐ๋ณธ ๋ก์ผ์ผ์ ๋ํด์๋ ํญ์ ๋ก์ผ์ผ์ด ์์ต๋๋ค.
en
=> http://my-app.com/en
(๊ธฐ๋ณธ ๋ก์ผ์ผ)
fr
=> http://my-app.com/fr
ํน์ ์ปจํธ๋กค๋ฌ์ ์ง์ {_locale}์ ์ถ๊ฐํ ๊ฒ๊ณผ ๊ฐ์ ๋์์ ํ๋ ๊ฒ์ด ๋ ํฅ๋ฏธ๋ก์ธ ๊ฒ์ ๋๋ค.
/**
* @Route("/{_locale}", name = "homepage", defaults = {"_locale" = "en"})
*/
@raziel057 Sf 2.2 ์ฌ์ฉ์ import์ ๊ธฐ๋ณธ๊ฐ ๊ฐ๋ฅ
๋๋ Symfony(v 2.2.1)์ ๋ง์ง๋ง ๋ฒ์ ์ ์ฌ์ฉํ์ง๋ง defaults
๋ ๋ค์๊ณผ ๊ฐ์ด ์ธ ๋ ์๋ฌด ํจ๊ณผ๊ฐ ์์ต๋๋ค.
PTCIdentialsBundle:
resource: "@PTCIdentialsBundle/Controller/"
type: annotation
prefix: /{_locale}
defaults: { _locale: en }
http://my-app.com/
์ก์ธ์คํ ์ ์์ต๋๋ค.
์ค์ ๋ก router:debug
๋ช
๋ น์ผ๋ก ๋ณผ ์ ์๋ฏ์ด defaults
๊ฐ ๊ณ ๋ ค๋ฉ๋๋ค.
> php app/console router:debug welcome_index
[router] Route "welcome_index"
Name welcome_index
Path /{_locale}/
Host ANY
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: PTC\IdentialsBundle\Controller\WelcomeController::indexAction
_locale: en
Requirements _locale: en|fr
Options compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex #^/(?P<_locale>en|fr)/$#s
๊ทธ๋ฌ๋ http://my-app.com/
์ ์ก์ธ์คํ๋ ค๊ณ ํ๋ฉด "NotFoundHttpException"์ด ๋ฐ์ํฉ๋๋ค.
@ raziel057 ๋๋ ํํ "/"์ ์ ๊ฑฐํ๊ณ ์๋ํ๋์ง ํ์ธํฉ๋๋ค.
@henrikbjorn ์ด๋ฏธ ์๋ํ์ง๋ง ์๋ํ์ง ์์ต๋๋ค.
๋ํ ๋ค์๊ณผ ๊ฐ์ ๋ค๋ฅธ ๊ฒฝ๋ก๊ฐ ์๋ ๊ฒฝ์ฐ:
> php app/console router:debug login
[router] Route "login"
Name login
Path /{_locale}/secured/login
Host ANY
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: PTC\IdentialsBundle\Controller\AuthenticationController::indexAction
Requirements _locale: en|fr
Options compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex #^/(?P<_locale>en|fr)/secured/login$#s
{_locale}/
๋ ๋ค์์ ์ํด ๊ธฐ๋ณธ ์ธ์ด๋ก ์ก์ธ์คํ ๋ ๋น ๋ฌธ์์ด๋ก ๋์ฒด๋์ด์ผ ํฉ๋๋ค.
http://my-app.com/secured/login
in ko
http://my-app.com/fr/secured/login
(ํ๋์ค์ด)
@raziel057 ๊ฒฝ๋ก๋ฅผ ๊ฐ์ ธ์ฌ ๋ #4322๋ฅผ ์น๊ณ ์์ต๋๋ค. /{_locale}/
์ ๋ก์ผ์ผ์ ๊ธฐ๋ณธ๊ฐ์ด ์์ด๋ ์ ํ ์ฌํญ์ด ์๋๋๋ค. ๋ฐ๋ผ์ /
๋ ์ผ์นํ์ง ์์ต๋๋ค. ์ ๊ท์์ ๋์์ผ๋ก ๋ณผ ์ ์์ต๋๋ค.
{_locale}/์ ๋ค์์ ํฌํจํ๊ธฐ ์ํด ๊ธฐ๋ณธ ์ธ์ด๋ก ์ก์ธ์คํ ๋ ๋น ๋ฌธ์์ด๋ก ๋์ฒด๋์ด์ผ ํฉ๋๋ค.
http://my-app.com/secured/login in ko
http://my-app.com/fr/secured/login in fr
๊ฐ์ ์ด์ : ์ค๊ฐ์ ์ ํ์ ์๋ฆฌ ํ์์๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. #7051์ด ํด๊ฒฐํฉ๋๋ค.
@ํ ๋น์จ ๋ค . ์ค๋ช ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. @maoueh ๋์ ๋๊ธ์ ๋ณด๋ ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํ์ต๋๋ค.
Symfony์ ๊ณต์ ๋ฌธ์๋ฅผ ํ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์์ญ๋๊น?
http://symfony.com/doc/current/book/forms.html <- "en"์ ๋ด์ฉ
http://symfony.com/fr/doc/current/book/forms.html <- "fr"์ ๋ด์ฉ
์ด ๋ฌถ์์ธ๊ฐ์? http://github.com/schmittjoh/JMSI18nRoutingBundle
๋ชจ๋ฅด๊ฒ ์ด. ๊ทธ๋ฌ๋ ํด๊ฒฐ ๋ฐฉ๋ฒ์ผ๋ก ๋ก์ผ์ผ์ด ์๋ ๊ฒฝ๋ก์ ์๋ ๊ฒฝ๋ก์ ๋ ๊ฐ์ง๋ฅผ ์ ์ํ ์ ์์ต๋๋ค. ๊ฒฝ๋ก๋ฅผ ์์ฑํ ๋ ์ ์ ํ ๊ฒฝ๋ก๋ฅผ ์ ํํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
์ด ๊ตฌ์ฑ์ผ๋ก http://github.com/schmittjoh/JMSI18nRoutingBundle ์ ์ฌ์ฉํ ๋:
jms_i18n_routing:
default_locale: en
locales: [en, fr]
strategy: prefix_except_default
php app/console router:debug
์คํํ ๋ ๋ค์ ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค.
en__RG__welcome_index ANY ANY ANY /
fr__RG__welcome_index ANY ANY ANY /fr/
en__RG__view_about ANY ANY ANY /about
fr__RG__view_about ANY ANY ANY /fr/about
en__RG__view_contact ANY ANY ANY /contact
fr__RG__view_contact ANY ANY ANY /fr/contact
...
๋ฐ๋ผ์ ๊ฒฝ๋ก๊ฐ ์ ๋ณต์ ๋์์์ ์ ์ ์์ต๋๋ค. ํ์ง๋ง http://my-app.com/
์ ์ก์ธ์คํ๋ ค๊ณ ํ๋ฉด ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
FatalErrorException: ์ค๋ฅ: ์ต๋ ํจ์ ์ค์ฒฉ ์์ค '100'์ ๋๋ฌํ์ฌ ์ค๋จํฉ๋๋ค! /home/lallement/workspace/Identials/vendor/symfony/symfony/src/Symfony/Component/Routing/Matcher/Dumper/DumperCollection.php ๋ผ์ธ 54
in /home/lallement/workspace/Identials/vendor/symfony/symfony/src/Symfony/Component/Routing/Matcher/Dumper/DumperCollection.php line 54
at ErrorHandler??handleFatal() in /home/lallement/workspace/Identials/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php line 0
at DumperCollection??add() in /home/lallement/workspace/Identials/vendor/symfony/symfony/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php line 70
at DumperPrefixCollection??addPrefixRoute() in /home/lallement/workspace/Identials/vendor/symfony/symfony/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php line 72
๊ฒ์์ ์ด์ฉํด์ฃผ์ธ์.
@Tobion ๋ค ์ฃ์กํฉ๋๋ค ;). xdebug์ max_nesting_level
๊ฐ ๋ฎ์ต๋๋ค.
์ฒซ ๋ฒ์งธ @damien-roche ๋ฌธ์ ์ ๋ํ ํด๊ฒฐ์ฑ
์ ์ฐพ์ ์ฌ๋์ด ์์ต๋๊น?
๋๋ ๋๊ฐ์ ๋ฌธ์ ๊ฐ ์๊ณ ๊นจ๋ํ ํด๊ฒฐ์ฑ
์ ์ฐพ์ ์ ์์ต๋๋ค
์ด๊ฒ์ด ๋น์ ์ด ์ฐพ๊ณ ์๋ ๊ฒ์ธ์ง ํ์คํ์ง ์์ผ๋ฉฐ ๋ช ๋ ํ์ ๊ทธ๋ ๊ฒ ์ ์ฉํ์ง ์์ ์๋ ์์ต๋๋ค ... ๊ทธ๋ฌ๋ ์ฌ๊ธฐ์ ์ค๋ช ๋ ์ํฉ๊ณผ ๋งค์ฐ ์ ์ฌํ ๊ฒ์ผ๋ก ๋ณด์ด๋ ๋ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ํ ํ ์์ ์ด ์์ต๋๋ค. @damien-roche .
์ฐ์ ์ ๋ Symfony์ 5.1.2
๋ฒ์ ์ผ๋ก ์์
ํ๊ณ ์์ต๋๋ค.
์ฌ๊ธฐ ๋ด translation.yaml
ํ์ผ์ด ์์ต๋๋ค:
framework:
default_locale: fr
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- en
enabled_locales: ['fr', 'en']
๋๋ ์ง์๋๋ ๋ก์ผ์ผ์ ์ ์ํ๊ณ ์ ์ ๋ FR
์ ๋์ฒด์ ๊ธฐ๋ณธ ํ๋๋ก EN
. ์ด ๊ตฌ์ฑ์ ๋ผ์ฐํ
์ง๋ฌธ์ ์ผ๋ถ๊ฐ ์๋์ง๋ง ๋ฒ์ญ ๊ด๋ฆฌ๋ ๋ค๊ตญ์ด ์น์ฌ์ดํธ๋ฅผ ๊ด๋ฆฌํ๋ ์๋ฃจ์
์ ์ผ๋ถ์ด๋ฏ๋ก ๊ณต์ ํฉ๋๋ค.
์ ์๋ ๋ชจ๋ ์ฃผ์์ด ๋ค๊ตญ์ด ์ง์์ ์ํด ๋ณ๊ฒฝ๋ ํ์๊ฐ ์๋๋ก ๊ฒฝ๋ก ๊ตฌ์ฑ์ ๋ณ๊ฒฝํ์ต๋๋ค. ๋จ์ํ routes/annotations.yaml
์์ ์ง์๋๋ ๊ฐ ์ธ์ด์ ๋ํ ์ ๋์ฌ๋ฅผ ์ ์ํ์ต๋๋ค.
controllers:
resource: ../../src/Controller/
type: annotation
defaults:
_locale: '%kernel.default_locale%'
requirements:
_locale: fr|en
prefix:
fr: ''
en: '/en'
๋ค์์ ๋ชจ๋ ๊ฒฝ๋ก๊ฐ ์์๋๋ก ์๋ํ๋๋ก security.yaml
์์ ๊ตฌ์ฑํ ๊ฒ์
๋๋ค.
access_control:
- { path: ^/(_|en/)login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/(_|en/)/admin, roles: ROLE_ADMIN}
- { path: ^/(_|en/)/, roles: IS_AUTHENTICATED_ANONYMOUSLY}
์ด ๊ตฌ์ฑ์ ํตํด ๊ธฐ๋ณธ ์ธ์ด( /
, /login
...)์ ๋ํ ๋ชจ๋ ๊ธฐ์กด ๊ฒฝ๋ก์ en
์ ๋์ฌ( /en
๊ฐ ์ ์๋ ์์ด ๋ฒ์ ์ ๋ํ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. /en/login
...).
์ด์ php bin/console debug:router
์คํํ๋ฉด ๋๋ญ๊ฐ์ง ํ
ํ๋ฆฟ์์ ์ฌ์ฉํ ์ ์๋ ๋ชจ๋ ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค.
์ฆ, ํ์ฌ ๋ก์ผ์ผ์ ๋ํด ๊ฑฑ์ ํ์ง ์๊ณ ์น์ฌ์ดํธ์์ ํ์์ ์ ์ํด์ผ ํ๋ ๊ฒฝ์ฐ ํ์์ ๊ฐ์ด ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
<a href="{{ path('app_login') }}">{% trans %}action.menu.member.access{% endtrans %}</a>
๊ทธ๋ฆฌ๊ณ ํน๋ณํ "ํ์งํ๋" ๊ฒฝ๋ก ์ค ํ๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ(์: ์น์ฌ์ดํธ๋ฅผ FR์์ EN์ผ๋ก ์ ํํ๋ ๋งํฌ) ์ํ๋ ๋ก์ผ์ผ์ ํฌํจํ๋ ์ ์ ํ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
{% if app.request.locale != 'en' %}
<a href="{{ path('app_home_index.en') }}">EN</a>
{% endif %}
{% if app.request.locale != 'fr' %}
<a href="{{ path('app_home_index.fr') }}">FR</a>
{% endif %}
์ด๊ฒ์ด ๋์์ด ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
๋ฌธ์ ์ธ์ฌ,
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ฒซ ๋ฒ์งธ @damien-roche ๋ฌธ์ ์ ๋ํ ํด๊ฒฐ์ฑ ์ ์ฐพ์ ์ฌ๋์ด ์์ต๋๊น?
๋๋ ๋๊ฐ์ ๋ฌธ์ ๊ฐ ์๊ณ ๊นจ๋ํ ํด๊ฒฐ์ฑ ์ ์ฐพ์ ์ ์์ต๋๋ค