Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
89e3bf1f
Commit
89e3bf1f
authored
Oct 26, 2012
by
Anahkiasen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add URL::to_language and HTML::link_to_language localization helpers
Signed-off-by:
Anahkiasen
<
ehtnam6@gmail.com
>
parent
205cc486
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
4 deletions
+84
-4
urls.md
laravel/documentation/urls.md
+11
-0
html.md
laravel/documentation/views/html.md
+12
-1
html.php
laravel/html.php
+16
-3
url.test.php
laravel/tests/cases/url.test.php
+20
-0
url.php
laravel/url.php
+25
-0
No files found.
laravel/documentation/urls.md
View file @
89e3bf1f
...
...
@@ -59,6 +59,17 @@ Sometimes you may need to generate a URL to a named route, but also need to spec
$url = URL::to_action('user@profile', array($username));
<a
name=
"urls-to-a-different-language"
></a>
## URLs To A Different Language
#### Generating a URL to the same page in another language:
$url = URL::to_language('fr');
#### Generating a URL to your home page in another language:
$url = URL::to_language('fr', true);
<a
name=
"urls-to-assets"
></a>
## URLs To Assets
...
...
laravel/documentation/views/html.md
View file @
89e3bf1f
...
...
@@ -87,6 +87,17 @@ For example, the < symbol should be converted to its entity representation. Conv
echo HTML::link_to_action('user@profile', 'User Profile', array($username));
<a
name=
"links-to-a-different-language"
></a>
## Links To A Different Language
#### Generating a link to the same page in another language:
echo HTML::link_to_language('fr');
#### Generating a link to your home page another language
echo HTML::link_to_language('fr', true);
<a
name=
"mail-to-links"
></a>
## Mail-To Links
...
...
@@ -119,7 +130,7 @@ The "mailto" method on the HTML class obfuscates the given e-mail address so it
echo HTML::ol(array('Get Peanut Butter', 'Get Chocolate', 'Feast'));
echo HTML::ul(array('Ubuntu', 'Snow Leopard', 'Windows'));
echo HTML::dl(array('Ubuntu' => 'An operating system by Canonical', 'Windows' => 'An operating system by Microsoft'));
<a
name=
"custom-macros"
></a>
...
...
laravel/html.php
View file @
89e3bf1f
...
...
@@ -238,6 +238,19 @@ class HTML {
return
static
::
link
(
URL
::
to_action
(
$action
,
$parameters
),
$title
,
$attributes
);
}
/**
* Generate an HTML link to a different language
*
* @param string $language
* @param string $title
* @param array $attributes
* @return string
*/
public
static
function
link_to_language
(
$language
,
$title
=
null
,
$attributes
=
array
())
{
return
static
::
link
(
URL
::
to_language
(
$language
),
$title
,
$attributes
);
}
/**
* Generate an HTML mailto link.
*
...
...
@@ -347,7 +360,7 @@ class HTML {
return
'<'
.
$type
.
static
::
attributes
(
$attributes
)
.
'>'
.
$html
.
'</'
.
$type
.
'>'
;
}
/**
* Generate a definition list.
*
...
...
@@ -360,13 +373,13 @@ class HTML {
$html
=
''
;
if
(
count
(
$list
)
==
0
)
return
$html
;
foreach
(
$list
as
$term
=>
$description
)
{
$html
.=
'<dt>'
.
static
::
entities
(
$term
)
.
'</dt>'
;
$html
.=
'<dd>'
.
static
::
entities
(
$description
)
.
'</dd>'
;
}
return
'<dl'
.
static
::
attributes
(
$attributes
)
.
'>'
.
$html
.
'</dl>'
;
}
...
...
laravel/tests/cases/url.test.php
View file @
89e3bf1f
...
...
@@ -105,6 +105,26 @@ class URLTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'http://localhost/index.php/url/test/taylor/otwell'
,
URL
::
to_route
(
'url-test-2'
,
array
(
'taylor'
,
'otwell'
)));
}
/**
* Test the URL::to_language method.
*
* @group laravel
*/
public
function
testToLanguageMethodGeneratesURLsToDifferentLanguage
()
{
URI
::
$uri
=
'foo/bar'
;
Config
::
set
(
'application.languages'
,
array
(
'sp'
,
'fr'
));
Config
::
set
(
'application.language'
,
'sp'
);
$this
->
assertEquals
(
'http://localhost/index.php/fr/foo/bar'
,
URL
::
to_language
(
'fr'
));
$this
->
assertEquals
(
'http://localhost/index.php/fr/'
,
URL
::
to_language
(
'fr'
,
true
));
Config
::
set
(
'application.index'
,
''
);
$this
->
assertEquals
(
'http://localhost/fr/foo/bar'
,
URL
::
to_language
(
'fr'
));
$this
->
assertEquals
(
'http://localhost/sp/foo/bar'
,
URL
::
to_language
(
'en'
));
}
/**
* Test language based URL generation.
...
...
laravel/url.php
View file @
89e3bf1f
...
...
@@ -294,6 +294,31 @@ class URL {
return
static
::
to
(
$uri
,
$https
);
}
/**
* Get the URL to switch language, keeping the current page or not
*
* @param string $language The new language
* @param boolean $reset Whether navigation should be reset
* @return string An URL
*/
public
static
function
to_language
(
$language
,
$reset
=
false
)
{
// Get the url to use as base
$url
=
$reset
?
URL
::
home
()
:
URL
::
to
(
URI
::
current
());
// Validate the language
if
(
!
in_array
(
$language
,
Config
::
get
(
'application.languages'
)))
{
return
$url
;
}
// Get the language we're switching from and the one we're going to
$from
=
'/'
.
Config
::
get
(
'application.language'
)
.
'/'
;
$to
=
'/'
.
$language
.
'/'
;
return
str_replace
(
$from
,
$to
,
$url
);
}
/**
* Substitute the parameters in a given URI.
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment