Web multi idiomas con URLs amigables sin usar MVC
Publicado por Nelson1386 (2 intervenciones) el 10/04/2015 17:14:16
Buenos días, actualmente estoy desarrollando un proyecto que ya cuenta con 2 idiomas, los cuales se pueden seleccionar desde un DropDownList. La web está implementada con WebForms y código .vb.
Lo que necesito es que al seleccionar cada uno de los idiomas se agregue '/es' al final de la URL y quede de esta forma: http://www.miweb.com/es ó http://www.miweb.com/eu, según el idioma.
He estado investigando como lograrlo y en muchas páginas recomiendan utilizar ASP.NET MVC, lo cual no me conviene ya que requiere demasiado tiempo para aprender a usarlo.
¿Hay alguna forma de lograr lo que necesito en mi aplicación desarrollada con WebForms?
El código que estoy utilizando es el siguiente:
En MasterPage.master:
En MasterPage.master.vb:
En Default.aspx
En Default.aspx.vb:
En Varios.vb:
También tengo una carpeta llamada App_LocalResources con archivos del tipo: Default.aspx.es.resx y Default.aspx.eu.resx para cada idioma.
Gracias de antemano por su ayuda.
Lo que necesito es que al seleccionar cada uno de los idiomas se agregue '/es' al final de la URL y quede de esta forma: http://www.miweb.com/es ó http://www.miweb.com/eu, según el idioma.
He estado investigando como lograrlo y en muchas páginas recomiendan utilizar ASP.NET MVC, lo cual no me conviene ya que requiere demasiado tiempo para aprender a usarlo.
¿Hay alguna forma de lograr lo que necesito en mi aplicación desarrollada con WebForms?
El código que estoy utilizando es el siguiente:
En MasterPage.master:
1
2
3
4
5
<asp:DropDownList ID="DropDownListIdioma" runat="server" AutoPostBack="True"
CssClass="language" meta:resourcekey="DropDownListIdiomaResource1">
<asp:ListItem Value="es" meta:resourcekey="ListItemResource2">Castellano</asp:ListItem>
<asp:ListItem Value="eu" meta:resourcekey="ListItemResource1">Euskera</asp:ListItem>
</asp:DropDownList>
En MasterPage.master.vb:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Public Sub PonerIdiomaDropDownList(ByVal strIdioma As String)
If strIdioma = "es" Then
DropDownListIdioma.SelectedIndex = 0
ElseIf strIdioma = "eu" Then
DropDownListIdioma.SelectedIndex = 1
Else
DropDownListIdioma.SelectedIndex = 0
End If
End Sub
End Class
En Default.aspx
1
2
3
4
5
6
7
8
9
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %>
<%@ MasterType VirtualPath ="~/MasterPage.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="lblDescripcion" runat="server" Text="Texto de prueba"
meta:resourcekey="lblDescripcionResource1"></asp:Label>
</asp:Content>
En Default.aspx.vb:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Imports Varios
Partial Class _Default
Inherits System.Web.UI.Page
Dim strIdiomaElegido As String
Protected Overrides Sub InitializeCulture()
If Not Request.Form("ctl00$DropDownListIdioma") Is Nothing Then
'POR AQUI SOLO PASAMOS SI SE SELECCIONA EL DROPDOWNLIST
'System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(Request.Form("ctl00$DropDownListIdioma"))
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(Request.Form("ctl00$DropDownListIdioma"))
strIdiomaElegido = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
'AHORA O ACTUALIZAMOS LA COOKIE O LA CREAMOS
PonerCookieIdioma(Request, Response, strIdiomaElegido)
PonerIdioma(strIdiomaElegido)
Else
'PASAMOS POR AQUI SI NO SE HA SELECCIONADO EL IDIOMA EN EL COMBOBOX
'SI HAY COOKIE COGEMOS EL IDIOMA SI NO HAY COOKIE COGEMOS EL IDOMA DEL NAVEGADOR
If ExisteCookieIdioma(Request) Then
strIdiomaElegido = CogerIdiomaDeCookie(Request, Response)
PonerIdioma(strIdiomaElegido)
Else
strIdiomaElegido = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToString
PonerIdioma(strIdiomaElegido)
End If
End If
End Sub
Private Sub PrepararSegunIdioma(ByVal strIdioma As String)
Master.PonerIdiomaDropDownList(strIdioma)
End Sub
Public Sub PonerIdioma(ByVal strIdioma As String)
If Session("Idioma") Is Nothing Then Session("Idioma") = ""
Select Case strIdioma
Case "eb"
Session("Idioma") = strIdioma
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("eb")
Case "es"
Session("Idioma") = strIdioma
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(strIdioma)
Case "fr"
Session("Idioma") = strIdioma
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("es")
Case "eu"
Session("Idioma") = strIdioma
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(strIdioma)
Case Else
Session("Idioma") = strIdioma
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("es")
End Select
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
PrepararSegunIdioma(strIdiomaElegido)
End Sub
End Class
En Varios.vb:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Imports System
Public Class Varios
Public Shared Function ComprobarRol(ByVal strNombreRol As String) As Boolean
Dim rolesArray() As String
Try
rolesArray = Roles.GetRolesForUser()
For Each s As String In rolesArray
System.Console.WriteLine("sadasds" & s)
If s = strNombreRol Then
Return True
End If
Next
Return False
Catch ex As Exception
End Try
End Function
Public Shared Function CogerCadenaConexion(ByVal strCadenac As String) As String
Dim a As String = System.Configuration.ConfigurationManager.ConnectionStrings(strCadenac).ToString
Return a
End Function
Public Shared Function CogerIdioma(ByRef aaa As HttpRequest, ByRef bbb As HttpResponse) As String
If Not aaa.Cookies("idioma-EUSKAL") Is Nothing Then
Dim x As String
Dim objCK As HttpCookie = aaa.Cookies("idioma-EUSKAL")
x = objCK.Value
Return x
Else
Dim x As String = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToString
Return x
End If
Dim str As String = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToString
Return str
End Function
Public Shared Function ExisteCookieIdioma1(ByRef HttpReq As HttpRequest) As Boolean
If Not HttpReq.Cookies("idioma-EUSKAL") Is Nothing Then
Dim objCK As HttpCookie = HttpReq.Cookies("idioma-EUSKAL")
Select Case objCK.Value.ToLower
Case "eu"
Return True
Case "es"
Return True
Case "fr"
Return False
Case "en"
Return False
Case Else
Return False
End Select
Else
Return False
End If
End Function
Public Function DevolverIdiomaCookie(ByRef HttpReq As HttpRequest) As String
If Not HttpReq.Cookies("idioma-EUSKAL") Is Nothing Then
Dim objCK As HttpCookie = HttpReq.Cookies("idioma-EUSKAL")
Select Case objCK.Value.ToLower
End Select
End If
End Function
Public Shared Function CogerIdiomaDeCookie(ByRef aaa As HttpRequest, ByRef bbb As HttpResponse) As String
If Not aaa.Cookies("idioma-EUSKAL") Is Nothing Then
Dim x As String
Dim objCK As HttpCookie = aaa.Cookies("idioma-EUSKAL")
x = objCK.Value
Return x
Else
Return ""
End If
End Function
Public Shared Sub PonerCookieIdioma(ByRef HttpReq As HttpRequest, ByRef HttpResp As HttpResponse, ByVal strIdioma As String)
Dim Cookie_I As New HttpCookie("idioma-EUSKAL")
Cookie_I.Value = strIdioma
Cookie_I.Expires = DateTime.Now.AddMinutes(6000)
HttpResp.Cookies.Add(Cookie_I)
End Sub
Public Shared Function ExisteCookieIdioma(ByRef HttpReq As HttpRequest) As Boolean
If Not HttpReq.Cookies("idioma-EUSKAL") Is Nothing Then
Dim objCK As HttpCookie = HttpReq.Cookies("idioma-EUSKAL")
Select Case objCK.Value.ToLower
Case "eb"
Return False
Case "es"
Return True
Case "fr"
Return False
Case "eu"
Return True
Case Else
Return False
End Select
Else
Return False
End If
End Function
End Class
También tengo una carpeta llamada App_LocalResources con archivos del tipo: Default.aspx.es.resx y Default.aspx.eu.resx para cada idioma.
Gracias de antemano por su ayuda.
Valora esta pregunta


0