Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Translating pages names

Hi, I currently translate pages names manually using the translation files.

So I have a translation file for the language I want to translate with the URL being the key and the translation being the value. For example:

return [
    'some_page' => // translation,
    'some_other_page' => // translation,
    // And so on..
];

So for every new route that represents a page, I manually add the key-value pair to the desired language

But doing that manually for each new page seems not efficient. Is there anything to do about it, a more convenient way? Some other pattern?

Thanks

0 likes
15 replies
kokoshneta's avatar

@vincent15000 Not really… that’s just describing the approach they’re already using (which is completely manual).

2 likes
kokoshneta's avatar

How are you naming your pages to begin with? What are the contents of the pages?

Are they something like posts or similar entities which live in a database? If so, you can have a slug field on your model for each language, auto-generated using Str::slug() from the post/article/thingy’s title attribute.

Or are they just completely manually named static pages? If so, I don’t think there’s an easier way than what you’re already doing.

2 likes
Ligonsker's avatar

@kokoshneta @povilaskorop

I don't name the pages currently, but I use the URLs themselves as the keys. The pages can be several different things.

@kokoshneta yes they are mostly manually named static pages :/

@povilaskorop what do you mean storing the pages in the database? I don't do that, but which part of the page do you mean is stored in the database?

1 like
kokoshneta's avatar

@Ligonsker What exactly do you use the URLs as keys for, then? You mean that you use the name stored in the translation files as the actual URL in your route definitions? Can you give an example of such a route definition?

2 likes
Ligonsker's avatar

@kokoshneta Yes, for example I have the following routes:

https://example.com/url1/sub_url1
https://example.com/url1/sub_url2
https://example.com/url2

Then in my translation file for the desired language, I have:

return [
    '/url1/sub_url1' => 'some page name translation'
    '/url1/sub_url2' => 'some page name translation'
    '/url2' => 'some page name translation'
];
1 like
Ligonsker's avatar

@vincent15000 Exactly, I know it's a bad method for that, I just had to do it quickly per demand (But now that I temporarily have something working, I want to make it better). In this case, what would you do to improve it?

1 like
kokoshneta's avatar

@Ligonsker Well, that just made everything even less clear… for one thing, those aren’t route definitions, which is what I asked for, and for another, I still don’t understand what you mean by ‘page name’, and just writing “some page name translation” doesn’t help. You seem to be using the term ‘page name’ to refer both to URLs and to page titles?

Let’s say you have your site in English and Spanish. The translation keys correspond to the English URLs. If I have the site in Spanish and I go to example.com/url1/sub_url1, what happens? Am I redirected to example.com/some%20page%20name%20translation, which would then show me a page in Spanish?

I really don’t understand what system you’re describing here at all.

2 likes
Ligonsker's avatar

@kokoshneta Sorry what I mean by page names is that I simply have translations for the pages themselves (They do eventually match the title of that page most of the time, and sometimes they represent the that system the page is part of as a whole).

By route definitions you mean what's in web.php, if yes, for me it was the same thing since I eventually take the URLs from there, i.e. if this is the web.php for example:

Route::get('/url1', [SomeController::class, 'url1']);
Route::get('/url2', [SomeController::class, 'url2']);
Route::post('/some-api-post-url', [UserController::class, 'some_api_post_url']);
Route::get('/fetch-data', [SomeController::class, 'fetch_data']);

Then I take the URLs that represent real ages. In the above example I won't use /some-api-post-url or /fetch-data because they don't represent pages, they are only used for getting/sending data

1 like
kokoshneta's avatar

@Ligonsker What do you mean that they “don’t represent pages”? Do you mean that going directly to those endpoints won’t result in a page of HTML being shown to the user? This is normal for a POST request (where you would normally redirect to a different endpoint in the controller), but not for a GET request, unless it’s a simple redirect endpoint.

I still don’t understand what aspect of the pages you’re translating. “The pages themselves” would be the HTML output shown to the user, which would normally be an entire Blade view, not just a single entry in a translation file. Do you really mean that you’re putting entire Blade views, with markup and everything, inside your translation files as individual entries? Because that is very definitely a bad idea.

Can you give an example of what the SomeController->url1() and SomeController->fetch_data() methods (one representing a ‘real page’ and one not) would look like in practice?

2 likes
Ligonsker's avatar

@kokoshneta by page, I simply mean what the user see in the frontend, like if you go to Google homepage, for me I'll translate it as "home". Or if you go to Laracasts tutorials main office, I'll translate it as "tutorials-main" (not in English but in the desired language).

Regarding the fetch_data, it might be a method that returns a json, so that it can be called via Ajax calls for example (like the post, but only to get the data)

kokoshneta's avatar

@Ligonsker

by page, I simply mean what the user see in the frontend, like if you go to Google homepage, for me I'll translate it as "home". Or if you go to Laracasts tutorials main office, I'll translate it as "tutorials-main"

But this makes no sense at all!

‘What the user sees in the frontend’ is not “home” or “tutorials-main”. Those are individual words that don’t appear anywhere in the user’s browser if visiting those websites.

What the user sees in the frontend is a whole page full of HTML. ‘Home’ and ‘tutorials-main’ are subjective descriptions that might be used for bookmarks/favourites, or perhaps for slugs (i.e., part of the URL that appears in the browser’s address bar, such as example.com/home, example.com/tutorials-main).

Are slugs what you mean? And if so, why do you need translations of them? Slugs are only used in the URL, which is part of the route definition and therefore doesn’t change, so having them in translation files serves no purpose at all.

I think in order for any of this to make any sense, we will need to see your actual code – not just placeholder code with generic names, but your actual names and method contents:

  • a route definition (from web.php)
  • the corresponding method in the controller that route calls
  • the Blade view that’s returned from the controller method

 

———

 

(P.S.: If fetch-data is a method that returns JSON, it shouldn’t be in your web.php file. That’s an API call, not a web call, and it should be defined in your api.php file. Plus, if it’s an API endpoint, translating its ‘name’ makes even less sense!)

Please or to participate in this conversation.