"您还可以使用App facade上的setLocale方法在运行时更改活动语言:" https://laravel.com/docs/5.3/localization#introduction
public static void main(String[] args){
File file=new File(XmlProcessing.class.getResource("InputXml.txt").getFile());
// create a new DocumentBuilderFactory
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(file);
System.out.println("Before removing the Node:-");
printDocument(document, System.out);
NodeList tagList = document.getElementsByTagName("tag_name");
Element elemToBeRemoved = (Element)tagList.item( 0 );
Node prevElem = elemToBeRemoved.getPreviousSibling();
if (prevElem != null &&
prevElem .getNodeType() == Node.TEXT_NODE &&
prevElem .getNodeValue().trim().length() == 0) {
elemToBeRemoved.getParentNode().removeChild(prevElem );
}
elemToBeRemoved.getParentNode().removeChild( elemToBeRemoved );
System.out.println("\nAfter removing the node:-");
printDocument(document, System.out);
}catch(Exception excep){
excep.printStackTrace();
}
}
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc),
new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}
/** Output:-
Before removing the Node:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<tag_name>text</tag_name>
<tag_name>text</tag_name>
<tag_name>text</tag_name>
</root>
After removing the node:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<tag_name>text</tag_name>
<tag_name>text</tag_name>
</root>
*/
如果我们有类似的东西,我们如何使用$ locale:
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
//
});
答案 0 :(得分:0)
问题在于路线参数不是本地化
由于你期望路线有两个参数,你应该通过两个参数来进行闭合。
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
在你的例子中
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function( $id, $locale ) {
// this does not work.
App::setLocale( $locale );
// this does work.
App::setLocale( Request::segment( 3 ) );
Route::resource('product', 'ProductController', ['except' => [ 'show' ]]);
});
有关详细信息,请参阅route parameters
答案 1 :(得分:0)
请注意 - Route::group
回调会对任何请求执行(即使不是您的前缀!)。 Route::group
与Route::get/post/match
不相似,它就像是内部获取/发布/匹配调用的助手。
App::setLocale( $locale );
不起作用,因为Laravel
仅将Illuminate\Routing\Router
的实例传递到群组的回调中。在此阶段,尚未提取locale
前缀,甚至尚未处理URL。
App::setLocale( Request::segment( 3 ) );
将针对&#39;一/二/三&#39;执行用&#39;三&#39;作为一个地区。
你的榜样应该是:
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function() {
// test a locale
Route::get('test', function($locale){ echo $locale; });
// ProductController::index($locale)
Route::resource('product', 'ProductController', ['except' => [
'show'
]]);
});
因此,只需更新您的ProductController并添加$ locale作为参数。
备选方案:如果您希望setLocale在一个地方更新您的路线:
// set locale for '/admin/anything/[en|fr|ru|jp]/anything' only
if (Request::segment(1) === 'admin' && in_array(Request::segment(3), ['en', 'fr', 'ru', 'jp'])) {
App::setLocale(Request::segment(3));
} else {
// set default / fallback locale
App::setLocale('en');
}
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function() {
Route::resource('product', 'ProductController', ['except' => [
'show'
]]);
});