Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Created August 7, 2012 16:25
Show Gist options
  • Save oscarryz/3286953 to your computer and use it in GitHub Desktop.
Save oscarryz/3286953 to your computer and use it in GitHub Desktop.
Toma un archivo .xaml y genera la entrada para poner las cadenas como recursos y sustituye el contenido para llamar a esos recursos
# encoding: utf-8
# Reemplaza todo los strings no puestos como recursos por la invocación a recursos
# y crea la entrada para ser incluída en el archivo .resx y designer.cs
#
# Ejemplo: Si el archivo de entrada tiene un texto así:
#
# <TextBlock Text="Escriba nombre" />
#
# 1. El programa genera la salida del archivo modificado:
#
# <TextBlock Text="{x:Static res:Strings.Prefijo_Escriba_nombre" />
#
# 2. La entrada para el archivo resx
#
# <data name="Prefijo_Escriba_nombre" xml:space="preserve">
# <value>Escriba nombre</value>
# </data>
#
# 3. Y la entrada para el archvo Designer.cs
#
#
# public static string Prefijo_Escriba_nombre {
# get {
# return ResourceManager.GetString("Prefijo_Escriba_nombre", resourceCulture );
# }
# }
#
#Params:
# nombre_archivo.xaml El archivo .xaml que tiene el código con las cadenas
# prefijo El nombre que se pondrá antes del recurdo, ejemplo: Xyz dara: res:String.Xyx_Nombre si no se usa se escribe "Res"
#
file_name = ARGV[0]
prefijo = ARGV[1].nil? ? "Res" : ARGV[1]
new_file_content = ""
resx = ""
designer_cs = ""
added = Hash.new
File.open( file_name, "r" ) { | f |
# (?!{) Means: except those containing {
# .*? Means: any character but stop at appareance of the followint ( \" in this case )
#
new_file_content = f.read().gsub(/(Header|Text|Content)="((?!{).*?)"/) {
type = $1
text = $2
key = prefijo+"_"+text.strip.downcase.capitalize.gsub(/[\s\/\(\)]+/, '_' )
if added[key].nil?
resx += <<-DATA
<data name="#{key}" xml:space="preserve">
<value>#{text}</value>
</data>
DATA
designer_cs += <<-RESOURCE
/// <summary>
/// Looks up a localized string similar to #{text}.
/// </summary>
public static string #{key} {
get {
return ResourceManager.GetString("#{key}", resourceCulture);
}
}
RESOURCE
added[key] = true
end
"#{type}=\"{x:Static res:Strings.#{key}}\""
}
}
File.open( "salida.xaml", "w:UTF-8" ) { |f| f.puts( new_file_content ) }
File.open( "salida.resx", "w:UTF-8" ) { |f| f.puts( resx ) }
File.open( "salida.designer.cs", "w:UTF-8" ) { |f| f.puts( designer_cs ) }
@oscarryz
Copy link
Author

oscarryz commented Aug 7, 2012

Ejemplo de uso:

$ ir to_resource.rb  MyNonResources.xaml  MyFancyPrefix

$ ls -l salida.*
-rw-r--r--    1 user Administ    48181 Aug  7 16:36 salida.designer.cs
-rw-r--r--    1 user  Administ    19068 Aug  7 16:36 salida.resx
-rw-r--r--    1 user  Administ    39221 Aug  7 16:36 salida.xaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment