laravel 3에서 다국어 페이지를 처리하는 방법을 정리했다. laravel 4에서는 다르게 처리를 해야 하는 것 같은데 이는 차후에 정리를 하겠다.

설정

config/application.php 수정

//영문 URL을 /en/sample로 사용하고자 한다면 
'languages' => array('en', 'kr', 'cn'),
    
//영문 URL을 /sample로 사용하고자 한다면 
'languages' => array('kr', 'cn'),

언어 리소스 파일 수정

  • language/en/sample.php
  • language/en/validation.php
  • language/kr/sample.php
  • language/kr/validation.php
  • language/cn/sample.php
  • language/cn/validation.php

언어 리소스 예제: language/kr/sample.php

<?php
    
return array(
    'page-title' => '한글 페이지',
);

사용예

** blade **

@section('page_title', __('sample.page-title'))

form validation message localization

** Server Side Validation **

$messages = array(
	'phone_match' => __('sample.phrase-phone-match'),
	'phone_required' => __('sample.phrase-phone-required'),
	'phone_min' => __('sample.phrase-phone-min'),
);

$validation = Validator::make($input, $rules, $messages);

** Client Side Validation **

  • jQuery validation 플러그인의 언어별 메세지 파일을 로드한다.
  • custom rule에서 $.validator.messages를 사용한다면 반드시 메시지 파일 다음에 로드해야 한다.

validate.js 파일에서 $.validator.messages.required를 사용한다.

$.validator.addMethod("selected", function(value, element, parameter) {
    return (parseInt(value) || 0) >= 0 ? true : false;
}, $.validator.messages.required);

client side validation 예제

@section('script')
@parent
<script type="text/javascript">
jQuery(function ($) {
	$('#frmSample').validate();
});
</script>

@if (URI::segment(1) === 'kr')
<script type="text/javascript" src="/js/jquery/plugins/validate/1.11.1/localization/messages_ko.js"> </script>
<script type="text/javascript" src="/js/lib/plugin/validate/validate.js"> </script>
@endif
@endsection

URL

URL 앞부분에 언어 정보가 포함되어 언어별 스위칭이 이루어지고 이는 laravel.php에서 수행된다.

  • /sample : 영문
  • /kr/sample : 한글
  • /cn/sample : 중문

view 단에서 언어 스위칭 예제

<?php
$lang = URI::segment(1);
if (strlen($lang) != 2) $lang = 'en';
?>
<ul class="nav nav-pills">
	<li class="{{ $lang === 'en' ? 'active' : '' }}"><a href="{{ $lang === 'en' ? 'javascript:void(0)' : str_replace('/kr', '', preg_replace('/http:\/\/[^\/]+/', '', URL::full())) }}"">{{ __('sample.label-engligh') }}</a></li>
	<li class="{{ $lang === 'kr' ? 'active' : '' }}"><a href="{{ $lang === 'kr' ? 'javascript:void(0)' : '/kr' . preg_replace('/http:\/\/[^\/]+/', '', URL::full()) }}">{{ __('sample.label-korean') }}</a></li>
</ul>

laravel.php에서 언어 정보 처리 코드

/*
|--------------------------------------------------------------------------
| Gather The URI And Locales
|--------------------------------------------------------------------------
|
| When routing, we'll need to grab the URI and the supported locales for
| the route so we can properly set the language and route the request
| to the proper end-point in the application.
|
*/

$uri = URI::current();

$languages = Config::get('application.languages', array());

$languages[] = Config::get('application.language');

/*
|--------------------------------------------------------------------------
| Set The Locale Based On The Route
|--------------------------------------------------------------------------
|
| If the URI starts with one of the supported languages, we will set
| the default lagnauge to match that URI segment and shorten the
| URI we'll pass to the router to not include the lang segment.
|
*/

foreach ($languages as $language)
{
	if (preg_match("#^{$language}(?:$|/)#i", $uri))
	{
		Config::set('application.language', $language);

		$uri = trim(substr($uri, strlen($language)), '/'); break;
	}
}

if ($uri == '') $uri = '/';

URI::$uri = $uri;

관련 Helper

$url = URL::to_language('fr');

References