Laravel Japanese Utility
The cuongnd88/jutility
package provides a convenient way to retrieve Japanese Utility such as Japanese Postal Code, Japanese Localization
https://github.com/cuongnd88/jutility
1-Install cuongnd88/jutility
using Composer.
$ composer require cuongnd88/jutility
2-You can modify the configuration by copying it to your local config directory:
php artisan vendor:publish --provider="Cuongnd88\Jutility\JutilityServiceProvider"
You select the utility by adding --tag
option:
php artisan vendor:publish --provider="Cuongnd88\Jutility\JutilityServiceProvider" --tag=public
There are 3 options:
--tag=public
is to publish the JPostal Utility via javascript.
--tag=config
is to publish the JPostal Utility via php/laravel.
--tag=lang
is to publish the Japanese Localization Utility.
Sample Usage
JPostal Utility via Javascript
With the JPostal utility, you can achieve Japanese postal data by postal code. You just need implementing like below
resources/views/user/jpostal.blade.php
. . . . <div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Post code') }}</label> <div class="col-md-6">
<input id="zip" type="text" class="form-control" name="email" value="" onkeyup="JPostal.capture('#zip', ['#info'])">
</div>
</div> <div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Info') }}</label> <div class="col-md-6">
<input id="info" type="text" class="form-control" name="info">
</div>
</div>. . . .
<script type="text/javascript" src="{{ asset('js/jpostal/jpostal.js') }}"></script>
<script type="text/javascript">
JPostal.init();
</script>
JPostal.capture(zip, response)
:
zip
: is a string value that you can assign a value contains id or class sign in identifing zip code. For example: .zip
or #zip
.
response
is a array or function that you get the data (prefecture, city, area and street). If the array only has one item, it resturns data with comma sign. The array has 4 elements, so it returns seperated data corresponding to prefecture, city, area and street. If the resposne is a function, it will callback .
MEMO
you can use id and class signs for zip and response parameters. You can enter both postal code formats (NNN-NNNN or NNNNNNN).
<div class="col-md-6">
<input id="zip" type="text" class="form-control" name="email" value="" onkeyup="JPostal.capture('#zip', ['.prefecture', '.city', '.area', '.street'])">
</div><script type="text/javascript">
JPostal.init(); $( "#zip" ).keyup(function() {
JPostal.capture('#zip', function(data){
console.log(data);
});
});
</script>
The JPostal provides functions to select a city correspond to a prefecture
JPostal.innerPrefecturesHtml(callback)
.
JPostal.nnerCityHtmlByPref(prefTag, callback)
.
. . . .
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right">{{ __('Prefecture') }}</label> <div class="col-md-6">
<select class="form-control selectPrefecture" id="selectPrefecture">
</select>
</div>
</div> <div class="form-group row">
<label class="col-md-4 col-form-label text-md-right">{{ __('City') }}</label> <div class="col-md-6">
<select class="form-control selectCity" id="selectCity">
</select>
</div>
</div>
. . . .<script type="text/javascript" src="{{ asset('js/jpostal/jpostal.js') }}"></script>
<script type="text/javascript">
JPostal.init(); JPostal.innerPrefecturesHtml(function(prefectures){
let selectTag = '<option value="">Prefecture</option>';
for (const [key, value] of Object.entries(prefectures)) {
selectTag += `<option value="${key}">${value}</option>`;
}
$('#selectPrefecture').append(selectTag);
}); $("#selectPrefecture").change(function(){
JPostal.innerCityHtmlByPref('#selectPrefecture', function(cities){
let selectTag = '<option value="">City</option>';
for (const item in cities) {
const {id, name} = cities[item];
selectTag += `<option value="${id}">${name}</option>`;
}
$('#selectCity').append(selectTag);
});
});
</script>
JPostal Utility via PHP/Laravel
There are several functions to assist you get Japanese postal code:
jpostal_pref($code = null)
: Get Japanese prefectures by code .
dump(jpostal_pref(47));
jpostal_pref_city($prefCode, $city = null)
: Get Japanese city by prefecture code .
dump(jpostal_pref_city(47));
dump(jpostal_pref_city(1, '01101));
jpostal_code($code)
: Get Japanese postal data by code .
dump(jpostal_code('1200000'));
dump(jpostal_code('120-0000'));
jlang($key)
: Use translation strings as keys are stored as JSON files in the resources/lang/{$currentLocale}/ directory .
dump(jlang('Add Team Member'));
Japanese Localization Utility
The cuongnd88/jutility
package provides a convenient way to retrieve strings in Japanese languages. The default language for your application is stored in the config/app.php
configuration file. You may modify this value to suit the needs of your application.
. . . .
'locale' => 'ja',
. . . .
Language strings are stored in files within the resources/lang directory.
/resources
/lang
/en
messages.php
/ja
messages.php
Demo
This is demo soure code. JPostal Utility