Skip to content

Commit cfdbefd

Browse files
committed
translate_url helper
1 parent 7fec514 commit cfdbefd

4 files changed

Lines changed: 77 additions & 6 deletions

File tree

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This package is built on top of [PHP JSON tongue](https://github.com/elaborate-c
1313

1414
### Requirements
1515

16-
- PHP 8.0 or higher.
16+
- PHP 8.0 or higher.
1717

1818
### Setup
1919

@@ -73,8 +73,8 @@ The output:
7373

7474
`two or three lowercase letters` for the language code + **optionally** `a dash (-) with two uppercase letters` for the region code. For example, all the following codes `ar`, `es`, `fr-CA`, `haw-US` are considered valid.
7575

76-
- [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/English_list.php) for language codes.
77-
- [ISO 3166](https://www.iso.org/obp/ui/#search) for region codes.
76+
- [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/English_list.php) for language codes.
77+
- [ISO 3166](https://www.iso.org/obp/ui/#search) for region codes.
7878

7979
## The multi folder
8080

@@ -240,12 +240,20 @@ Usage example:
240240
</nav>
241241
```
242242

243+
### translate_url
244+
245+
Just like the `translate_path` helper, but it prepends the `baseUrl` if set in the config.
246+
247+
```php
248+
translate_url($page, $target_locale)
249+
```
250+
243251
### locale_path
244252

245253
To avoid hard coding the `current_locale` into `paths`, input only the partial path that comes after the `locale code` part into this helper and it will handle the rest for you.
246254

247255
```php
248-
$href = locale_path($page, $partial_path)
256+
locale_path($page, $partial_path)
249257
```
250258

251259
| $partial_path | current_locale | href |
@@ -260,7 +268,7 @@ $href = locale_path($page, $partial_path)
260268
Just like the `locale_path` helper, but it prepends the `baseUrl` if set in the config.
261269

262270
```php
263-
$href = locale_url($page, $partial_path)
271+
locale_url($page, $partial_path)
264272
```
265273

266274
## Live test

src/helpers.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ function translate_path($page, ?string $target_locale = null): string
6666
};
6767
}
6868

69-
// TODO: add translate_url helper
69+
/**
70+
* @param mixed $page
71+
* @param ?string $target_locale set to the default locale if null
72+
* @return string Places $target_locale code in the current url
73+
*/
74+
function translate_url($page, ?string $target_locale = null): string
75+
{
76+
return url(translate_path($page, $target_locale));
77+
}
7078

7179
/**
7280
* @param mixed $page

tests/Pest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
*/
1313

1414
uses(Tests\TestCase::class)->in('unit/LocaleUrlTest.php');
15+
uses(Tests\TestCase::class)->in('unit/TranslateUrlTest.php');

tests/unit/TranslateUrlTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use ElaborateCode\JigsawLocalization\Mocks\PageMock;
4+
5+
test('path es => ar', function () {
6+
$this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']);
7+
8+
$page = (new PageMock)->setPath('/es/blog');
9+
10+
expect(translate_url($page, 'ar'))->toBe('https://elaboratecode.com/packages/ar/blog');
11+
});
12+
13+
test('path ar <=> fr-CA', function () {
14+
$this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']);
15+
16+
expect(translate_url((new PageMock)->setPath('/ar/blog'), 'fr-CA'))->toBe('https://elaboratecode.com/packages/fr-CA/blog');
17+
expect(translate_url((new PageMock)->setPath('/fr-CA/blog'), 'fr-CA'))->toBe('https://elaboratecode.com/packages/fr-CA/blog');
18+
});
19+
20+
test('path ar <=> haw-US', function () {
21+
$this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']);
22+
23+
expect(translate_url((new PageMock)->setPath('/ar/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog');
24+
expect(translate_url((new PageMock)->setPath('/haw-US/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog');
25+
});
26+
27+
test('path haw-US <=> fr-CA', function () {
28+
$this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']);
29+
30+
expect(translate_url((new PageMock)->setPath('/fr-CA/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog');
31+
expect(translate_url((new PageMock)->setPath('/haw-US/blog'), 'fr-CA'))->toBe('https://elaboratecode.com/packages/fr-CA/blog');
32+
});
33+
34+
test('path from DEFAULT_LOCALE', function () {
35+
$this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']);
36+
37+
expect(translate_url((new PageMock)->setPath('/blog'), 'ar'))->toBe('https://elaboratecode.com/packages/ar/blog');
38+
expect(translate_url((new PageMock)->setPath('/blog'), 'en-UK'))->toBe('https://elaboratecode.com/packages/en-UK/blog');
39+
expect(translate_url((new PageMock)->setPath('/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog');
40+
expect(translate_url((new PageMock)->setPath('/blog'), packageDefaultLocale()))->toBe('https://elaboratecode.com/packages/blog');
41+
});
42+
43+
test('path to DEFAULT_LOCALE', function () {
44+
$this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']);
45+
46+
expect([
47+
translate_url((new PageMock)->setPath('/ar/blog')),
48+
translate_url((new PageMock)->setPath('/en-UK/blog')),
49+
translate_url((new PageMock)->setPath('/haw-US/blog')),
50+
translate_url((new PageMock)->setPath('/blog')),
51+
])
52+
->each
53+
->toBe('https://elaboratecode.com/packages/blog');
54+
});

0 commit comments

Comments
 (0)